Skip to content

Commit f14d69c

Browse files
authored
Rollup merge of #134321 - dtolnay:docassocconst, r=fmease
Hide `= _` as associated constant value inside impl blocks Closes #134320. ### Before: <img src="https://github.com/user-attachments/assets/19d28811-45d2-4563-9726-f40c6af411c6" width="300">&nbsp;<img src="https://github.com/user-attachments/assets/1ecf8764-97ce-47f0-87fa-3b174d2fc578" width="300"> ### After: <img src="https://github.com/user-attachments/assets/6408c4ca-b1c4-42e4-884b-248833a4865f" width="300">&nbsp;<img src="https://github.com/user-attachments/assets/df2f6981-16f6-409f-8abb-73c0a4a71d6b" width="300"> r? `@fmease`
2 parents a53204f + 7ee31eb commit f14d69c

16 files changed

+201
-100
lines changed

Diff for: src/librustdoc/clean/mod.rs

+27-20
Original file line numberDiff line numberDiff line change
@@ -1222,22 +1222,24 @@ fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext
12221222
let local_did = trait_item.owner_id.to_def_id();
12231223
cx.with_param_env(local_did, |cx| {
12241224
let inner = match trait_item.kind {
1225-
hir::TraitItemKind::Const(ty, Some(default)) => AssocConstItem(Box::new(Constant {
1226-
generics: enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx)),
1227-
kind: ConstantKind::Local { def_id: local_did, body: default },
1228-
type_: clean_ty(ty, cx),
1229-
})),
1225+
hir::TraitItemKind::Const(ty, Some(default)) => {
1226+
ProvidedAssocConstItem(Box::new(Constant {
1227+
generics: enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx)),
1228+
kind: ConstantKind::Local { def_id: local_did, body: default },
1229+
type_: clean_ty(ty, cx),
1230+
}))
1231+
}
12301232
hir::TraitItemKind::Const(ty, None) => {
12311233
let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
1232-
TyAssocConstItem(generics, Box::new(clean_ty(ty, cx)))
1234+
RequiredAssocConstItem(generics, Box::new(clean_ty(ty, cx)))
12331235
}
12341236
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
12351237
let m = clean_function(cx, sig, trait_item.generics, FunctionArgs::Body(body));
12361238
MethodItem(m, None)
12371239
}
12381240
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(names)) => {
12391241
let m = clean_function(cx, sig, trait_item.generics, FunctionArgs::Names(names));
1240-
TyMethodItem(m)
1242+
RequiredMethodItem(m)
12411243
}
12421244
hir::TraitItemKind::Type(bounds, Some(default)) => {
12431245
let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
@@ -1257,7 +1259,7 @@ fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext
12571259
hir::TraitItemKind::Type(bounds, None) => {
12581260
let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
12591261
let bounds = bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect();
1260-
TyAssocTypeItem(generics, bounds)
1262+
RequiredAssocTypeItem(generics, bounds)
12611263
}
12621264
};
12631265
Item::from_def_id_and_parts(local_did, Some(trait_item.ident.name), inner, cx)
@@ -1271,7 +1273,7 @@ pub(crate) fn clean_impl_item<'tcx>(
12711273
let local_did = impl_.owner_id.to_def_id();
12721274
cx.with_param_env(local_did, |cx| {
12731275
let inner = match impl_.kind {
1274-
hir::ImplItemKind::Const(ty, expr) => AssocConstItem(Box::new(Constant {
1276+
hir::ImplItemKind::Const(ty, expr) => ImplAssocConstItem(Box::new(Constant {
12751277
generics: clean_generics(impl_.generics, cx),
12761278
kind: ConstantKind::Local { def_id: local_did, body: expr },
12771279
type_: clean_ty(ty, cx),
@@ -1320,18 +1322,23 @@ pub(crate) fn clean_middle_assoc_item(assoc_item: &ty::AssocItem, cx: &mut DocCo
13201322
);
13211323
simplify::move_bounds_to_generic_parameters(&mut generics);
13221324

1323-
let provided = match assoc_item.container {
1324-
ty::AssocItemContainer::Impl => true,
1325-
ty::AssocItemContainer::Trait => tcx.defaultness(assoc_item.def_id).has_value(),
1326-
};
1327-
if provided {
1328-
AssocConstItem(Box::new(Constant {
1325+
match assoc_item.container {
1326+
ty::AssocItemContainer::Impl => ImplAssocConstItem(Box::new(Constant {
13291327
generics,
13301328
kind: ConstantKind::Extern { def_id: assoc_item.def_id },
13311329
type_: ty,
1332-
}))
1333-
} else {
1334-
TyAssocConstItem(generics, Box::new(ty))
1330+
})),
1331+
ty::AssocItemContainer::Trait => {
1332+
if tcx.defaultness(assoc_item.def_id).has_value() {
1333+
ProvidedAssocConstItem(Box::new(Constant {
1334+
generics,
1335+
kind: ConstantKind::Extern { def_id: assoc_item.def_id },
1336+
type_: ty,
1337+
}))
1338+
} else {
1339+
RequiredAssocConstItem(generics, Box::new(ty))
1340+
}
1341+
}
13351342
}
13361343
}
13371344
ty::AssocKind::Fn => {
@@ -1369,7 +1376,7 @@ pub(crate) fn clean_middle_assoc_item(assoc_item: &ty::AssocItem, cx: &mut DocCo
13691376
};
13701377
MethodItem(item, defaultness)
13711378
} else {
1372-
TyMethodItem(item)
1379+
RequiredMethodItem(item)
13731380
}
13741381
}
13751382
ty::AssocKind::Type => {
@@ -1486,7 +1493,7 @@ pub(crate) fn clean_middle_assoc_item(assoc_item: &ty::AssocItem, cx: &mut DocCo
14861493
bounds,
14871494
)
14881495
} else {
1489-
TyAssocTypeItem(generics, bounds)
1496+
RequiredAssocTypeItem(generics, bounds)
14901497
}
14911498
} else {
14921499
AssocTypeItem(

Diff for: src/librustdoc/clean/types.rs

+28-18
Original file line numberDiff line numberDiff line change
@@ -545,14 +545,14 @@ impl Item {
545545
pub(crate) fn is_associated_type(&self) -> bool {
546546
matches!(self.kind, AssocTypeItem(..) | StrippedItem(box AssocTypeItem(..)))
547547
}
548-
pub(crate) fn is_ty_associated_type(&self) -> bool {
549-
matches!(self.kind, TyAssocTypeItem(..) | StrippedItem(box TyAssocTypeItem(..)))
548+
pub(crate) fn is_required_associated_type(&self) -> bool {
549+
matches!(self.kind, RequiredAssocTypeItem(..) | StrippedItem(box RequiredAssocTypeItem(..)))
550550
}
551551
pub(crate) fn is_associated_const(&self) -> bool {
552-
matches!(self.kind, AssocConstItem(..) | StrippedItem(box AssocConstItem(..)))
552+
matches!(self.kind, ProvidedAssocConstItem(..) | ImplAssocConstItem(..) | StrippedItem(box (ProvidedAssocConstItem(..) | ImplAssocConstItem(..))))
553553
}
554-
pub(crate) fn is_ty_associated_const(&self) -> bool {
555-
matches!(self.kind, TyAssocConstItem(..) | StrippedItem(box TyAssocConstItem(..)))
554+
pub(crate) fn is_required_associated_const(&self) -> bool {
555+
matches!(self.kind, RequiredAssocConstItem(..) | StrippedItem(box RequiredAssocConstItem(..)))
556556
}
557557
pub(crate) fn is_method(&self) -> bool {
558558
self.type_() == ItemType::Method
@@ -669,7 +669,9 @@ impl Item {
669669
asyncness: hir::IsAsync::NotAsync,
670670
}
671671
}
672-
ItemKind::FunctionItem(_) | ItemKind::MethodItem(_, _) | ItemKind::TyMethodItem(_) => {
672+
ItemKind::FunctionItem(_)
673+
| ItemKind::MethodItem(_, _)
674+
| ItemKind::RequiredMethodItem(_) => {
673675
let def_id = self.def_id().unwrap();
674676
build_fn_header(def_id, tcx, tcx.asyncness(def_id))
675677
}
@@ -699,8 +701,13 @@ impl Item {
699701
// Variants always inherit visibility
700702
VariantItem(..) | ImplItem(..) => return None,
701703
// Trait items inherit the trait's visibility
702-
AssocConstItem(..) | TyAssocConstItem(..) | AssocTypeItem(..) | TyAssocTypeItem(..)
703-
| TyMethodItem(..) | MethodItem(..) => {
704+
RequiredAssocConstItem(..)
705+
| ProvidedAssocConstItem(..)
706+
| ImplAssocConstItem(..)
707+
| AssocTypeItem(..)
708+
| RequiredAssocTypeItem(..)
709+
| RequiredMethodItem(..)
710+
| MethodItem(..) => {
704711
let assoc_item = tcx.associated_item(def_id);
705712
let is_trait_item = match assoc_item.container {
706713
ty::AssocItemContainer::Trait => true,
@@ -845,10 +852,10 @@ pub(crate) enum ItemKind {
845852
TraitAliasItem(TraitAlias),
846853
ImplItem(Box<Impl>),
847854
/// A required method in a trait declaration meaning it's only a function signature.
848-
TyMethodItem(Box<Function>),
855+
RequiredMethodItem(Box<Function>),
849856
/// A method in a trait impl or a provided method in a trait declaration.
850857
///
851-
/// Compared to [TyMethodItem], it also contains a method body.
858+
/// Compared to [RequiredMethodItem], it also contains a method body.
852859
MethodItem(Box<Function>, Option<hir::Defaultness>),
853860
StructFieldItem(Type),
854861
VariantItem(Variant),
@@ -862,14 +869,16 @@ pub(crate) enum ItemKind {
862869
ProcMacroItem(ProcMacro),
863870
PrimitiveItem(PrimitiveType),
864871
/// A required associated constant in a trait declaration.
865-
TyAssocConstItem(Generics, Box<Type>),
872+
RequiredAssocConstItem(Generics, Box<Type>),
866873
ConstantItem(Box<Constant>),
867-
/// An associated constant in a trait impl or a provided one in a trait declaration.
868-
AssocConstItem(Box<Constant>),
874+
/// An associated constant in a trait declaration with provided default value.
875+
ProvidedAssocConstItem(Box<Constant>),
876+
/// An associated constant in an inherent impl or trait impl.
877+
ImplAssocConstItem(Box<Constant>),
869878
/// A required associated type in a trait declaration.
870879
///
871880
/// The bounds may be non-empty if there is a `where` clause.
872-
TyAssocTypeItem(Generics, Vec<GenericBound>),
881+
RequiredAssocTypeItem(Generics, Vec<GenericBound>),
873882
/// An associated type in a trait impl or a provided one in a trait declaration.
874883
AssocTypeItem(Box<TypeAlias>, Vec<GenericBound>),
875884
/// An item that has been stripped by a rustdoc pass
@@ -900,7 +909,7 @@ impl ItemKind {
900909
| StaticItem(_)
901910
| ConstantItem(_)
902911
| TraitAliasItem(_)
903-
| TyMethodItem(_)
912+
| RequiredMethodItem(_)
904913
| MethodItem(_, _)
905914
| StructFieldItem(_)
906915
| ForeignFunctionItem(_, _)
@@ -909,9 +918,10 @@ impl ItemKind {
909918
| MacroItem(_)
910919
| ProcMacroItem(_)
911920
| PrimitiveItem(_)
912-
| TyAssocConstItem(..)
913-
| AssocConstItem(..)
914-
| TyAssocTypeItem(..)
921+
| RequiredAssocConstItem(..)
922+
| ProvidedAssocConstItem(..)
923+
| ImplAssocConstItem(..)
924+
| RequiredAssocTypeItem(..)
915925
| AssocTypeItem(..)
916926
| StrippedItem(_)
917927
| KeywordItem => [].iter(),

Diff for: src/librustdoc/fold.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub(crate) trait DocFolder: Sized {
8282
| StaticItem(_)
8383
| ConstantItem(..)
8484
| TraitAliasItem(_)
85-
| TyMethodItem(_)
85+
| RequiredMethodItem(_)
8686
| MethodItem(_, _)
8787
| StructFieldItem(_)
8888
| ForeignFunctionItem(..)
@@ -91,9 +91,10 @@ pub(crate) trait DocFolder: Sized {
9191
| MacroItem(_)
9292
| ProcMacroItem(_)
9393
| PrimitiveItem(_)
94-
| TyAssocConstItem(..)
95-
| AssocConstItem(..)
96-
| TyAssocTypeItem(..)
94+
| RequiredAssocConstItem(..)
95+
| ProvidedAssocConstItem(..)
96+
| ImplAssocConstItem(..)
97+
| RequiredAssocTypeItem(..)
9798
| AssocTypeItem(..)
9899
| KeywordItem => kind,
99100
}

Diff for: src/librustdoc/formats/cache.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,13 @@ impl DocFolder for CacheBuilder<'_, '_> {
334334
clean::ExternCrateItem { .. }
335335
| clean::ImportItem(..)
336336
| clean::ImplItem(..)
337-
| clean::TyMethodItem(..)
337+
| clean::RequiredMethodItem(..)
338338
| clean::MethodItem(..)
339339
| clean::StructFieldItem(..)
340-
| clean::TyAssocConstItem(..)
341-
| clean::AssocConstItem(..)
342-
| clean::TyAssocTypeItem(..)
340+
| clean::RequiredAssocConstItem(..)
341+
| clean::ProvidedAssocConstItem(..)
342+
| clean::ImplAssocConstItem(..)
343+
| clean::RequiredAssocTypeItem(..)
343344
| clean::AssocTypeItem(..)
344345
| clean::StrippedItem(..)
345346
| clean::KeywordItem => {
@@ -443,15 +444,17 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
443444
let item_def_id = item.item_id.as_def_id().unwrap();
444445
let (parent_did, parent_path) = match item.kind {
445446
clean::StrippedItem(..) => return,
446-
clean::AssocConstItem(..) | clean::AssocTypeItem(..)
447+
clean::ProvidedAssocConstItem(..)
448+
| clean::ImplAssocConstItem(..)
449+
| clean::AssocTypeItem(..)
447450
if cache.parent_stack.last().is_some_and(|parent| parent.is_trait_impl()) =>
448451
{
449452
// skip associated items in trait impls
450453
return;
451454
}
452-
clean::TyMethodItem(..)
453-
| clean::TyAssocConstItem(..)
454-
| clean::TyAssocTypeItem(..)
455+
clean::RequiredMethodItem(..)
456+
| clean::RequiredAssocConstItem(..)
457+
| clean::RequiredAssocTypeItem(..)
455458
| clean::StructFieldItem(..)
456459
| clean::VariantItem(..) => {
457460
// Don't index if containing module is stripped (i.e., private),
@@ -467,7 +470,10 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
467470
let parent_path = &cache.stack[..cache.stack.len() - 1];
468471
(Some(parent_did), parent_path)
469472
}
470-
clean::MethodItem(..) | clean::AssocConstItem(..) | clean::AssocTypeItem(..) => {
473+
clean::MethodItem(..)
474+
| clean::ProvidedAssocConstItem(..)
475+
| clean::ImplAssocConstItem(..)
476+
| clean::AssocTypeItem(..) => {
471477
let last = cache.parent_stack.last().expect("parent_stack is empty 2");
472478
let parent_did = match last {
473479
// impl Trait for &T { fn method(self); }

Diff for: src/librustdoc/formats/item_type.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,18 @@ impl<'a> From<&'a clean::Item> for ItemType {
8888
clean::ConstantItem(..) => ItemType::Constant,
8989
clean::TraitItem(..) => ItemType::Trait,
9090
clean::ImplItem(..) => ItemType::Impl,
91-
clean::TyMethodItem(..) => ItemType::TyMethod,
91+
clean::RequiredMethodItem(..) => ItemType::TyMethod,
9292
clean::MethodItem(..) => ItemType::Method,
9393
clean::StructFieldItem(..) => ItemType::StructField,
9494
clean::VariantItem(..) => ItemType::Variant,
9595
clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction
9696
clean::ForeignStaticItem(..) => ItemType::Static, // no ForeignStatic
9797
clean::MacroItem(..) => ItemType::Macro,
9898
clean::PrimitiveItem(..) => ItemType::Primitive,
99-
clean::TyAssocConstItem(..) | clean::AssocConstItem(..) => ItemType::AssocConst,
100-
clean::TyAssocTypeItem(..) | clean::AssocTypeItem(..) => ItemType::AssocType,
99+
clean::RequiredAssocConstItem(..)
100+
| clean::ProvidedAssocConstItem(..)
101+
| clean::ImplAssocConstItem(..) => ItemType::AssocConst,
102+
clean::RequiredAssocTypeItem(..) | clean::AssocTypeItem(..) => ItemType::AssocType,
101103
clean::ForeignTypeItem => ItemType::ForeignType,
102104
clean::KeywordItem => ItemType::Keyword,
103105
clean::TraitAliasItem(..) => ItemType::TraitAlias,

0 commit comments

Comments
 (0)