Skip to content

Commit c643bed

Browse files
committed
rustdoc: render gen params & where-clauses of cross-crate assoc tys in impl blocks
1 parent e0ba2d0 commit c643bed

File tree

3 files changed

+74
-43
lines changed

3 files changed

+74
-43
lines changed

src/librustdoc/clean/mod.rs

+45-43
Original file line numberDiff line numberDiff line change
@@ -1360,21 +1360,53 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
13601360
}
13611361
}
13621362

1363+
let mut predicates = tcx.explicit_predicates_of(assoc_item.def_id).predicates;
13631364
if let ty::TraitContainer = assoc_item.container {
13641365
let bounds = tcx
13651366
.explicit_item_bounds(assoc_item.def_id)
13661367
.subst_identity_iter_copied()
13671368
.map(|(c, s)| (c.as_predicate(), s));
1368-
let predicates = tcx.explicit_predicates_of(assoc_item.def_id).predicates;
1369-
let predicates =
1370-
tcx.arena.alloc_from_iter(bounds.chain(predicates.iter().copied()));
1371-
let mut generics = clean_ty_generics(
1372-
cx,
1373-
tcx.generics_of(assoc_item.def_id),
1374-
ty::GenericPredicates { parent: None, predicates },
1375-
);
1376-
// Filter out the bounds that are (likely?) directly attached to the associated type,
1377-
// as opposed to being located in the where clause.
1369+
predicates = tcx.arena.alloc_from_iter(bounds.chain(predicates.iter().copied()));
1370+
}
1371+
let mut generics = clean_ty_generics(
1372+
cx,
1373+
tcx.generics_of(assoc_item.def_id),
1374+
ty::GenericPredicates { parent: None, predicates },
1375+
);
1376+
// Move bounds that are (likely) directly attached to the parameters of the
1377+
// (generic) associated type from the where clause to the respective parameter.
1378+
// There is no guarantee that this is what the user actually wrote but we have
1379+
// no way of knowing.
1380+
let mut where_predicates = ThinVec::new();
1381+
for mut pred in generics.where_predicates {
1382+
if let WherePredicate::BoundPredicate { ty: Generic(arg), bounds, .. } = &mut pred
1383+
&& let Some(GenericParamDef {
1384+
kind: GenericParamDefKind::Type { bounds: param_bounds, .. },
1385+
..
1386+
}) = generics.params.iter_mut().find(|param| &param.name == arg)
1387+
{
1388+
param_bounds.append(bounds);
1389+
} else if let WherePredicate::RegionPredicate { lifetime: Lifetime(arg), bounds } = &mut pred
1390+
&& let Some(GenericParamDef {
1391+
kind: GenericParamDefKind::Lifetime { outlives: param_bounds },
1392+
..
1393+
}) = generics.params.iter_mut().find(|param| &param.name == arg)
1394+
{
1395+
param_bounds.extend(bounds.drain(..).map(|bound| match bound {
1396+
GenericBound::Outlives(lifetime) => lifetime,
1397+
_ => unreachable!(),
1398+
}));
1399+
} else {
1400+
where_predicates.push(pred);
1401+
}
1402+
}
1403+
generics.where_predicates = where_predicates;
1404+
1405+
if let ty::TraitContainer = assoc_item.container {
1406+
// Move bounds that are (likely) directly attached to the associated type
1407+
// from the where-clause to the associated type.
1408+
// There is no guarantee that this is what the user actually wrote but we have
1409+
// no way of knowing.
13781410
let mut bounds: Vec<GenericBound> = Vec::new();
13791411
generics.where_predicates.retain_mut(|pred| match *pred {
13801412
WherePredicate::BoundPredicate {
@@ -1431,33 +1463,6 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
14311463
}
14321464
None => bounds.push(GenericBound::maybe_sized(cx)),
14331465
}
1434-
// Move bounds that are (likely) directly attached to the parameters of the
1435-
// (generic) associated type from the where clause to the respective parameter.
1436-
// There is no guarantee that this is what the user actually wrote but we have
1437-
// no way of knowing.
1438-
let mut where_predicates = ThinVec::new();
1439-
for mut pred in generics.where_predicates {
1440-
if let WherePredicate::BoundPredicate { ty: Generic(arg), bounds, .. } = &mut pred
1441-
&& let Some(GenericParamDef {
1442-
kind: GenericParamDefKind::Type { bounds: param_bounds, .. },
1443-
..
1444-
}) = generics.params.iter_mut().find(|param| &param.name == arg)
1445-
{
1446-
param_bounds.append(bounds);
1447-
} else if let WherePredicate::RegionPredicate { lifetime: Lifetime(arg), bounds } = &mut pred
1448-
&& let Some(GenericParamDef {
1449-
kind: GenericParamDefKind::Lifetime { outlives: param_bounds },
1450-
..
1451-
}) = generics.params.iter_mut().find(|param| &param.name == arg) {
1452-
param_bounds.extend(bounds.drain(..).map(|bound| match bound {
1453-
GenericBound::Outlives(lifetime) => lifetime,
1454-
_ => unreachable!(),
1455-
}));
1456-
} else {
1457-
where_predicates.push(pred);
1458-
}
1459-
}
1460-
generics.where_predicates = where_predicates;
14611466

14621467
if tcx.defaultness(assoc_item.def_id).has_value() {
14631468
AssocTypeItem(
@@ -1469,7 +1474,6 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
14691474
None,
14701475
),
14711476
generics,
1472-
// FIXME: should we obtain the Type from HIR and pass it on here?
14731477
item_type: None,
14741478
}),
14751479
bounds,
@@ -1478,7 +1482,6 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
14781482
TyAssocTypeItem(generics, bounds)
14791483
}
14801484
} else {
1481-
// FIXME: when could this happen? Associated items in inherent impls?
14821485
AssocTypeItem(
14831486
Box::new(Typedef {
14841487
type_: clean_middle_ty(
@@ -1487,12 +1490,11 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
14871490
Some(assoc_item.def_id),
14881491
None,
14891492
),
1490-
generics: Generics {
1491-
params: ThinVec::new(),
1492-
where_predicates: ThinVec::new(),
1493-
},
1493+
generics,
14941494
item_type: None,
14951495
}),
1496+
// Associated types inside trait or inherent impls are not allowed to have
1497+
// item bounds. Thus we don't attempt to move any bounds there.
14961498
Vec::new(),
14971499
)
14981500
}

tests/rustdoc/inline_cross/assoc_item_trait_bounds.rs

+12
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,15 @@ pub use aux::Main;
4242
// @has main/trait.Aid.html
4343
// @has - '//*[@id="associatedtype.Result"]' "type Result<'inter: 'src>"
4444
pub use aux::Aid;
45+
46+
// Below, ensure that we correctly display generic parameters and where-clauses on
47+
// associated types inside trait *impls*. More particularly, check that we don't render
48+
// any bounds (here `Self::Alias<T>: ...`) as item bounds unlike all the trait test cases above.
49+
50+
// @has main/struct.Implementor.html
51+
// @has - '//*[@id="associatedtype.Alias"]' \
52+
// "type Alias<T: Eq> = T \
53+
// where \
54+
// String: From<T>, \
55+
// <Implementor as Implementee>::Alias<T>: From<<Implementor as Implementee>::Alias<T>>"
56+
pub use aux::Implementor;

tests/rustdoc/inline_cross/auxiliary/assoc_item_trait_bounds.rs

+17
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,20 @@ pub trait Helper {
4444
pub trait Aid<'src> {
4545
type Result<'inter: 'src>;
4646
}
47+
48+
pub trait Implementee {
49+
type Alias<T: Eq>
50+
where
51+
String: From<T>;
52+
}
53+
54+
pub struct Implementor;
55+
56+
impl Implementee for Implementor {
57+
type Alias<T: Eq> = T
58+
where
59+
String: From<T>,
60+
// We will check that this bound doesn't get turned into an item bound since
61+
// associated types in impls are not allowed to have any.
62+
Self::Alias<T>: From<Self::Alias<T>>;
63+
}

0 commit comments

Comments
 (0)