Skip to content

Commit 044885c

Browse files
committed
Split AssocConstItem into ProvidedAssocConstItem and ImplAssocConstItem
1 parent 57e1a47 commit 044885c

File tree

11 files changed

+62
-34
lines changed

11 files changed

+62
-34
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,11 +1222,13 @@ 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));
12321234
RequiredAssocConstItem(generics, Box::new(clean_ty(ty, 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-
RequiredAssocConstItem(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 => {

src/librustdoc/clean/types.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ impl Item {
551551
matches!(self.kind, TyAssocTypeItem(..) | StrippedItem(box TyAssocTypeItem(..)))
552552
}
553553
pub(crate) fn is_associated_const(&self) -> bool {
554-
matches!(self.kind, AssocConstItem(..) | StrippedItem(box AssocConstItem(..)))
554+
matches!(self.kind, ProvidedAssocConstItem(..) | ImplAssocConstItem(..) | StrippedItem(box (ProvidedAssocConstItem(..) | ImplAssocConstItem(..))))
555555
}
556556
pub(crate) fn is_required_associated_const(&self) -> bool {
557557
matches!(self.kind, RequiredAssocConstItem(..) | StrippedItem(box RequiredAssocConstItem(..)))
@@ -701,8 +701,9 @@ impl Item {
701701
// Variants always inherit visibility
702702
VariantItem(..) | ImplItem(..) => return None,
703703
// Trait items inherit the trait's visibility
704-
AssocConstItem(..)
705-
| RequiredAssocConstItem(..)
704+
RequiredAssocConstItem(..)
705+
| ProvidedAssocConstItem(..)
706+
| ImplAssocConstItem(..)
706707
| AssocTypeItem(..)
707708
| TyAssocTypeItem(..)
708709
| TyMethodItem(..)
@@ -870,8 +871,10 @@ pub(crate) enum ItemKind {
870871
/// A required associated constant in a trait declaration.
871872
RequiredAssocConstItem(Generics, Box<Type>),
872873
ConstantItem(Box<Constant>),
873-
/// An associated constant in a trait impl or a provided one in a trait declaration.
874-
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>),
875878
/// A required associated type in a trait declaration.
876879
///
877880
/// The bounds may be non-empty if there is a `where` clause.
@@ -916,7 +919,8 @@ impl ItemKind {
916919
| ProcMacroItem(_)
917920
| PrimitiveItem(_)
918921
| RequiredAssocConstItem(..)
919-
| AssocConstItem(..)
922+
| ProvidedAssocConstItem(..)
923+
| ImplAssocConstItem(..)
920924
| TyAssocTypeItem(..)
921925
| AssocTypeItem(..)
922926
| StrippedItem(_)

src/librustdoc/fold.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ pub(crate) trait DocFolder: Sized {
9292
| ProcMacroItem(_)
9393
| PrimitiveItem(_)
9494
| RequiredAssocConstItem(..)
95-
| AssocConstItem(..)
95+
| ProvidedAssocConstItem(..)
96+
| ImplAssocConstItem(..)
9697
| TyAssocTypeItem(..)
9798
| AssocTypeItem(..)
9899
| KeywordItem => kind,

src/librustdoc/formats/cache.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ impl DocFolder for CacheBuilder<'_, '_> {
338338
| clean::MethodItem(..)
339339
| clean::StructFieldItem(..)
340340
| clean::RequiredAssocConstItem(..)
341-
| clean::AssocConstItem(..)
341+
| clean::ProvidedAssocConstItem(..)
342+
| clean::ImplAssocConstItem(..)
342343
| clean::TyAssocTypeItem(..)
343344
| clean::AssocTypeItem(..)
344345
| clean::StrippedItem(..)
@@ -443,7 +444,9 @@ 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
@@ -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); }

src/librustdoc/formats/item_type.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ impl<'a> From<&'a clean::Item> for ItemType {
9696
clean::ForeignStaticItem(..) => ItemType::Static, // no ForeignStatic
9797
clean::MacroItem(..) => ItemType::Macro,
9898
clean::PrimitiveItem(..) => ItemType::Primitive,
99-
clean::RequiredAssocConstItem(..) | clean::AssocConstItem(..) => ItemType::AssocConst,
99+
clean::RequiredAssocConstItem(..)
100+
| clean::ProvidedAssocConstItem(..)
101+
| clean::ImplAssocConstItem(..) => ItemType::AssocConst,
100102
clean::TyAssocTypeItem(..) | clean::AssocTypeItem(..) => ItemType::AssocType,
101103
clean::ForeignTypeItem => ItemType::ForeignType,
102104
clean::KeywordItem => ItemType::Keyword,

src/librustdoc/html/render/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ fn render_assoc_item(
10911091
if parent == ItemType::Trait { 4 } else { 0 },
10921092
cx,
10931093
),
1094-
clean::AssocConstItem(ci) => assoc_const(
1094+
clean::ProvidedAssocConstItem(ci) | clean::ImplAssocConstItem(ci) => assoc_const(
10951095
w,
10961096
item,
10971097
&ci.generics,
@@ -1711,7 +1711,7 @@ fn render_impl(
17111711
);
17121712
w.write_str("</h4></section>");
17131713
}
1714-
clean::AssocConstItem(ci) => {
1714+
clean::ProvidedAssocConstItem(ci) | clean::ImplAssocConstItem(ci) => {
17151715
let source_id = format!("{item_type}.{name}");
17161716
let id = cx.derive_id(&source_id);
17171717
write!(w, "<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">");
@@ -1812,7 +1812,9 @@ fn render_impl(
18121812
clean::TyAssocTypeItem(..) | clean::AssocTypeItem(..) => {
18131813
assoc_types.push(trait_item)
18141814
}
1815-
clean::RequiredAssocConstItem(..) | clean::AssocConstItem(_) => {
1815+
clean::RequiredAssocConstItem(..)
1816+
| clean::ProvidedAssocConstItem(_)
1817+
| clean::ImplAssocConstItem(_) => {
18161818
// We render it directly since they're supposed to come first.
18171819
doc_impl_item(
18181820
&mut default_impl_items,

src/librustdoc/json/conversions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ fn from_clean_item(item: clean::Item, renderer: &JsonRenderer<'_>) -> ItemEnum {
343343
ItemEnum::AssocConst { type_: (*ty).into_json(renderer), value: None }
344344
}
345345
// FIXME(generic_const_items): Add support for generic associated consts.
346-
AssocConstItem(ci) => ItemEnum::AssocConst {
346+
ProvidedAssocConstItem(ci) | ImplAssocConstItem(ci) => ItemEnum::AssocConst {
347347
type_: ci.type_.into_json(renderer),
348348
value: Some(ci.kind.expr(renderer.tcx)),
349349
},

src/librustdoc/passes/check_doc_test_visibility.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -
7272
| clean::ForeignFunctionItem(..)
7373
| clean::ForeignStaticItem(..)
7474
| clean::ForeignTypeItem
75-
| clean::AssocConstItem(..)
7675
| clean::AssocTypeItem(..)
7776
| clean::RequiredAssocConstItem(..)
77+
| clean::ProvidedAssocConstItem(..)
78+
| clean::ImplAssocConstItem(..)
7879
| clean::TyAssocTypeItem(..)
7980
// check for trait impl
8081
| clean::ImplItem(box clean::Impl { trait_: Some(_), .. })

src/librustdoc/passes/propagate_stability.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ impl DocFolder for StabilityPropagator<'_, '_> {
7070
| ItemKind::TyMethodItem(..)
7171
| ItemKind::MethodItem(..)
7272
| ItemKind::RequiredAssocConstItem(..)
73-
| ItemKind::AssocConstItem(..)
73+
| ItemKind::ProvidedAssocConstItem(..)
74+
| ItemKind::ImplAssocConstItem(..)
7475
| ItemKind::TyAssocTypeItem(..)
7576
| ItemKind::AssocTypeItem(..)
7677
| ItemKind::PrimitiveItem(..)

src/librustdoc/passes/stripper.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ impl DocFolder for Stripper<'_, '_> {
7979
}
8080
}
8181

82-
clean::MethodItem(..) | clean::AssocConstItem(..) | clean::AssocTypeItem(..) => {
82+
clean::MethodItem(..)
83+
| clean::ProvidedAssocConstItem(..)
84+
| clean::ImplAssocConstItem(..)
85+
| clean::AssocTypeItem(..) => {
8386
let item_id = i.item_id;
8487
if item_id.is_local()
8588
&& !self.effective_visibilities.is_reachable(self.tcx, item_id.expect_def_id())

src/librustdoc/visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ pub(crate) trait DocVisitor<'a>: Sized {
4545
| ProcMacroItem(_)
4646
| PrimitiveItem(_)
4747
| RequiredAssocConstItem(..)
48-
| AssocConstItem(..)
48+
| ProvidedAssocConstItem(..)
49+
| ImplAssocConstItem(..)
4950
| TyAssocTypeItem(..)
5051
| AssocTypeItem(..)
5152
| KeywordItem => {}

0 commit comments

Comments
 (0)