Skip to content

Commit 628da35

Browse files
committed
[WIP] Collapse RequiredAssocTy & AssocTy
1 parent b24af7b commit 628da35

16 files changed

Lines changed: 123 additions & 237 deletions

File tree

‎src/librustdoc/clean/inline.rs‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,7 @@ fn build_type_alias(
388388
let type_ = clean_middle_ty(ty::Binder::dummy(ty), cx, Some(did), None);
389389
let inner_type = clean_ty_alias_inner_type(ty, cx, ret);
390390

391-
Box::new(clean::TypeAlias {
392-
type_,
393-
generics: clean_ty_generics(cx, did),
394-
inner_type,
395-
item_type: None,
396-
})
391+
Box::new(clean::TypeAlias { type_, generics: clean_ty_generics(cx, did), inner_type })
397392
}
398393

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

‎src/librustdoc/clean/mod.rs‎

Lines changed: 44 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,22 +1297,24 @@ fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext
12971297
hir::TraitItemKind::Type(bounds, Some(default)) => {
12981298
let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
12991299
let bounds = bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect();
1300-
let item_type =
1300+
let type_from_middle =
13011301
clean_middle_ty(ty::Binder::dummy(lower_ty(cx.tcx, default)), cx, None, None);
1302-
ItemKind::AssocTy(
1303-
Box::new(TypeAlias {
1304-
type_: clean_ty(default, cx),
1305-
generics,
1306-
inner_type: None,
1307-
item_type: Some(item_type),
1308-
}),
1302+
ItemKind::AssocTy(Box::new(AssocTy {
1303+
generics,
13091304
bounds,
1310-
)
1305+
type_: Some(clean_ty(default, cx)),
1306+
type_guaranteed_to_come_from_middle: Some(type_from_middle),
1307+
}))
13111308
}
13121309
hir::TraitItemKind::Type(bounds, None) => {
13131310
let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
13141311
let bounds = bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect();
1315-
ItemKind::RequiredAssocTy(generics, bounds)
1312+
ItemKind::AssocTy(Box::new(AssocTy {
1313+
generics,
1314+
bounds,
1315+
type_: None,
1316+
type_guaranteed_to_come_from_middle: None,
1317+
}))
13161318
}
13171319
};
13181320
Item::from_def_id_and_parts(local_did, Some(trait_item.ident.name), inner, cx.tcx)
@@ -1342,17 +1344,14 @@ pub(crate) fn clean_impl_item<'tcx>(
13421344
hir::ImplItemKind::Type(hir_ty) => {
13431345
let type_ = clean_ty(hir_ty, cx);
13441346
let generics = clean_generics(impl_.generics, cx);
1345-
let item_type =
1347+
let type_from_middle =
13461348
clean_middle_ty(ty::Binder::dummy(lower_ty(cx.tcx, hir_ty)), cx, None, None);
1347-
ItemKind::AssocTy(
1348-
Box::new(TypeAlias {
1349-
type_,
1350-
generics,
1351-
inner_type: None,
1352-
item_type: Some(item_type),
1353-
}),
1354-
Vec::new(),
1355-
)
1349+
ItemKind::AssocTy(Box::new(AssocTy {
1350+
generics,
1351+
bounds: Vec::new(),
1352+
type_: Some(type_),
1353+
type_guaranteed_to_come_from_middle: Some(type_from_middle),
1354+
}))
13561355
}
13571356
};
13581357

@@ -1462,7 +1461,7 @@ pub(crate) fn clean_middle_assoc_item(assoc_item: &ty::AssocItem, cx: &mut DocCo
14621461
);
14631462
simplify::move_bounds_to_generic_parameters(&mut generics);
14641463

1465-
if let ty::AssocContainer::Trait = assoc_item.container {
1464+
let (bounds, type_) = if let ty::AssocContainer::Trait = assoc_item.container {
14661465
// Move bounds that are (likely) directly attached to the associated type
14671466
// from the where-clause to the associated type.
14681467
// There is no guarantee that this is what the user actually wrote but we have
@@ -1530,50 +1529,27 @@ pub(crate) fn clean_middle_assoc_item(assoc_item: &ty::AssocItem, cx: &mut DocCo
15301529
None => bounds.push(GenericBound::maybe_sized(cx)),
15311530
}
15321531

1533-
if tcx.defaultness(assoc_item.def_id).has_value() {
1534-
ItemKind::AssocTy(
1535-
Box::new(TypeAlias {
1536-
type_: clean_middle_ty(
1537-
ty::Binder::dummy(
1538-
tcx.type_of(assoc_item.def_id)
1539-
.instantiate_identity()
1540-
.skip_norm_wip(),
1541-
),
1542-
cx,
1543-
Some(assoc_item.def_id),
1544-
None,
1545-
),
1546-
generics,
1547-
inner_type: None,
1548-
item_type: None,
1549-
}),
1550-
bounds,
1551-
)
1552-
} else {
1553-
ItemKind::RequiredAssocTy(generics, bounds)
1554-
}
1532+
let type_ = tcx.defaultness(assoc_item.def_id).has_value().then(|| {
1533+
let ty = tcx.type_of(assoc_item.def_id).instantiate_identity().skip_norm_wip();
1534+
clean_middle_ty(ty::Binder::dummy(ty), cx, Some(assoc_item.def_id), None)
1535+
});
1536+
1537+
(bounds, type_)
15551538
} else {
1556-
ItemKind::AssocTy(
1557-
Box::new(TypeAlias {
1558-
type_: clean_middle_ty(
1559-
ty::Binder::dummy(
1560-
tcx.type_of(assoc_item.def_id)
1561-
.instantiate_identity()
1562-
.skip_norm_wip(),
1563-
),
1564-
cx,
1565-
Some(assoc_item.def_id),
1566-
None,
1567-
),
1568-
generics,
1569-
inner_type: None,
1570-
item_type: None,
1571-
}),
1572-
// Associated types inside trait or inherent impls are not allowed to have
1573-
// item bounds. Thus we don't attempt to move any bounds there.
1574-
Vec::new(),
1575-
)
1576-
}
1539+
// Associated types inside trait or inherent impls are not allowed to have
1540+
// item bounds. Thus we don't attempt to move any bounds there.
1541+
let bounds = Vec::new();
1542+
let ty = tcx.type_of(assoc_item.def_id).instantiate_identity().skip_norm_wip();
1543+
let type_ =
1544+
clean_middle_ty(ty::Binder::dummy(ty), cx, Some(assoc_item.def_id), None);
1545+
(bounds, Some(type_))
1546+
};
1547+
ItemKind::AssocTy(Box::new(AssocTy {
1548+
generics,
1549+
bounds,
1550+
type_,
1551+
type_guaranteed_to_come_from_middle: None,
1552+
}))
15771553
}
15781554
};
15791555

@@ -2893,10 +2869,10 @@ fn clean_maybe_renamed_item<'tcx>(
28932869
})),
28942870
hir::ItemKind::TyAlias(_, generics, ty) => {
28952871
*cx.current_type_aliases.entry(def_id).or_insert(0) += 1;
2896-
let rustdoc_ty = clean_ty(ty, cx);
2897-
let type_ =
2898-
clean_middle_ty(ty::Binder::dummy(lower_ty(cx.tcx, ty)), cx, None, None);
2872+
28992873
let generics = clean_generics(generics, cx);
2874+
let type_ = clean_ty(ty, cx);
2875+
29002876
if let Some(count) = cx.current_type_aliases.get_mut(&def_id) {
29012877
*count -= 1;
29022878
if *count == 0 {
@@ -2905,18 +2881,12 @@ fn clean_maybe_renamed_item<'tcx>(
29052881
}
29062882

29072883
let ty = cx.tcx.type_of(def_id).instantiate_identity().skip_norm_wip();
2908-
29092884
let mut ret = Vec::new();
29102885
let inner_type = clean_ty_alias_inner_type(ty, cx, &mut ret);
29112886

29122887
ret.push(generate_item_with_correct_attrs(
29132888
cx,
2914-
ItemKind::TyAlias(Box::new(TypeAlias {
2915-
generics,
2916-
inner_type,
2917-
type_: rustdoc_ty,
2918-
item_type: Some(type_),
2919-
})),
2889+
ItemKind::TyAlias(Box::new(TypeAlias { generics, inner_type, type_ })),
29202890
item.owner_id.def_id.to_def_id(),
29212891
name,
29222892
import_ids,

‎src/librustdoc/clean/types.rs‎

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -645,14 +645,13 @@ impl Item {
645645
pub(crate) fn is_variant(&self) -> bool {
646646
self.type_() == ItemType::Variant
647647
}
648-
pub(crate) fn is_associated_type(&self) -> bool {
649-
matches!(self.kind, ItemKind::AssocTy(..) | ItemKind::Stripped(ItemKind::AssocTy(..)))
648+
pub(crate) fn is_assoc_ty_with_body(&self) -> bool {
649+
let kind = if let ItemKind::Stripped(kind) = &self.kind { kind } else { &self.kind };
650+
matches!(kind, ItemKind::AssocTy(AssocTy { type_: Some(_), .. }))
650651
}
651-
pub(crate) fn is_required_associated_type(&self) -> bool {
652-
matches!(
653-
self.kind,
654-
ItemKind::RequiredAssocTy(..) | ItemKind::Stripped(ItemKind::RequiredAssocTy(..))
655-
)
652+
pub(crate) fn is_assoc_ty_without_body(&self) -> bool {
653+
let kind = if let ItemKind::Stripped(kind) = &self.kind { kind } else { &self.kind };
654+
matches!(kind, ItemKind::AssocTy(AssocTy { type_: None, .. }))
656655
}
657656
pub(crate) fn is_assoc_const_with_body(&self) -> bool {
658657
let kind = if let ItemKind::Stripped(kind) = &self.kind { kind } else { &self.kind };
@@ -860,10 +859,7 @@ impl Item {
860859
// Variants always inherit visibility
861860
ItemKind::Variant(..) | ItemKind::Impl(..) => return None,
862861
// Trait items inherit the trait's visibility
863-
ItemKind::AssocConst(..)
864-
| ItemKind::AssocTy(..)
865-
| ItemKind::RequiredAssocTy(..)
866-
| ItemKind::AssocFn(..) => {
862+
ItemKind::AssocConst(..) | ItemKind::AssocTy(..) | ItemKind::AssocFn(..) => {
867863
match tcx.associated_item(def_id).container {
868864
// Trait impl items always inherit the impl's visibility --
869865
// we don't want to show `pub`.
@@ -931,12 +927,7 @@ pub(crate) enum ItemKind {
931927
Primitive(PrimitiveType),
932928
Const(Box<Constant>),
933929
AssocConst(Box<AssocConst>),
934-
/// A required associated type in a trait declaration.
935-
///
936-
/// The bounds may be non-empty if there is a `where` clause.
937-
RequiredAssocTy(Generics, Vec<GenericBound>),
938-
/// An associated type in a trait impl or a provided one in a trait declaration.
939-
AssocTy(Box<TypeAlias>, Vec<GenericBound>),
930+
AssocTy(Box<AssocTy>),
940931
/// An item that has been stripped by a rustdoc pass
941932
Stripped(Box<ItemKind>),
942933
/// This item represents an anonymous constant with a `#[doc(keyword = "...")]` attribute which is used
@@ -979,7 +970,6 @@ impl ItemKind {
979970
| Self::ProcMacro(_)
980971
| Self::Primitive(_)
981972
| Self::AssocConst(..)
982-
| Self::RequiredAssocTy(..)
983973
| Self::AssocTy(..)
984974
| Self::Stripped(_)
985975
| Self::Keyword
@@ -2224,18 +2214,21 @@ impl TypeAliasInnerType {
22242214

22252215
#[derive(Clone, Debug)]
22262216
pub(crate) struct TypeAlias {
2227-
pub(crate) type_: Type,
22282217
pub(crate) generics: Generics,
2218+
pub(crate) type_: Type,
22292219
/// Inner `AdtDef` type, ie `type TyKind = IrTyKind<Adt, Ty>`,
22302220
/// to be shown directly on the typedef page.
22312221
pub(crate) inner_type: Option<TypeAliasInnerType>,
2232-
/// `type_` can come from either the HIR or from metadata. If it comes from HIR, it may be a type
2233-
/// alias instead of the final type. This will always have the final type, regardless of whether
2234-
/// `type_` came from HIR or from metadata.
2235-
///
2236-
/// If `item_type.is_none()`, `type_` is guaranteed to come from metadata (and therefore hold the
2237-
/// final type).
2238-
pub(crate) item_type: Option<Type>,
2222+
}
2223+
2224+
#[derive(Clone, Debug)]
2225+
pub(crate) struct AssocTy {
2226+
pub(crate) generics: Generics,
2227+
pub(crate) bounds: Vec<GenericBound>,
2228+
pub(crate) type_: Option<Type>,
2229+
// FIXME: Get rid of this somehow!
2230+
// FIXME(fmease): If `type_` is None this is guaranteed to be None, too! Force this via the type system
2231+
pub(crate) type_guaranteed_to_come_from_middle: Option<Type>,
22392232
}
22402233

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

‎src/librustdoc/clean/utils.rs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use crate::clean::auto_trait::synthesize_auto_trait_impls;
2424
use crate::clean::blanket_impl::synthesize_blanket_impls;
2525
use crate::clean::render_macro_matchers::render_macro_matcher;
2626
use crate::clean::{
27-
AssocItemConstraint, AssocItemConstraintKind, Crate, ExternalCrate, Generic, GenericArg,
28-
GenericArgs, ImportSource, Item, ItemKind, Lifetime, Path, PathSegment, Primitive,
27+
AssocItemConstraint, AssocItemConstraintKind, AssocTy, Crate, ExternalCrate, Generic,
28+
GenericArg, GenericArgs, ImportSource, Item, ItemKind, Lifetime, Path, PathSegment, Primitive,
2929
PrimitiveType, Term, Type, clean_doc_module, clean_middle_const, clean_middle_region,
3030
clean_middle_ty, inline,
3131
};
@@ -276,10 +276,7 @@ pub(crate) fn build_deref_target_impls(
276276
let tcx = cx.tcx;
277277

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

284281
if let Some(prim) = target.primitive_type() {
285282
let _prof_timer = tcx.sess.prof.generic_activity("build_primitive_inherent_impls");

‎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 { type_: 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(_, None)
557557
| ItemKind::AssocConst(clean::AssocConst { rhs: None, .. })
558-
| ItemKind::RequiredAssocTy(..)
558+
| ItemKind::AssocTy(clean::AssocTy { type_: 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)