Skip to content

Commit 4b35c49

Browse files
Handle new kinds of macros at once
1 parent 8b05b08 commit 4b35c49

8 files changed

Lines changed: 120 additions & 13 deletions

File tree

src/librustdoc/clean/inline.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ pub(crate) fn try_inline(
165165
MacroKinds::BANG => ItemType::Macro,
166166
MacroKinds::ATTR => ItemType::ProcAttribute,
167167
MacroKinds::DERIVE => ItemType::ProcDerive,
168-
_ if kinds.contains(MacroKinds::BANG) => ItemType::Macro,
169-
_ => panic!("unsupported macro kind {kinds:?}"),
168+
// Then it means it's more than one type so we default to "macro".
169+
_ => ItemType::Macro,
170170
};
171171
record_extern_fqn(cx, did, type_kind);
172172
let first = try_inline_inner(cx, mac, did, name, import_def_id);
@@ -819,25 +819,25 @@ fn build_macro(
819819
}),
820820
None,
821821
),
822-
_ if macro_kinds.contains(MacroKinds::BANG) => {
823-
let kind = clean::MacroItem(
822+
_ => {
823+
let mut kinds = Vec::new();
824+
kinds.push(clean::MacroItem(
824825
clean::Macro {
825826
source: utils::display_macro_source(cx, name, &def),
826827
macro_rules: def.macro_rules,
827828
},
828829
macro_kinds,
829-
);
830-
let mut ret = vec![];
830+
));
831831
for kind in macro_kinds.iter().filter(|kind| *kind != MacroKinds::BANG) {
832832
match kind {
833-
MacroKinds::ATTR => ret.push(clean::AttrMacroItem),
834-
MacroKinds::DERIVE => ret.push(clean::DeriveMacroItem),
833+
MacroKinds::ATTR => kinds.push(clean::AttrMacroItem),
834+
MacroKinds::DERIVE => kinds.push(clean::DeriveMacroItem),
835835
_ => panic!("unsupported macro kind {kind:?}"),
836836
}
837837
}
838-
(kind, Some(ret))
838+
let kind = kinds.pop().expect("no supported macro kind found");
839+
(kind, Some(kinds))
839840
}
840-
_ => panic!("unsupported macro kind {macro_kinds:?}"),
841841
},
842842
LoadedMacro::ProcMacro(ext) => {
843843
// Proc macros can only have a single kind

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,7 +2831,7 @@ fn clean_maybe_renamed_item<'tcx>(
28312831
),
28322832
MacroKinds::ATTR => clean_proc_macro(item, &mut name, MacroKind::Attr, cx),
28332833
MacroKinds::DERIVE => clean_proc_macro(item, &mut name, MacroKind::Derive, cx),
2834-
_ if kinds.contains(MacroKinds::BANG) => {
2834+
_ => {
28352835
let kind = MacroItem(
28362836
Macro {
28372837
source: display_macro_source(cx, name, macro_def),
@@ -2867,7 +2867,6 @@ fn clean_maybe_renamed_item<'tcx>(
28672867
ret.push(mac);
28682868
return ret;
28692869
}
2870-
_ => panic!("unsupported macro kind {kinds:?}"),
28712870
},
28722871
// proc macros can have a name set by attributes
28732872
ItemKind::Fn { ref sig, generics, body: body_id, .. } => {

src/librustdoc/html/render/context.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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;
1314
use rustc_hir::def_id::{DefIdMap, LOCAL_CRATE};
1415
use rustc_middle::ty::TyCtxt;
1516
use rustc_session::Session;
@@ -904,7 +905,9 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
904905
let buf = self.render_item(item, false);
905906
// buf will be empty if the item is stripped and there is no redirect for it
906907
if !buf.is_empty() {
907-
if !self.info.render_redirect_pages {
908+
if !self.info.render_redirect_pages
909+
&& !matches!(item.kind, clean::ItemKind::MacroItem(_, kinds) if !kinds.contains(MacroKinds::BANG))
910+
{
908911
self.shared.all.borrow_mut().append(full_path(self, item), &item);
909912
}
910913

src/librustdoc/html/render/print_item.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
233233
for (index, item) in items.iter().filter(|i| !i.is_stripped()).enumerate() {
234234
// To prevent having new "bang macro attribute/derive" sections in the module,
235235
// we cheat by turning them into their "proc-macro equivalent".
236+
if let clean::ItemKind::MacroItem(_, kinds) = &item.kind
237+
&& !kinds.contains(MacroKinds::BANG)
238+
{
239+
// This is a placeholder, no need to list it in the module items.
240+
continue;
241+
}
236242
let type_ = match item.type_() {
237243
ItemType::BangMacroAttribute => ItemType::ProcAttribute,
238244
ItemType::BangMacroDerive => ItemType::ProcDerive,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Test for the `macro_attr` and `macro_derive` features.
2+
3+
#![feature(macro_attr)]
4+
#![feature(macro_derive)]
5+
6+
#![crate_name = "foo"]
7+
8+
//@ has 'foo/index.html'
9+
//@ count - '//*[@id="main-content"]/h2[@class="section-header"]' 3
10+
//@ has - '//*[@id="main-content"]/h2[@class="section-header"]' 'Attribute Macros'
11+
//@ has - '//*[@id="main-content"]/h2[@class="section-header"]' 'Derive Macros'
12+
//@ has - '//*[@id="main-content"]/h2[@class="section-header"]' 'Macros'
13+
//@ has - '//a[@href="macro.all.html"]' 'all'
14+
15+
//@ has 'foo/macro.all.html'
16+
//@ has - '//*[@class="macro-info"]' 'ⓘ This is an attribute/derive/function macro'
17+
18+
//@ has 'foo/all.html'
19+
//@ count - '//*[@id="main-content"]/h3' 3
20+
//@ has - '//*[@id="main-content"]/h3' 'Attribute Macros'
21+
//@ has - '//*[@id="main-content"]/h3' 'Derive Macros'
22+
//@ has - '//*[@id="main-content"]/h3' 'Macros'
23+
24+
#[macro_export]
25+
macro_rules! all {
26+
() => {};
27+
attr() () => {};
28+
derive() () => {};
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Test for the `macro_attr` and `macro_derive` features.
2+
3+
#![feature(macro_attr)]
4+
5+
#![crate_name = "foo"]
6+
7+
//@ has 'foo/index.html'
8+
//@ count - '//*[@id="main-content"]/h2[@class="section-header"]' 1
9+
//@ has - '//*[@id="main-content"]/h2[@class="section-header"]' 'Attribute Macros'
10+
//@ has - '//a[@href="attr.attr.html"]' 'attr'
11+
12+
//@ has 'foo/attr.attr.html'
13+
//@ has - '//*[@class="rust item-decl"]/code' '#[attr]'
14+
15+
//@ has 'foo/all.html'
16+
//@ count - '//*[@id="main-content"]/h3' 1
17+
//@ has - '//*[@id="main-content"]/h3' 'Attribute Macros'
18+
19+
#[macro_export]
20+
macro_rules! attr {
21+
attr() () => {};
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Test for the `macro_attr` and `macro_derive` features.
2+
3+
#![feature(macro_derive)]
4+
5+
#![crate_name = "foo"]
6+
7+
//@ has 'foo/index.html'
8+
//@ count - '//*[@id="main-content"]/h2[@class="section-header"]' 1
9+
//@ has - '//*[@id="main-content"]/h2[@class="section-header"]' 'Derive Macros'
10+
//@ has - '//a[@href="derive.derive.html"]' 'derive'
11+
12+
//@ has 'foo/derive.derive.html'
13+
//@ has - '//*[@class="rust item-decl"]/code' '#[derive(derive)]'
14+
15+
//@ has 'foo/all.html'
16+
//@ count - '//*[@id="main-content"]/h3' 1
17+
//@ has - '//*[@id="main-content"]/h3' 'Derive Macros'
18+
19+
#[macro_export]
20+
macro_rules! derive {
21+
derive() () => {};
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Test for the `macro_attr` and `macro_derive` features.
2+
3+
#![feature(macro_attr)]
4+
#![feature(macro_derive)]
5+
6+
#![crate_name = "foo"]
7+
8+
//@ has 'foo/index.html'
9+
//@ count - '//*[@id="main-content"]/h2[@class="section-header"]' 2
10+
//@ has - '//*[@id="main-content"]/h2[@class="section-header"]' 'Attribute Macros'
11+
//@ has - '//*[@id="main-content"]/h2[@class="section-header"]' 'Derive Macros'
12+
//@ has - '//a[@href="macro.no_bang.html"]' 'no_bang'
13+
14+
//@ has 'foo/macro.no_bang.html'
15+
//@ has - '//*[@class="macro-info"]' 'ⓘ This is an attribute/derive macro'
16+
17+
//@ has 'foo/all.html'
18+
//@ count - '//*[@id="main-content"]/h3' 2
19+
//@ has - '//*[@id="main-content"]/h3' 'Attribute Macros'
20+
//@ has - '//*[@id="main-content"]/h3' 'Derive Macros'
21+
22+
#[macro_export]
23+
macro_rules! no_bang {
24+
attr() () => {};
25+
derive() () => {};
26+
}

0 commit comments

Comments
 (0)