Skip to content

Commit 702d85b

Browse files
Simplify bang macro handling by merging all into one type
1 parent bdf7fd2 commit 702d85b

15 files changed

Lines changed: 131 additions & 204 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
@@ -2908,42 +2908,13 @@ fn clean_maybe_renamed_item<'tcx>(
29082908
),
29092909
MacroKinds::ATTR => clean_proc_macro(item, &mut name, MacroKind::Attr, cx.tcx),
29102910
MacroKinds::DERIVE => clean_proc_macro(item, &mut name, MacroKind::Derive, cx.tcx),
2911-
_ => {
2912-
let kind = MacroItem(
2913-
Macro {
2914-
source: display_macro_source(cx.tcx, name, macro_def),
2915-
macro_rules: macro_def.macro_rules,
2916-
},
2917-
kinds,
2918-
);
2919-
let mac = generate_item_with_correct_attrs(
2920-
cx,
2921-
kind,
2922-
item.owner_id.def_id.to_def_id(),
2923-
name,
2924-
import_ids,
2925-
renamed,
2926-
);
2927-
2928-
let mut ret = Vec::with_capacity(3);
2929-
for kind in kinds.iter().filter(|kind| *kind != MacroKinds::BANG) {
2930-
match kind {
2931-
MacroKinds::ATTR => {
2932-
let mut attr = mac.clone();
2933-
attr.inner.kind = AttrMacroItem;
2934-
ret.push(attr);
2935-
}
2936-
MacroKinds::DERIVE => {
2937-
let mut derive = mac.clone();
2938-
derive.inner.kind = DeriveMacroItem;
2939-
ret.push(derive);
2940-
}
2941-
_ => panic!("unsupported macro kind {kind:?}"),
2942-
}
2943-
}
2944-
ret.push(mac);
2945-
return ret;
2946-
}
2911+
_ => MacroItem(
2912+
Macro {
2913+
source: display_macro_source(cx.tcx, name, macro_def),
2914+
macro_rules: macro_def.macro_rules,
2915+
},
2916+
kinds,
2917+
),
29472918
},
29482919
// proc macros can have a name set by attributes
29492920
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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::mem;
22

33
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
44
use rustc_hir::StabilityLevel;
5-
use rustc_hir::def::MacroKinds;
65
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet};
76
use rustc_metadata::creader::CStore;
87
use rustc_middle::ty::{self, TyCtxt};
@@ -384,8 +383,6 @@ impl DocFolder for CacheBuilder<'_, '_> {
384383
| clean::AssocTypeItem(..)
385384
| clean::StrippedItem(..)
386385
| clean::AttributeItem
387-
| clean::AttrMacroItem
388-
| clean::DeriveMacroItem
389386
| clean::KeywordItem => {
390387
// FIXME: Do these need handling?
391388
// The person writing this comment doesn't know.
@@ -489,7 +486,6 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
489486
// Item has a name, so it must also have a DefId (can't be an impl, let alone a blanket or auto impl).
490487
let item_def_id = item.item_id.as_def_id().unwrap();
491488
let (parent_did, parent_path) = match item.kind {
492-
clean::MacroItem(_, kinds) if !kinds.contains(MacroKinds::BANG) => return,
493489
clean::StrippedItem(..) => return,
494490
clean::ProvidedAssocConstItem(..)
495491
| clean::ImplAssocConstItem(..)
@@ -601,8 +597,9 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
601597
let aliases = item.attrs.get_doc_aliases();
602598
let is_deprecated = item.is_deprecated(tcx);
603599
let is_unstable = item.is_unstable();
600+
let mut types = item.types();
604601
let index_item = IndexItem {
605-
ty: item.type_(),
602+
ty: types.pop().unwrap(),
606603
defid: Some(defid),
607604
name,
608605
module_path: parent_path.to_vec(),
@@ -618,7 +615,11 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
618615
is_deprecated,
619616
is_unstable,
620617
};
621-
618+
for type_ in types {
619+
let mut index_item_copy = index_item.clone();
620+
index_item_copy.ty = type_;
621+
cache.search_index.push(index_item_copy);
622+
}
622623
cache.search_index.push(index_item);
623624
}
624625

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

0 commit comments

Comments
 (0)