Skip to content

Commit 1f7bce0

Browse files
Teach tools that macros are now HIR items
1 parent 0299ed8 commit 1f7bce0

File tree

7 files changed

+72
-92
lines changed

7 files changed

+72
-92
lines changed

src/librustdoc/clean/mod.rs

+4-19
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ impl Clean<Item> for doctree::Module<'_> {
9191
items.extend(self.foreigns.iter().map(|x| x.clean(cx)));
9292
items.extend(self.mods.iter().map(|x| x.clean(cx)));
9393
items.extend(self.items.iter().map(|x| x.clean(cx)).flatten());
94-
items.extend(self.macros.iter().map(|x| x.clean(cx)));
9594

9695
// determine if we should display the inner contents or
9796
// the outer `mod` item for the source code.
@@ -1861,6 +1860,10 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
18611860
ItemKind::Fn(ref sig, ref generics, body_id) => {
18621861
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
18631862
}
1863+
ItemKind::Macro(ref macro_def) => MacroItem(Macro {
1864+
source: display_macro_source(cx, name, &macro_def, def_id, &item.vis),
1865+
imported_from: None,
1866+
}),
18641867
ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref item_ids) => {
18651868
let items = item_ids
18661869
.iter()
@@ -2138,24 +2141,6 @@ impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Symbol>) {
21382141
}
21392142
}
21402143

2141-
impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
2142-
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
2143-
let (item, renamed) = self;
2144-
let name = renamed.unwrap_or(item.ident.name);
2145-
let def_id = item.def_id.to_def_id();
2146-
2147-
Item::from_hir_id_and_parts(
2148-
item.hir_id(),
2149-
Some(name),
2150-
MacroItem(Macro {
2151-
source: display_macro_source(cx, name, &item.ast, def_id, &item.vis),
2152-
imported_from: None,
2153-
}),
2154-
cx,
2155-
)
2156-
}
2157-
}
2158-
21592144
impl Clean<TypeBinding> for hir::TypeBinding<'_> {
21602145
fn clean(&self, cx: &mut DocContext<'_>) -> TypeBinding {
21612146
TypeBinding { name: self.ident.name, kind: self.kind.clean(cx) }

src/librustdoc/doctest.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -1171,10 +1171,21 @@ impl<'a, 'hir, 'tcx> intravisit::Visitor<'hir> for HirCollector<'a, 'hir, 'tcx>
11711171
}
11721172

11731173
fn visit_item(&mut self, item: &'hir hir::Item<'_>) {
1174-
let name = if let hir::ItemKind::Impl(impl_) = &item.kind {
1175-
rustc_hir_pretty::id_to_string(&self.map, impl_.self_ty.hir_id)
1176-
} else {
1177-
item.ident.to_string()
1174+
let name = match &item.kind {
1175+
hir::ItemKind::Macro(ref macro_def) => {
1176+
// FIXME(#88038): Non exported macros have historically not been tested,
1177+
// but we really ought to start testing them.
1178+
let def_id = item.def_id.to_def_id();
1179+
if macro_def.macro_rules && !self.tcx.has_attr(def_id, sym::macro_export) {
1180+
intravisit::walk_item(self, item);
1181+
return;
1182+
}
1183+
item.ident.to_string()
1184+
}
1185+
hir::ItemKind::Impl(impl_) => {
1186+
rustc_hir_pretty::id_to_string(&self.map, impl_.self_ty.hir_id)
1187+
}
1188+
_ => item.ident.to_string(),
11781189
};
11791190

11801191
self.visit_testable(name, item.hir_id(), item.span, |this| {
@@ -1216,15 +1227,6 @@ impl<'a, 'hir, 'tcx> intravisit::Visitor<'hir> for HirCollector<'a, 'hir, 'tcx>
12161227
intravisit::walk_field_def(this, f);
12171228
});
12181229
}
1219-
1220-
fn visit_macro_def(&mut self, macro_def: &'hir hir::MacroDef<'_>) {
1221-
self.visit_testable(
1222-
macro_def.ident.to_string(),
1223-
macro_def.hir_id(),
1224-
macro_def.span,
1225-
|_| (),
1226-
);
1227-
}
12281230
}
12291231

12301232
#[cfg(test)]

src/librustdoc/doctree.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_span::{self, Span, Symbol};
55

66
use rustc_hir as hir;
77

8+
#[derive(Debug)]
89
crate struct Module<'hir> {
910
crate name: Symbol,
1011
crate where_inner: Span,
@@ -13,20 +14,11 @@ crate struct Module<'hir> {
1314
// (item, renamed)
1415
crate items: Vec<(&'hir hir::Item<'hir>, Option<Symbol>)>,
1516
crate foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Symbol>)>,
16-
crate macros: Vec<(&'hir hir::MacroDef<'hir>, Option<Symbol>)>,
1717
}
1818

1919
impl Module<'hir> {
2020
crate fn new(name: Symbol, id: hir::HirId, where_inner: Span) -> Module<'hir> {
21-
Module {
22-
name,
23-
id,
24-
where_inner,
25-
mods: Vec::new(),
26-
items: Vec::new(),
27-
foreigns: Vec::new(),
28-
macros: Vec::new(),
29-
}
21+
Module { name, id, where_inner, mods: Vec::new(), items: Vec::new(), foreigns: Vec::new() }
3022
}
3123

3224
crate fn where_outer(&self, tcx: TyCtxt<'_>) -> Span {

src/librustdoc/visit_ast.rs

+42-49
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir::Node;
99
use rustc_middle::middle::privacy::AccessLevel;
1010
use rustc_middle::ty::TyCtxt;
1111
use rustc_span;
12-
use rustc_span::def_id::LOCAL_CRATE;
12+
use rustc_span::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
1313
use rustc_span::source_map::Spanned;
1414
use rustc_span::symbol::{kw, sym, Symbol};
1515

@@ -79,49 +79,23 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
7979
&krate.module(),
8080
self.cx.tcx.crate_name(LOCAL_CRATE),
8181
);
82-
// Attach the crate's exported macros to the top-level module.
83-
// In the case of macros 2.0 (`pub macro`), and for built-in `derive`s or attributes as
84-
// well (_e.g._, `Copy`), these are wrongly bundled in there too, so we need to fix that by
85-
// moving them back to their correct locations.
86-
'exported_macros: for def in krate.exported_macros() {
87-
// The `def` of a macro in `exported_macros` should correspond to either:
88-
// - a `#[macro_export] macro_rules!` macro,
89-
// - a built-in `derive` (or attribute) macro such as the ones in `::core`,
90-
// - a `pub macro`.
91-
// Only the last two need to be fixed, thus:
92-
if def.ast.macro_rules {
93-
top_level_module.macros.push((def, None));
94-
continue 'exported_macros;
95-
}
96-
let tcx = self.cx.tcx;
97-
// Note: this is not the same as `.parent_module()`. Indeed, the latter looks
98-
// for the closest module _ancestor_, which is not necessarily a direct parent
99-
// (since a direct parent isn't necessarily a module, c.f. #77828).
100-
let macro_parent_def_id = {
101-
use rustc_middle::ty::DefIdTree;
102-
tcx.parent(def.def_id.to_def_id()).unwrap()
103-
};
104-
let macro_parent_path = tcx.def_path(macro_parent_def_id);
105-
// HACK: rustdoc has no way to lookup `doctree::Module`s by their HirId. Instead,
106-
// lookup the module by its name, by looking at each path segment one at a time.
107-
let mut cur_mod = &mut top_level_module;
108-
for path_segment in macro_parent_path.data {
109-
// Path segments may refer to a module (in which case they belong to the type
110-
// namespace), which is _necessary_ for the macro to be accessible outside it
111-
// (no "associated macros" as of yet). Else we bail with an outer `continue`.
112-
let path_segment_ty_ns = match path_segment.data {
113-
rustc_hir::definitions::DefPathData::TypeNs(symbol) => symbol,
114-
_ => continue 'exported_macros,
115-
};
116-
// Descend into the child module that matches this path segment (if any).
117-
match cur_mod.mods.iter_mut().find(|child| child.name == path_segment_ty_ns) {
118-
Some(child_mod) => cur_mod = &mut *child_mod,
119-
None => continue 'exported_macros,
82+
83+
// `#[macro_export] macro_rules!` items are reexported at the top level of the
84+
// crate, regardless of where they're defined. We want to document the
85+
// top level rexport of the macro, not its original definition, since
86+
// the rexport defines the path that a user will actually see. Accordingly,
87+
// we add the rexport as an item here, and then skip over the original
88+
// definition in `visit_item()` below.
89+
for export in self.cx.tcx.module_exports(CRATE_DEF_ID).unwrap_or(&[]) {
90+
if let Res::Def(DefKind::Macro(_), def_id) = export.res {
91+
if let Some(local_def_id) = def_id.as_local() {
92+
if self.cx.tcx.has_attr(def_id, sym::macro_export) {
93+
let hir_id = self.cx.tcx.hir().local_def_id_to_hir_id(local_def_id);
94+
let item = self.cx.tcx.hir().expect_item(hir_id);
95+
top_level_module.items.push((item, None));
96+
}
12097
}
12198
}
122-
let cur_mod_def_id = tcx.hir().local_def_id(cur_mod.id).to_def_id();
123-
assert_eq!(cur_mod_def_id, macro_parent_def_id);
124-
cur_mod.macros.push((def, None));
12599
}
126100
self.cx.cache.exact_paths = self.exact_paths;
127101
top_level_module
@@ -238,10 +212,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
238212
self.inlining = prev;
239213
true
240214
}
241-
Node::MacroDef(def) if !glob => {
242-
om.macros.push((def, renamed));
243-
true
244-
}
245215
_ => false,
246216
};
247217
self.view_item_stack.remove(&res_hir_id);
@@ -257,7 +227,10 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
257227
debug!("visiting item {:?}", item);
258228
let name = renamed.unwrap_or(item.ident.name);
259229

260-
if item.vis.node.is_pub() {
230+
let def_id = item.def_id.to_def_id();
231+
let is_pub = item.vis.node.is_pub() || self.cx.tcx.has_attr(def_id, sym::macro_export);
232+
233+
if is_pub {
261234
self.store_path(item.def_id.to_def_id());
262235
}
263236

@@ -269,7 +242,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
269242
}
270243
}
271244
// If we're inlining, skip private items.
272-
_ if self.inlining && !item.vis.node.is_pub() => {}
245+
_ if self.inlining && !is_pub => {}
273246
hir::ItemKind::GlobalAsm(..) => {}
274247
hir::ItemKind::Use(_, hir::UseKind::ListStem) => {}
275248
hir::ItemKind::Use(ref path, kind) => {
@@ -285,7 +258,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
285258

286259
// If there was a private module in the current path then don't bother inlining
287260
// anything as it will probably be stripped anyway.
288-
if item.vis.node.is_pub() && self.inside_public_path {
261+
if is_pub && self.inside_public_path {
289262
let please_inline = attrs.iter().any(|item| match item.meta_item_list() {
290263
Some(ref list) if item.has_name(sym::doc) => {
291264
list.iter().any(|i| i.has_name(sym::inline))
@@ -307,6 +280,26 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
307280

308281
om.items.push((item, renamed))
309282
}
283+
hir::ItemKind::Macro(ref macro_def) => {
284+
// `#[macro_export] macro_rules!` items are handled seperately in `visit()`,
285+
// above, since they need to be documented at the module top level. Accordingly,
286+
// we only want to handle macros if one of three conditions holds:
287+
//
288+
// 1. This macro was defined by `macro`, and thus isn't covered by the case
289+
// above.
290+
// 2. This macro isn't marked with `#[macro_export]`, and thus isn't covered
291+
// by the case above.
292+
// 3. We're inlining, since a reexport where inlining has been requested
293+
// should be inlined even if it is also documented at the top level.
294+
295+
let def_id = item.def_id.to_def_id();
296+
let is_macro_2_0 = !macro_def.macro_rules;
297+
let nonexported = !self.cx.tcx.has_attr(def_id, sym::macro_export);
298+
299+
if is_macro_2_0 || nonexported || self.inlining {
300+
om.items.push((item, renamed));
301+
}
302+
}
310303
hir::ItemKind::Mod(ref m) => {
311304
om.mods.push(self.visit_mod_contents(&item.vis, item.hir_id(), m, name));
312305
}

src/tools/clippy/clippy_lints/src/missing_doc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
122122
},
123123
hir::ItemKind::Const(..)
124124
| hir::ItemKind::Enum(..)
125-
| hir::ItemKind::Mod(..)
126125
| hir::ItemKind::Macro(..)
126+
| hir::ItemKind::Mod(..)
127127
| hir::ItemKind::Static(..)
128128
| hir::ItemKind::Struct(..)
129129
| hir::ItemKind::Trait(..)

src/tools/clippy/clippy_lints/src/missing_inline.rs

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
118118
},
119119
hir::ItemKind::Const(..)
120120
| hir::ItemKind::Enum(..)
121+
| hir::ItemKind::Macro(..)
121122
| hir::ItemKind::Mod(..)
122123
| hir::ItemKind::Static(..)
123124
| hir::ItemKind::Struct(..)

src/tools/clippy/clippy_lints/src/utils/inspector.rs

+7
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,13 @@ fn print_item(cx: &LateContext<'_>, item: &hir::Item<'_>) {
381381
let item_ty = cx.tcx.type_of(did);
382382
println!("function of type {:#?}", item_ty);
383383
},
384+
hir::ItemKind::Macro(ref macro_def) => {
385+
if macro_def.macro_rules {
386+
println!("macro introduced by `macro_rules!`");
387+
} else {
388+
println!("macro introduced by `macro`");
389+
}
390+
},
384391
hir::ItemKind::Mod(..) => println!("module"),
385392
hir::ItemKind::ForeignMod { abi, .. } => println!("foreign module with abi: {}", abi),
386393
hir::ItemKind::GlobalAsm(asm) => println!("global asm: {:?}", asm),

0 commit comments

Comments
 (0)