Skip to content

Commit af9c8d7

Browse files
committed
rustdoc: Collapse RequiredAssocTy & AssocTy
1 parent 027d001 commit af9c8d7

18 files changed

Lines changed: 140 additions & 267 deletions

‎src/librustdoc/clean/inline.rs‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,17 +391,12 @@ fn build_type_alias(
391391
cx: &mut DocContext<'_>,
392392
did: DefId,
393393
ret: &mut Vec<Item>,
394-
) -> Box<clean::TypeAlias> {
394+
) -> Box<clean::TyAlias> {
395395
let ty = cx.tcx.type_of(did).instantiate_identity().skip_norm_wip();
396396
let type_ = clean_middle_ty(ty::Binder::dummy(ty), cx, Some(did), None);
397397
let inner_type = clean_ty_alias_inner_type(ty, cx, ret);
398398

399-
Box::new(clean::TypeAlias {
400-
type_,
401-
generics: clean_ty_generics(cx, did),
402-
inner_type,
403-
item_type: None,
404-
})
399+
Box::new(clean::TyAlias { ty: type_, generics: clean_ty_generics(cx, did), inner_type })
405400
}
406401

407402
/// Builds all inherent implementations of an ADT (struct/union/enum) or Trait item/path/reexport.

‎src/librustdoc/clean/mod.rs‎

Lines changed: 40 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,25 +1315,18 @@ fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext
13151315
clean_function(cx, sig, trait_item.generics, params, local_did);
13161316
ItemKind::AssocFn(Box::new(AssocFn { generics, decl, body }))
13171317
}
1318-
hir::TraitItemKind::Type(bounds, Some(default)) => {
1318+
hir::TraitItemKind::Type(bounds, ty) => {
13191319
let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
13201320
let bounds = bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect();
1321-
let item_type =
1322-
clean_middle_ty(ty::Binder::dummy(lower_ty(cx.tcx, default)), cx, None, None);
1323-
ItemKind::AssocTy(
1324-
Box::new(TypeAlias {
1325-
type_: clean_ty(default, cx),
1326-
generics,
1327-
inner_type: None,
1328-
item_type: Some(item_type),
1329-
}),
1321+
ItemKind::AssocTy(Box::new(AssocTy {
1322+
generics,
13301323
bounds,
1331-
)
1332-
}
1333-
hir::TraitItemKind::Type(bounds, None) => {
1334-
let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
1335-
let bounds = bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect();
1336-
ItemKind::RequiredAssocTy(generics, bounds)
1324+
ty: ty.map(|ty| {
1325+
let middle_ty = ty::Binder::dummy(lower_ty(cx.tcx, ty));
1326+
let middle_ty = Some(clean_middle_ty(middle_ty, cx, None, None));
1327+
AliasedTy { ty: clean_ty(ty, cx), middle_ty }
1328+
}),
1329+
}))
13371330
}
13381331
};
13391332
Item::from_def_id_and_parts(local_did, Some(trait_item.ident.name), inner, cx.tcx)
@@ -1364,20 +1357,16 @@ pub(crate) fn clean_impl_item<'tcx>(
13641357
})),
13651358
}))
13661359
}
1367-
hir::ImplItemKind::Type(hir_ty) => {
1368-
let type_ = clean_ty(hir_ty, cx);
1360+
hir::ImplItemKind::Type(ty) => {
13691361
let generics = clean_generics(impl_.generics, cx);
1370-
let item_type =
1371-
clean_middle_ty(ty::Binder::dummy(lower_ty(cx.tcx, hir_ty)), cx, None, None);
1372-
ItemKind::AssocTy(
1373-
Box::new(TypeAlias {
1374-
type_,
1375-
generics,
1376-
inner_type: None,
1377-
item_type: Some(item_type),
1378-
}),
1379-
Vec::new(),
1380-
)
1362+
let middle_ty =
1363+
clean_middle_ty(ty::Binder::dummy(lower_ty(cx.tcx, ty)), cx, None, None);
1364+
let ty = clean_ty(ty, cx);
1365+
ItemKind::AssocTy(Box::new(AssocTy {
1366+
generics,
1367+
bounds: Vec::new(),
1368+
ty: Some(AliasedTy { ty, middle_ty: Some(middle_ty) }),
1369+
}))
13811370
}
13821371
};
13831372

@@ -1487,7 +1476,7 @@ pub(crate) fn clean_middle_assoc_item(assoc_item: &ty::AssocItem, cx: &mut DocCo
14871476
);
14881477
simplify::move_bounds_to_generic_parameters(&mut generics);
14891478

1490-
if let ty::AssocContainer::Trait = assoc_item.container {
1479+
let (bounds, ty) = if let ty::AssocContainer::Trait = assoc_item.container {
14911480
// Move bounds that are (likely) directly attached to the associated type
14921481
// from the where-clause to the associated type.
14931482
// There is no guarantee that this is what the user actually wrote but we have
@@ -1555,50 +1544,24 @@ pub(crate) fn clean_middle_assoc_item(assoc_item: &ty::AssocItem, cx: &mut DocCo
15551544
None => bounds.push(GenericBound::maybe_sized(cx)),
15561545
}
15571546

1558-
if tcx.defaultness(assoc_item.def_id).has_value() {
1559-
ItemKind::AssocTy(
1560-
Box::new(TypeAlias {
1561-
type_: clean_middle_ty(
1562-
ty::Binder::dummy(
1563-
tcx.type_of(assoc_item.def_id)
1564-
.instantiate_identity()
1565-
.skip_norm_wip(),
1566-
),
1567-
cx,
1568-
Some(assoc_item.def_id),
1569-
None,
1570-
),
1571-
generics,
1572-
inner_type: None,
1573-
item_type: None,
1574-
}),
1575-
bounds,
1576-
)
1577-
} else {
1578-
ItemKind::RequiredAssocTy(generics, bounds)
1579-
}
1547+
let ty = tcx.defaultness(assoc_item.def_id).has_value().then(|| {
1548+
let ty = tcx.type_of(assoc_item.def_id).instantiate_identity().skip_norm_wip();
1549+
clean_middle_ty(ty::Binder::dummy(ty), cx, Some(assoc_item.def_id), None)
1550+
});
1551+
1552+
(bounds, ty)
15801553
} else {
1581-
ItemKind::AssocTy(
1582-
Box::new(TypeAlias {
1583-
type_: clean_middle_ty(
1584-
ty::Binder::dummy(
1585-
tcx.type_of(assoc_item.def_id)
1586-
.instantiate_identity()
1587-
.skip_norm_wip(),
1588-
),
1589-
cx,
1590-
Some(assoc_item.def_id),
1591-
None,
1592-
),
1593-
generics,
1594-
inner_type: None,
1595-
item_type: None,
1596-
}),
1597-
// Associated types inside trait or inherent impls are not allowed to have
1598-
// item bounds. Thus we don't attempt to move any bounds there.
1599-
Vec::new(),
1600-
)
1601-
}
1554+
let ty = tcx.type_of(assoc_item.def_id).instantiate_identity().skip_norm_wip();
1555+
let ty = clean_middle_ty(ty::Binder::dummy(ty), cx, Some(assoc_item.def_id), None);
1556+
// Associated types inside trait or inherent impls are not allowed to have
1557+
// item bounds. Thus we don't attempt to move any bounds there.
1558+
(Vec::new(), Some(ty))
1559+
};
1560+
ItemKind::AssocTy(Box::new(AssocTy {
1561+
generics,
1562+
bounds,
1563+
ty: ty.map(|ty| AliasedTy { ty, middle_ty: None }),
1564+
}))
16021565
}
16031566
};
16041567

@@ -2948,10 +2911,10 @@ fn clean_maybe_renamed_item<'tcx>(
29482911
})),
29492912
hir::ItemKind::TyAlias(_, generics, ty) => {
29502913
*cx.current_type_aliases.entry(def_id).or_insert(0) += 1;
2951-
let rustdoc_ty = clean_ty(ty, cx);
2952-
let type_ =
2953-
clean_middle_ty(ty::Binder::dummy(lower_ty(cx.tcx, ty)), cx, None, None);
2914+
29542915
let generics = clean_generics(generics, cx);
2916+
let type_ = clean_ty(ty, cx);
2917+
29552918
if let Some(count) = cx.current_type_aliases.get_mut(&def_id) {
29562919
*count -= 1;
29572920
if *count == 0 {
@@ -2960,18 +2923,12 @@ fn clean_maybe_renamed_item<'tcx>(
29602923
}
29612924

29622925
let ty = cx.tcx.type_of(def_id).instantiate_identity().skip_norm_wip();
2963-
29642926
let mut ret = Vec::new();
29652927
let inner_type = clean_ty_alias_inner_type(ty, cx, &mut ret);
29662928

29672929
ret.push(generate_item_with_correct_attrs(
29682930
cx,
2969-
ItemKind::TyAlias(Box::new(TypeAlias {
2970-
generics,
2971-
inner_type,
2972-
type_: rustdoc_ty,
2973-
item_type: Some(type_),
2974-
})),
2931+
ItemKind::TyAlias(Box::new(TyAlias { generics, inner_type, ty: type_ })),
29752932
item.owner_id.def_id.to_def_id(),
29762933
name,
29772934
import_ids,

‎src/librustdoc/clean/types.rs‎

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -646,14 +646,13 @@ impl Item {
646646
pub(crate) fn is_variant(&self) -> bool {
647647
self.type_() == ItemType::Variant
648648
}
649-
pub(crate) fn is_associated_type(&self) -> bool {
650-
matches!(self.kind, ItemKind::AssocTy(..) | ItemKind::Stripped(ItemKind::AssocTy(..)))
649+
pub(crate) fn is_assoc_ty_with_body(&self) -> bool {
650+
let kind = if let ItemKind::Stripped(kind) = &self.kind { kind } else { &self.kind };
651+
matches!(kind, ItemKind::AssocTy(AssocTy { ty: Some(_), .. }))
651652
}
652-
pub(crate) fn is_required_associated_type(&self) -> bool {
653-
matches!(
654-
self.kind,
655-
ItemKind::RequiredAssocTy(..) | ItemKind::Stripped(ItemKind::RequiredAssocTy(..))
656-
)
653+
pub(crate) fn is_assoc_ty_without_body(&self) -> bool {
654+
let kind = if let ItemKind::Stripped(kind) = &self.kind { kind } else { &self.kind };
655+
matches!(kind, ItemKind::AssocTy(AssocTy { ty: None, .. }))
657656
}
658657
pub(crate) fn is_assoc_const_with_body(&self) -> bool {
659658
let kind = if let ItemKind::Stripped(kind) = &self.kind { kind } else { &self.kind };
@@ -861,10 +860,7 @@ impl Item {
861860
// Variants always inherit visibility
862861
ItemKind::Variant(..) | ItemKind::Impl(..) => return None,
863862
// Trait items inherit the trait's visibility
864-
ItemKind::AssocConst(..)
865-
| ItemKind::AssocTy(..)
866-
| ItemKind::RequiredAssocTy(..)
867-
| ItemKind::AssocFn(..) => {
863+
ItemKind::AssocConst(..) | ItemKind::AssocTy(..) | ItemKind::AssocFn(..) => {
868864
match tcx.associated_item(def_id).container {
869865
// Trait impl items always inherit the impl's visibility --
870866
// we don't want to show `pub`.
@@ -904,7 +900,7 @@ pub(crate) enum ItemKind {
904900
Enum(Enum),
905901
Fn(Box<Function>),
906902
Module(Module),
907-
TyAlias(Box<TypeAlias>),
903+
TyAlias(Box<TyAlias>),
908904
Static(Static),
909905
Trait(Box<Trait>),
910906
TraitAlias(TraitAlias),
@@ -932,12 +928,7 @@ pub(crate) enum ItemKind {
932928
Primitive(PrimitiveType),
933929
Const(Box<Constant>),
934930
AssocConst(Box<AssocConst>),
935-
/// A required associated type in a trait declaration.
936-
///
937-
/// The bounds may be non-empty if there is a `where` clause.
938-
RequiredAssocTy(Generics, Vec<GenericBound>),
939-
/// An associated type in a trait impl or a provided one in a trait declaration.
940-
AssocTy(Box<TypeAlias>, Vec<GenericBound>),
931+
AssocTy(Box<AssocTy>),
941932
/// An item that has been stripped by a rustdoc pass
942933
Stripped(Box<ItemKind>),
943934
/// This item represents an anonymous constant with a `#[doc(keyword = "...")]` attribute which is used
@@ -980,7 +971,6 @@ impl ItemKind {
980971
| Self::ProcMacro(_)
981972
| Self::Primitive(_)
982973
| Self::AssocConst(..)
983-
| Self::RequiredAssocTy(..)
984974
| Self::AssocTy(..)
985975
| Self::Stripped(_)
986976
| Self::Keyword
@@ -2231,19 +2221,28 @@ impl TypeAliasInnerType {
22312221
}
22322222

22332223
#[derive(Clone, Debug)]
2234-
pub(crate) struct TypeAlias {
2235-
pub(crate) type_: Type,
2224+
pub(crate) struct TyAlias {
22362225
pub(crate) generics: Generics,
2226+
pub(crate) ty: Type,
22372227
/// Inner `AdtDef` type, ie `type TyKind = IrTyKind<Adt, Ty>`,
22382228
/// to be shown directly on the typedef page.
22392229
pub(crate) inner_type: Option<TypeAliasInnerType>,
2240-
/// `type_` can come from either the HIR or from metadata. If it comes from HIR, it may be a type
2241-
/// alias instead of the final type. This will always have the final type, regardless of whether
2242-
/// `type_` came from HIR or from metadata.
2243-
///
2244-
/// If `item_type.is_none()`, `type_` is guaranteed to come from metadata (and therefore hold the
2245-
/// final type).
2246-
pub(crate) item_type: Option<Type>,
2230+
}
2231+
2232+
#[derive(Clone, Debug)]
2233+
pub(crate) struct AssocTy {
2234+
pub(crate) generics: Generics,
2235+
pub(crate) bounds: Vec<GenericBound>,
2236+
pub(crate) ty: Option<AliasedTy>,
2237+
}
2238+
2239+
#[derive(Clone, Debug)]
2240+
pub(crate) struct AliasedTy {
2241+
pub(crate) ty: Type,
2242+
// FIXME: Somehow get rid of this field.
2243+
/// Iff [`Self::ty`] was cleaned from the HIR, this contains
2244+
/// the corresponding type cleaned from the middle::ty IR.
2245+
pub(crate) middle_ty: Option<Type>,
22472246
}
22482247

22492248
#[derive(Clone, PartialEq, Eq, Debug, Hash)]

‎src/librustdoc/clean/utils.rs‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use crate::clean::auto_trait::synthesize_auto_trait_impls;
2323
use crate::clean::blanket_impl::synthesize_blanket_impls;
2424
use crate::clean::render_macro_matchers::render_macro_matcher;
2525
use crate::clean::{
26-
AssocItemConstraint, AssocItemConstraintKind, Crate, ExternalCrate, Generic, GenericArg,
27-
GenericArgs, ImportSource, Item, ItemKind, Lifetime, Path, PathSegment, Primitive,
26+
AssocItemConstraint, AssocItemConstraintKind, AssocTy, Crate, ExternalCrate, Generic,
27+
GenericArg, GenericArgs, ImportSource, Item, ItemKind, Lifetime, Path, PathSegment, Primitive,
2828
PrimitiveType, Term, Type, clean_doc_module, clean_middle_const, clean_middle_region,
2929
clean_middle_ty, inline,
3030
};
@@ -275,19 +275,16 @@ pub(crate) fn build_deref_target_impls(
275275
let tcx = cx.tcx;
276276

277277
for item in items {
278-
let target = match item.kind {
279-
ItemKind::AssocTy(ref t, _) => &t.type_,
280-
_ => continue,
281-
};
278+
let ItemKind::AssocTy(AssocTy { ty: Some(target), .. }) = &item.kind else { continue };
282279

283-
if let Some(prim) = target.primitive_type() {
280+
if let Some(prim) = target.ty.primitive_type() {
284281
let _prof_timer = tcx.sess.prof.generic_activity("build_primitive_inherent_impls");
285282
for did in prim.impls(tcx).filter(|did| !did.is_local()) {
286283
cx.with_param_env(did, |cx| {
287284
inline::build_impl(cx, did, None, ret);
288285
});
289286
}
290-
} else if let Type::Path { path } = target {
287+
} else if let Type::Path { path } = &target.ty {
291288
let did = path.def_id();
292289
if !did.is_local() {
293290
cx.with_param_env(did, |cx| {

‎src/librustdoc/fold.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ pub(crate) trait DocFolder: Sized {
9191
| ItemKind::ProcMacro(_)
9292
| ItemKind::Primitive(_)
9393
| ItemKind::AssocConst(..)
94-
| ItemKind::RequiredAssocTy(..)
9594
| ItemKind::AssocTy(..)
9695
| ItemKind::Keyword
9796
| ItemKind::Attribute

‎src/librustdoc/formats/cache.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,6 @@ impl DocFolder for CacheBuilder<'_, '_> {
448448
| ItemKind::AssocFn(..)
449449
| ItemKind::StructField(..)
450450
| ItemKind::AssocConst(..)
451-
| ItemKind::RequiredAssocTy(..)
452451
| ItemKind::AssocTy(..)
453452
| ItemKind::Stripped(..)
454453
| ItemKind::Keyword
@@ -547,15 +546,16 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
547546
let item_def_id = item.item_id.as_def_id().unwrap();
548547
let (parent_did, parent_path) = match item.kind {
549548
ItemKind::Stripped(..) => return,
550-
ItemKind::AssocConst(clean::AssocConst { rhs: Some(_), .. }) | ItemKind::AssocTy(..)
549+
ItemKind::AssocConst(clean::AssocConst { rhs: Some(_), .. })
550+
| ItemKind::AssocTy(clean::AssocTy { ty: Some(_), .. })
551551
if cache.parent_stack.last().is_some_and(|parent| parent.is_trait_impl()) =>
552552
{
553553
// skip associated items in trait impls
554554
return;
555555
}
556556
ItemKind::AssocFn(clean::AssocFn { body: None, .. })
557557
| ItemKind::AssocConst(clean::AssocConst { rhs: None, .. })
558-
| ItemKind::RequiredAssocTy(..)
558+
| ItemKind::AssocTy(clean::AssocTy { ty: None, .. })
559559
| ItemKind::StructField(..)
560560
| ItemKind::Variant(..) => {
561561
// Don't index if containing module is stripped (i.e., private),

‎src/librustdoc/formats/item_type.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl<'a> From<&'a clean::Item> for ItemType {
138138
ItemKind::DeclMacro(..) => ItemType::Macro,
139139
ItemKind::Primitive(..) => ItemType::Primitive,
140140
ItemKind::AssocConst(..) => ItemType::AssocConst,
141-
ItemKind::RequiredAssocTy(..) | ItemKind::AssocTy(..) => ItemType::AssocType,
141+
ItemKind::AssocTy(..) => ItemType::AssocType,
142142
ItemKind::ForeignTy => ItemType::ForeignType,
143143
ItemKind::Keyword => ItemType::Keyword,
144144
ItemKind::Attribute => ItemType::Attribute,

0 commit comments

Comments
 (0)