Skip to content

Commit 76a8322

Browse files
Use ItemKind placeholders for alternative macro kinds
1 parent add2aaf commit 76a8322

File tree

14 files changed

+210
-153
lines changed

14 files changed

+210
-153
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ pub(crate) fn try_inline(
4646
attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
4747
visited: &mut DefIdSet,
4848
) -> Option<Vec<clean::Item>> {
49+
fn try_inline_inner(
50+
cx: &mut DocContext<'_>,
51+
kind: clean::ItemKind,
52+
did: DefId,
53+
name: Symbol,
54+
import_def_id: Option<LocalDefId>,
55+
) -> clean::Item {
56+
cx.inlined.insert(did.into());
57+
let mut item = crate::clean::generate_item_with_correct_attrs(
58+
cx,
59+
kind,
60+
did,
61+
name,
62+
import_def_id.as_slice(),
63+
None,
64+
);
65+
// The visibility needs to reflect the one from the reexport and not from the "source" DefId.
66+
item.inner.inline_stmt_id = import_def_id;
67+
item
68+
}
69+
4970
let did = res.opt_def_id()?;
5071
if did.is_local() {
5172
return None;
@@ -138,7 +159,7 @@ pub(crate) fn try_inline(
138159
})
139160
}
140161
Res::Def(DefKind::Macro(kinds), did) => {
141-
let mac = build_macro(cx, did, name, kinds);
162+
let (mac, others) = build_macro(cx, did, name, kinds);
142163

143164
let type_kind = match kinds {
144165
MacroKinds::BANG => ItemType::Macro,
@@ -148,23 +169,21 @@ pub(crate) fn try_inline(
148169
_ => panic!("unsupported macro kind {kinds:?}"),
149170
};
150171
record_extern_fqn(cx, did, type_kind);
151-
mac
172+
let first = try_inline_inner(cx, mac, did, name, import_def_id);
173+
if let Some(others) = others {
174+
for mac_kind in others {
175+
let mut mac = first.clone();
176+
mac.inner.kind = mac_kind;
177+
ret.push(mac);
178+
}
179+
}
180+
ret.push(first);
181+
return Some(ret);
152182
}
153183
_ => return None,
154184
};
155185

156-
cx.inlined.insert(did.into());
157-
let mut item = crate::clean::generate_item_with_correct_attrs(
158-
cx,
159-
kind,
160-
did,
161-
name,
162-
import_def_id.as_slice(),
163-
None,
164-
);
165-
// The visibility needs to reflect the one from the reexport and not from the "source" DefId.
166-
item.inner.inline_stmt_id = import_def_id;
167-
ret.push(item);
186+
ret.push(try_inline_inner(cx, kind, did, name, import_def_id));
168187
Some(ret)
169188
}
170189

@@ -761,31 +780,51 @@ fn build_macro(
761780
def_id: DefId,
762781
name: Symbol,
763782
macro_kinds: MacroKinds,
764-
) -> clean::ItemKind {
783+
) -> (clean::ItemKind, Option<Vec<clean::ItemKind>>) {
765784
match CStore::from_tcx(cx.tcx).load_macro_untracked(def_id, cx.tcx) {
766785
LoadedMacro::MacroDef { def, .. } => match macro_kinds {
767-
MacroKinds::BANG => clean::MacroItem(
768-
clean::Macro {
769-
source: utils::display_macro_source(cx, name, &def),
770-
macro_rules: def.macro_rules,
771-
},
786+
MacroKinds::BANG => (
787+
clean::MacroItem(
788+
clean::Macro {
789+
source: utils::display_macro_source(cx, name, &def),
790+
macro_rules: def.macro_rules,
791+
},
792+
None,
793+
),
794+
None,
795+
),
796+
MacroKinds::DERIVE => (
797+
clean::ProcMacroItem(clean::ProcMacro {
798+
kind: MacroKind::Derive,
799+
helpers: Vec::new(),
800+
}),
772801
None,
773802
),
774-
MacroKinds::DERIVE => clean::ProcMacroItem(clean::ProcMacro {
775-
kind: MacroKind::Derive,
776-
helpers: Vec::new(),
777-
}),
778-
MacroKinds::ATTR => clean::ProcMacroItem(clean::ProcMacro {
779-
kind: MacroKind::Attr,
780-
helpers: Vec::new(),
781-
}),
782-
_ if macro_kinds == (MacroKinds::BANG | MacroKinds::ATTR) => clean::MacroItem(
783-
clean::Macro {
784-
source: utils::display_macro_source(cx, name, &def),
785-
macro_rules: def.macro_rules,
786-
},
787-
Some(macro_kinds),
803+
MacroKinds::ATTR => (
804+
clean::ProcMacroItem(clean::ProcMacro {
805+
kind: MacroKind::Attr,
806+
helpers: Vec::new(),
807+
}),
808+
None,
788809
),
810+
_ if macro_kinds.contains(MacroKinds::BANG) => {
811+
let kind = clean::MacroItem(
812+
clean::Macro {
813+
source: utils::display_macro_source(cx, name, &def),
814+
macro_rules: def.macro_rules,
815+
},
816+
Some(macro_kinds),
817+
);
818+
let mut ret = vec![];
819+
for kind in macro_kinds.iter().filter(|kind| *kind != MacroKinds::BANG) {
820+
match kind {
821+
MacroKinds::ATTR => ret.push(clean::AttrMacroItem),
822+
MacroKinds::DERIVE => ret.push(clean::DeriveMacroItem),
823+
_ => panic!("unsupported macro kind {kind:?}"),
824+
}
825+
}
826+
(kind, Some(ret))
827+
}
789828
_ => panic!("unsupported macro kind {macro_kinds:?}"),
790829
},
791830
LoadedMacro::ProcMacro(ext) => {
@@ -796,7 +835,7 @@ fn build_macro(
796835
MacroKinds::DERIVE => MacroKind::Derive,
797836
_ => unreachable!(),
798837
};
799-
clean::ProcMacroItem(clean::ProcMacro { kind, helpers: ext.helper_attrs })
838+
(clean::ProcMacroItem(clean::ProcMacro { kind, helpers: ext.helper_attrs }), None)
800839
}
801840
}
802841
}

src/librustdoc/clean/mod.rs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,13 +2851,43 @@ fn clean_maybe_renamed_item<'tcx>(
28512851
),
28522852
MacroKinds::ATTR => clean_proc_macro(item, &mut name, MacroKind::Attr, cx),
28532853
MacroKinds::DERIVE => clean_proc_macro(item, &mut name, MacroKind::Derive, cx),
2854-
_ => MacroItem(
2855-
Macro {
2856-
source: display_macro_source(cx, name, macro_def),
2857-
macro_rules: macro_def.macro_rules,
2858-
},
2859-
Some(kinds),
2860-
),
2854+
_ if kinds.contains(MacroKinds::BANG) => {
2855+
let kind = MacroItem(
2856+
Macro {
2857+
source: display_macro_source(cx, name, macro_def),
2858+
macro_rules: macro_def.macro_rules,
2859+
},
2860+
Some(kinds),
2861+
);
2862+
let mac = generate_item_with_correct_attrs(
2863+
cx,
2864+
kind,
2865+
item.owner_id.def_id.to_def_id(),
2866+
name,
2867+
import_ids,
2868+
renamed,
2869+
);
2870+
2871+
let mut ret = Vec::with_capacity(3);
2872+
for kind in kinds.iter().filter(|kind| *kind != MacroKinds::BANG) {
2873+
match kind {
2874+
MacroKinds::ATTR => {
2875+
let mut attr = mac.clone();
2876+
attr.inner.kind = AttrMacroItem;
2877+
ret.push(attr);
2878+
}
2879+
MacroKinds::DERIVE => {
2880+
let mut derive = mac.clone();
2881+
derive.inner.kind = DeriveMacroItem;
2882+
ret.push(derive);
2883+
}
2884+
_ => panic!("unsupported macro kind {kind:?}"),
2885+
}
2886+
}
2887+
ret.push(mac);
2888+
return ret;
2889+
}
2890+
_ => panic!("unsupported macro kind {kinds:?}"),
28612891
},
28622892
// proc macros can have a name set by attributes
28632893
ItemKind::Fn { ref sig, generics, body: body_id, .. } => {

src/librustdoc/clean/types.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -664,29 +664,23 @@ impl Item {
664664
find_attr!(&self.attrs.other_attrs, AttributeKind::NonExhaustive(..))
665665
}
666666

667-
pub(crate) fn bang_macro_types(&self) -> Option<Vec<ItemType>> {
668-
match self.kind {
669-
ItemKind::MacroItem(_, None) => Some(vec![ItemType::Macro]),
670-
ItemKind::MacroItem(_, Some(kinds)) => Some(
671-
kinds
672-
.iter()
673-
.map(|kind| match kind {
674-
MacroKinds::BANG => ItemType::Macro,
675-
MacroKinds::ATTR => ItemType::ProcAttribute,
676-
MacroKinds::DERIVE => ItemType::ProcDerive,
677-
_ => panic!("unexpected macro kind {kind:?}"),
678-
})
679-
.collect::<Vec<_>>(),
680-
),
681-
_ => None,
682-
}
683-
}
684-
685667
/// Returns a documentation-level item type from the item.
686668
pub(crate) fn type_(&self) -> ItemType {
687669
ItemType::from(self)
688670
}
689671

672+
/// Generates the HTML file name based on the item kind.
673+
pub(crate) fn html_filename(&self) -> String {
674+
let type_ = if self.is_macro_placeholder() { ItemType::Macro } else { self.type_() };
675+
format!("{type_}.{}.html", self.name.unwrap())
676+
}
677+
678+
/// If the current item is a "fake" macro (ie, `AttrMacroItem | ItemKind::DeriveMacroItem` which
679+
/// don't hold any data), it returns `true`.
680+
pub(crate) fn is_macro_placeholder(&self) -> bool {
681+
matches!(self.kind, ItemKind::AttrMacroItem | ItemKind::DeriveMacroItem)
682+
}
683+
690684
pub(crate) fn is_default(&self) -> bool {
691685
match self.kind {
692686
ItemKind::MethodItem(_, Some(defaultness)) => {
@@ -853,6 +847,8 @@ pub(crate) enum ItemKind {
853847
/// `type`s from an extern block
854848
ForeignTypeItem,
855849
MacroItem(Macro, Option<MacroKinds>),
850+
AttrMacroItem,
851+
DeriveMacroItem,
856852
ProcMacroItem(ProcMacro),
857853
PrimitiveItem(PrimitiveType),
858854
/// A required associated constant in a trait declaration.
@@ -908,6 +904,8 @@ impl ItemKind {
908904
| ForeignStaticItem(_, _)
909905
| ForeignTypeItem
910906
| MacroItem(..)
907+
| AttrMacroItem
908+
| DeriveMacroItem
911909
| ProcMacroItem(_)
912910
| PrimitiveItem(_)
913911
| RequiredAssocConstItem(..)

src/librustdoc/fold.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ pub(crate) trait DocFolder: Sized {
8989
| ForeignStaticItem(..)
9090
| ForeignTypeItem
9191
| MacroItem(..)
92+
| AttrMacroItem
93+
| DeriveMacroItem
9294
| ProcMacroItem(_)
9395
| PrimitiveItem(_)
9496
| RequiredAssocConstItem(..)

src/librustdoc/formats/cache.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,10 @@ impl DocFolder for CacheBuilder<'_, '_> {
378378
| clean::RequiredAssocTypeItem(..)
379379
| clean::AssocTypeItem(..)
380380
| clean::StrippedItem(..)
381-
| clean::KeywordItem
382-
| clean::AttributeItem => {
381+
| clean::AttributeItem
382+
| clean::AttrMacroItem
383+
| clean::DeriveMacroItem
384+
| clean::KeywordItem => {
383385
// FIXME: Do these need handling?
384386
// The person writing this comment doesn't know.
385387
// So would rather leave them to an expert,

src/librustdoc/formats/item_type.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ impl<'a> From<&'a clean::Item> for ItemType {
128128
clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction
129129
clean::ForeignStaticItem(..) => ItemType::Static, // no ForeignStatic
130130
clean::MacroItem(..) => ItemType::Macro,
131+
// Is this a good idea?
132+
clean::AttrMacroItem => ItemType::ProcAttribute,
133+
// Is this a good idea?
134+
clean::DeriveMacroItem => ItemType::ProcDerive,
131135
clean::PrimitiveItem(..) => ItemType::Primitive,
132136
clean::RequiredAssocConstItem(..)
133137
| clean::ProvidedAssocConstItem(..)

0 commit comments

Comments
 (0)