Skip to content

Commit 3e05767

Browse files
Handle new kinds of macros at once
1 parent 6dce601 commit 3e05767

10 files changed

Lines changed: 158 additions & 15 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
@@ -2833,7 +2833,7 @@ fn clean_maybe_renamed_item<'tcx>(
28332833
),
28342834
MacroKinds::ATTR => clean_proc_macro(item, &mut name, MacroKind::Attr, cx),
28352835
MacroKinds::DERIVE => clean_proc_macro(item, &mut name, MacroKind::Derive, cx),
2836-
_ if kinds.contains(MacroKinds::BANG) => {
2836+
_ => {
28372837
let kind = MacroItem(
28382838
Macro {
28392839
source: display_macro_source(cx, name, macro_def),
@@ -2869,7 +2869,6 @@ fn clean_maybe_renamed_item<'tcx>(
28692869
ret.push(mac);
28702870
return ret;
28712871
}
2872-
_ => panic!("unsupported macro kind {kinds:?}"),
28732872
},
28742873
// proc macros can have a name set by attributes
28752874
ItemKind::Fn { ref sig, generics, body: body_id, .. } => {

src/librustdoc/formats/cache.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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;
56
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet};
67
use rustc_metadata::creader::CStore;
78
use rustc_middle::ty::{self, TyCtxt};
@@ -486,6 +487,7 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
486487
// Item has a name, so it must also have a DefId (can't be an impl, let alone a blanket or auto impl).
487488
let item_def_id = item.item_id.as_def_id().unwrap();
488489
let (parent_did, parent_path) = match item.kind {
490+
clean::MacroItem(_, kinds) if !kinds.contains(MacroKinds::BANG) => return,
489491
clean::StrippedItem(..) => return,
490492
clean::ProvidedAssocConstItem(..)
491493
| clean::ImplAssocConstItem(..)

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+
}

tests/rustdoc-js/macro-kinds.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,42 @@ const EXPECTED = [
44
{
55
'query': 'macro:macro',
66
'others': [
7-
{ 'path': 'macro_kinds', 'name': 'macro1', 'href': '../macro_kinds/macro.macro1.html' },
8-
{ 'path': 'macro_kinds', 'name': 'macro3', 'href': '../macro_kinds/macro.macro3.html' },
7+
{
8+
'path': 'macro_kinds',
9+
'name': 'macro1',
10+
'href': '../macro_kinds/macro.macro1.html',
11+
'ty': 16,
12+
},
13+
{
14+
'path': 'macro_kinds',
15+
'name': 'macro1',
16+
'href': '../macro_kinds/macro.macro1.html',
17+
'ty': 23,
18+
},
19+
{
20+
'path': 'macro_kinds',
21+
'name': 'macro1',
22+
'href': '../macro_kinds/macro.macro1.html',
23+
'ty': 24,
24+
},
25+
{
26+
'path': 'macro_kinds',
27+
'name': 'macro2',
28+
'href': '../macro_kinds/attr.macro2.html',
29+
'ty': 23,
30+
},
31+
{
32+
'path': 'macro_kinds',
33+
'name': 'macro4',
34+
'href': '../macro_kinds/derive.macro4.html',
35+
'ty': 24,
36+
},
37+
{
38+
'path': 'macro_kinds',
39+
'name': 'macro3',
40+
'href': '../macro_kinds/macro.macro3.html',
41+
'ty': 16,
42+
},
943
],
1044
},
1145
{

0 commit comments

Comments
 (0)