Skip to content

Commit ce98d4e

Browse files
Simplify bang macro handling by merging all into one type
1 parent 2b9e7e7 commit ce98d4e

15 files changed

Lines changed: 123 additions & 199 deletions

File tree

src/librustdoc/clean/inline.rs

Lines changed: 18 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pub(crate) fn try_inline(
158158
})
159159
}
160160
Res::Def(DefKind::Macro(kinds), did) => {
161-
let (mac, others) = build_macro(cx.tcx, did, name, kinds);
161+
let mac = build_macro(cx.tcx, did, name, kinds);
162162

163163
let type_kind = match kinds {
164164
MacroKinds::BANG => ItemType::Macro,
@@ -168,15 +168,7 @@ pub(crate) fn try_inline(
168168
_ => ItemType::Macro,
169169
};
170170
record_extern_fqn(cx, did, type_kind);
171-
let first = try_inline_inner(cx, mac, did, name, import_def_id);
172-
if let Some(others) = others {
173-
for mac_kind in others {
174-
let mut mac = first.clone();
175-
mac.inner.kind = mac_kind;
176-
ret.push(mac);
177-
}
178-
}
179-
ret.push(first);
171+
ret.push(try_inline_inner(cx, mac, did, name, import_def_id));
180172
return Some(ret);
181173
}
182174
_ => return None,
@@ -793,52 +785,24 @@ fn build_macro(
793785
def_id: DefId,
794786
name: Symbol,
795787
macro_kinds: MacroKinds,
796-
) -> (clean::ItemKind, Option<Vec<clean::ItemKind>>) {
788+
) -> clean::ItemKind {
797789
match CStore::from_tcx(tcx).load_macro_untracked(tcx, def_id) {
798790
LoadedMacro::MacroDef { def, .. } => match macro_kinds {
799-
MacroKinds::BANG => (
800-
clean::MacroItem(
801-
clean::Macro {
802-
source: utils::display_macro_source(tcx, name, &def),
803-
macro_rules: def.macro_rules,
804-
},
805-
MacroKinds::BANG,
806-
),
807-
None,
808-
),
809-
MacroKinds::DERIVE => (
810-
clean::ProcMacroItem(clean::ProcMacro {
811-
kind: MacroKind::Derive,
812-
helpers: Vec::new(),
813-
}),
814-
None,
815-
),
816-
MacroKinds::ATTR => (
817-
clean::ProcMacroItem(clean::ProcMacro {
818-
kind: MacroKind::Attr,
819-
helpers: Vec::new(),
820-
}),
821-
None,
791+
MacroKinds::DERIVE => clean::ProcMacroItem(clean::ProcMacro {
792+
kind: MacroKind::Derive,
793+
helpers: Vec::new(),
794+
}),
795+
MacroKinds::ATTR => clean::ProcMacroItem(clean::ProcMacro {
796+
kind: MacroKind::Attr,
797+
helpers: Vec::new(),
798+
}),
799+
_ => clean::MacroItem(
800+
clean::Macro {
801+
source: utils::display_macro_source(tcx, name, &def),
802+
macro_rules: def.macro_rules,
803+
},
804+
macro_kinds,
822805
),
823-
_ => {
824-
let mut kinds = Vec::new();
825-
kinds.push(clean::MacroItem(
826-
clean::Macro {
827-
source: utils::display_macro_source(tcx, name, &def),
828-
macro_rules: def.macro_rules,
829-
},
830-
macro_kinds,
831-
));
832-
for kind in macro_kinds.iter().filter(|kind| *kind != MacroKinds::BANG) {
833-
match kind {
834-
MacroKinds::ATTR => kinds.push(clean::AttrMacroItem),
835-
MacroKinds::DERIVE => kinds.push(clean::DeriveMacroItem),
836-
_ => panic!("unsupported macro kind {kind:?}"),
837-
}
838-
}
839-
let kind = kinds.pop().expect("no supported macro kind found");
840-
(kind, Some(kinds))
841-
}
842806
},
843807
LoadedMacro::ProcMacro(ext) => {
844808
// Proc macros can only have a single kind
@@ -848,7 +812,7 @@ fn build_macro(
848812
MacroKinds::DERIVE => MacroKind::Derive,
849813
_ => unreachable!(),
850814
};
851-
(clean::ProcMacroItem(clean::ProcMacro { kind, helpers: ext.helper_attrs }), None)
815+
clean::ProcMacroItem(clean::ProcMacro { kind, helpers: ext.helper_attrs })
852816
}
853817
}
854818
}

src/librustdoc/clean/mod.rs

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2869,42 +2869,13 @@ fn clean_maybe_renamed_item<'tcx>(
28692869
),
28702870
MacroKinds::ATTR => clean_proc_macro(item, &mut name, MacroKind::Attr, cx.tcx),
28712871
MacroKinds::DERIVE => clean_proc_macro(item, &mut name, MacroKind::Derive, cx.tcx),
2872-
_ => {
2873-
let kind = MacroItem(
2874-
Macro {
2875-
source: display_macro_source(cx.tcx, name, macro_def),
2876-
macro_rules: macro_def.macro_rules,
2877-
},
2878-
kinds,
2879-
);
2880-
let mac = generate_item_with_correct_attrs(
2881-
cx,
2882-
kind,
2883-
item.owner_id.def_id.to_def_id(),
2884-
name,
2885-
import_ids,
2886-
renamed,
2887-
);
2888-
2889-
let mut ret = Vec::with_capacity(3);
2890-
for kind in kinds.iter().filter(|kind| *kind != MacroKinds::BANG) {
2891-
match kind {
2892-
MacroKinds::ATTR => {
2893-
let mut attr = mac.clone();
2894-
attr.inner.kind = AttrMacroItem;
2895-
ret.push(attr);
2896-
}
2897-
MacroKinds::DERIVE => {
2898-
let mut derive = mac.clone();
2899-
derive.inner.kind = DeriveMacroItem;
2900-
ret.push(derive);
2901-
}
2902-
_ => panic!("unsupported macro kind {kind:?}"),
2903-
}
2904-
}
2905-
ret.push(mac);
2906-
return ret;
2907-
}
2872+
_ => MacroItem(
2873+
Macro {
2874+
source: display_macro_source(cx.tcx, name, macro_def),
2875+
macro_rules: macro_def.macro_rules,
2876+
},
2877+
kinds,
2878+
),
29082879
},
29092880
// proc macros can have a name set by attributes
29102881
ItemKind::Fn { ref sig, generics, body: body_id, .. } => {

src/librustdoc/clean/types.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,29 @@ impl Item {
750750
ItemType::from(self)
751751
}
752752

753+
// FIXME: Return an iterator instead of a `ThinVec`.
754+
pub(crate) fn types(&self) -> ThinVec<ItemType> {
755+
if let ItemKind::MacroItem(_, macro_kinds) = self.kind {
756+
let mut types = ThinVec::with_capacity(3);
757+
for kind in macro_kinds.iter() {
758+
match kind {
759+
MacroKinds::ATTR => types.push(ItemType::BangMacroAttribute),
760+
MacroKinds::DERIVE => types.push(ItemType::BangMacroDerive),
761+
MacroKinds::BANG => types.push(ItemType::Macro),
762+
_ => panic!("unsupported macro kind {kind:?}"),
763+
}
764+
}
765+
return types;
766+
}
767+
let mut types = ThinVec::with_capacity(1);
768+
types.push(self.type_());
769+
types
770+
}
771+
772+
pub(crate) fn is_macro_rules(&self) -> bool {
773+
matches!(self.kind, ItemKind::MacroItem(..))
774+
}
775+
753776
pub(crate) fn defaultness(&self) -> Option<Defaultness> {
754777
match self.kind {
755778
ItemKind::MethodItem(_, defaultness) | ItemKind::RequiredMethodItem(_, defaultness) => {
@@ -761,14 +784,7 @@ impl Item {
761784

762785
/// Generates the HTML file name based on the item kind.
763786
pub(crate) fn html_filename(&self) -> String {
764-
let type_ = if self.is_macro_placeholder() { ItemType::Macro } else { self.type_() };
765-
format!("{type_}.{}.html", self.name.unwrap())
766-
}
767-
768-
/// If the current item is a "fake" macro (ie, `AttrMacroItem | ItemKind::DeriveMacroItem` which
769-
/// don't hold any data), it returns `true`.
770-
pub(crate) fn is_macro_placeholder(&self) -> bool {
771-
matches!(self.kind, ItemKind::AttrMacroItem | ItemKind::DeriveMacroItem)
787+
format!("{type_}.{name}.html", type_ = self.type_(), name = self.name.unwrap())
772788
}
773789

774790
/// Returns a `FnHeader` if `self` is a function item, otherwise returns `None`.
@@ -930,13 +946,9 @@ pub(crate) enum ItemKind {
930946
ForeignStaticItem(Static, hir::Safety),
931947
/// `type`s from an extern block
932948
ForeignTypeItem,
949+
/// A bang macro. it can be multiple things (macro, derive and attribute, potentially multiple
950+
/// at once). Don't forget to look into the `MacroKinds` values.
933951
MacroItem(Macro, MacroKinds),
934-
/// This is NOT an attribute proc-macro but a bang macro with support for being used as an
935-
/// attribute macro.
936-
AttrMacroItem,
937-
/// This is NOT an attribute proc-macro but a bang macro with support for being used as a
938-
/// derive macro.
939-
DeriveMacroItem,
940952
ProcMacroItem(ProcMacro),
941953
PrimitiveItem(PrimitiveType),
942954
/// A required associated constant in a trait declaration.
@@ -992,8 +1004,6 @@ impl ItemKind {
9921004
| ForeignStaticItem(_, _)
9931005
| ForeignTypeItem
9941006
| MacroItem(..)
995-
| AttrMacroItem
996-
| DeriveMacroItem
9971007
| ProcMacroItem(_)
9981008
| PrimitiveItem(_)
9991009
| RequiredAssocConstItem(..)

src/librustdoc/fold.rs

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

src/librustdoc/formats/cache.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,6 @@ impl DocFolder for CacheBuilder<'_, '_> {
384384
| clean::AssocTypeItem(..)
385385
| clean::StrippedItem(..)
386386
| clean::AttributeItem
387-
| clean::AttrMacroItem
388-
| clean::DeriveMacroItem
389387
| clean::KeywordItem => {
390388
// FIXME: Do these need handling?
391389
// The person writing this comment doesn't know.

src/librustdoc/formats/item_type.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ impl<'a> From<&'a clean::Item> for ItemType {
132132
clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction
133133
clean::ForeignStaticItem(..) => ItemType::Static, // no ForeignStatic
134134
clean::MacroItem(..) => ItemType::Macro,
135-
clean::AttrMacroItem => ItemType::BangMacroAttribute,
136-
clean::DeriveMacroItem => ItemType::BangMacroDerive,
137135
clean::PrimitiveItem(..) => ItemType::Primitive,
138136
clean::RequiredAssocConstItem(..)
139137
| clean::ProvidedAssocConstItem(..)
@@ -167,10 +165,9 @@ impl ItemType {
167165
DefKind::Trait => Self::Trait,
168166
DefKind::TyAlias => Self::TypeAlias,
169167
DefKind::TraitAlias => Self::TraitAlias,
170-
DefKind::Macro(MacroKinds::BANG) => ItemType::Macro,
171168
DefKind::Macro(MacroKinds::ATTR) => ItemType::ProcAttribute,
172169
DefKind::Macro(MacroKinds::DERIVE) => ItemType::ProcDerive,
173-
DefKind::Macro(_) => todo!("Handle macros with multiple kinds"),
170+
DefKind::Macro(_) => ItemType::Macro,
174171
DefKind::ForeignTy => Self::ForeignType,
175172
DefKind::Variant => Self::Variant,
176173
DefKind::Field => Self::StructField,

src/librustdoc/html/render/context.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use rustc_ast::join_path_syms;
1010
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
1111
use rustc_hir::Attribute;
1212
use rustc_hir::attrs::AttributeKind;
13-
use rustc_hir::def::MacroKinds;
1413
use rustc_hir::def_id::{DefIdMap, LOCAL_CRATE};
1514
use rustc_middle::ty::TyCtxt;
1615
use rustc_session::Session;
@@ -352,14 +351,13 @@ impl<'tcx> Context<'tcx> {
352351
Some(s) => s,
353352
};
354353

355-
let type_ = item.type_();
356-
357-
if inserted.entry(type_).or_default().insert(name) {
358-
let type_ = type_.to_string();
359-
let name = name.to_string();
360-
map.entry(type_)
361-
.or_default()
362-
.push(SidebarItem { name, is_macro_rules: item.is_macro_placeholder() });
354+
let is_macro_rules = item.is_macro_rules();
355+
for type_ in item.types() {
356+
if inserted.entry(type_).or_default().insert(name) {
357+
let type_ = type_.to_string();
358+
let name = name.to_string();
359+
map.entry(type_).or_default().push(SidebarItem { name, is_macro_rules });
360+
}
363361
}
364362
}
365363

@@ -840,7 +838,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
840838

841839
info!("Recursing into {}", self.dst.display());
842840

843-
if !item.is_stripped() && !item.is_macro_placeholder() {
841+
if !item.is_stripped() {
844842
let buf = self.render_item(item, true);
845843
// buf will be empty if the module is stripped and there is no redirect for it
846844
if !buf.is_empty() {
@@ -896,19 +894,10 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
896894
self.info.render_redirect_pages = item.is_stripped();
897895
}
898896

899-
if item.is_macro_placeholder() {
900-
if !self.info.render_redirect_pages {
901-
self.shared.all.borrow_mut().append(full_path(self, item), &item);
902-
}
903-
return Ok(());
904-
}
905-
906897
let buf = self.render_item(item, false);
907898
// buf will be empty if the item is stripped and there is no redirect for it
908899
if !buf.is_empty() {
909-
if !self.info.render_redirect_pages
910-
&& !matches!(item.kind, clean::ItemKind::MacroItem(_, kinds) if !kinds.contains(MacroKinds::BANG))
911-
{
900+
if !self.info.render_redirect_pages {
912901
self.shared.all.borrow_mut().append(full_path(self, item), &item);
913902
}
914903

src/librustdoc/html/render/mod.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -507,33 +507,43 @@ impl AllTypes {
507507
}
508508
}
509509

510+
fn add_item_entry(&mut self, item_type: ItemType, new_url: String, name: String) {
511+
match item_type {
512+
ItemType::Struct => self.structs.insert(ItemEntry::new(new_url, name)),
513+
ItemType::Enum => self.enums.insert(ItemEntry::new(new_url, name)),
514+
ItemType::Union => self.unions.insert(ItemEntry::new(new_url, name)),
515+
ItemType::Primitive => self.primitives.insert(ItemEntry::new(new_url, name)),
516+
ItemType::Trait => self.traits.insert(ItemEntry::new(new_url, name)),
517+
ItemType::Macro => self.macros.insert(ItemEntry::new(new_url, name)),
518+
ItemType::Function => self.functions.insert(ItemEntry::new(new_url, name)),
519+
ItemType::TypeAlias => self.type_aliases.insert(ItemEntry::new(new_url, name)),
520+
ItemType::Static => self.statics.insert(ItemEntry::new(new_url, name)),
521+
ItemType::Constant => self.constants.insert(ItemEntry::new(new_url, name)),
522+
ItemType::ProcAttribute | ItemType::BangMacroAttribute => {
523+
self.attribute_macros.insert(ItemEntry::new(new_url, name))
524+
}
525+
ItemType::ProcDerive | ItemType::BangMacroDerive => {
526+
self.derive_macros.insert(ItemEntry::new(new_url, name))
527+
}
528+
ItemType::TraitAlias => self.trait_aliases.insert(ItemEntry::new(new_url, name)),
529+
_ => true,
530+
};
531+
}
532+
510533
fn append(&mut self, item_name: String, item: &clean::Item) {
511534
let mut url: Vec<_> = item_name.split("::").skip(1).collect();
512535
if let Some(name) = url.pop() {
513536
let new_url = format!("{}/{}", url.join("/"), item.html_filename());
514537
url.push(name);
515-
let item_type = item.type_();
516538
let name = url.join("::");
517-
match item_type {
518-
ItemType::Struct => self.structs.insert(ItemEntry::new(new_url, name)),
519-
ItemType::Enum => self.enums.insert(ItemEntry::new(new_url, name)),
520-
ItemType::Union => self.unions.insert(ItemEntry::new(new_url, name)),
521-
ItemType::Primitive => self.primitives.insert(ItemEntry::new(new_url, name)),
522-
ItemType::Trait => self.traits.insert(ItemEntry::new(new_url, name)),
523-
ItemType::Macro => self.macros.insert(ItemEntry::new(new_url, name)),
524-
ItemType::Function => self.functions.insert(ItemEntry::new(new_url, name)),
525-
ItemType::TypeAlias => self.type_aliases.insert(ItemEntry::new(new_url, name)),
526-
ItemType::Static => self.statics.insert(ItemEntry::new(new_url, name)),
527-
ItemType::Constant => self.constants.insert(ItemEntry::new(new_url, name)),
528-
ItemType::ProcAttribute | ItemType::BangMacroAttribute => {
529-
self.attribute_macros.insert(ItemEntry::new(new_url, name))
530-
}
531-
ItemType::ProcDerive | ItemType::BangMacroDerive => {
532-
self.derive_macros.insert(ItemEntry::new(new_url, name))
539+
let mut types = item.types();
540+
if types.len() == 1 {
541+
self.add_item_entry(types.pop().unwrap(), new_url, name);
542+
} else {
543+
for type_ in types {
544+
self.add_item_entry(type_, new_url.clone(), name.clone());
533545
}
534-
ItemType::TraitAlias => self.trait_aliases.insert(ItemEntry::new(new_url, name)),
535-
_ => true,
536-
};
546+
}
537547
}
538548
}
539549

0 commit comments

Comments
 (0)