Skip to content

Commit e1fc89a

Browse files
committed
rustdoc: Minimal fixes to compile with MacroKinds
This makes the minimal fixes necessary for rustdoc to compile and pass existing tests with the switch to `MacroKinds`. It only works for macros that don't actually have multiple kinds, and will panic (with a `todo!`) if it encounters a macro with multiple kinds. rustdoc needs further fixes to handle macros with multiple kinds, and to handle attributes and derive macros that aren't proc macros.
1 parent 33ba4a2 commit e1fc89a

File tree

5 files changed

+70
-41
lines changed

5 files changed

+70
-41
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::sync::Arc;
66
use rustc_data_structures::fx::FxHashSet;
77
use rustc_hir as hir;
88
use rustc_hir::Mutability;
9-
use rustc_hir::def::{DefKind, Res};
9+
use rustc_hir::def::{DefKind, MacroKinds, Res};
1010
use rustc_hir::def_id::{DefId, DefIdSet, LocalDefId, LocalModDefId};
1111
use rustc_metadata::creader::{CStore, LoadedMacro};
1212
use rustc_middle::ty::fast_reject::SimplifiedType;
@@ -137,13 +137,16 @@ pub(crate) fn try_inline(
137137
clean::ConstantItem(Box::new(ct))
138138
})
139139
}
140-
Res::Def(DefKind::Macro(kind), did) => {
141-
let mac = build_macro(cx, did, name, kind);
142-
143-
let type_kind = match kind {
144-
MacroKind::Bang => ItemType::Macro,
145-
MacroKind::Attr => ItemType::ProcAttribute,
146-
MacroKind::Derive => ItemType::ProcDerive,
140+
Res::Def(DefKind::Macro(kinds), did) => {
141+
let mac = build_macro(cx, did, name, kinds);
142+
143+
// FIXME: handle attributes and derives that aren't proc macros, and macros with
144+
// multiple kinds
145+
let type_kind = match kinds {
146+
MacroKinds::BANG => ItemType::Macro,
147+
MacroKinds::ATTR => ItemType::ProcAttribute,
148+
MacroKinds::DERIVE => ItemType::ProcDerive,
149+
_ => todo!("Handle macros with multiple kinds"),
147150
};
148151
record_extern_fqn(cx, did, type_kind);
149152
mac
@@ -749,22 +752,36 @@ fn build_macro(
749752
cx: &mut DocContext<'_>,
750753
def_id: DefId,
751754
name: Symbol,
752-
macro_kind: MacroKind,
755+
macro_kinds: MacroKinds,
753756
) -> clean::ItemKind {
754757
match CStore::from_tcx(cx.tcx).load_macro_untracked(def_id, cx.tcx) {
755-
LoadedMacro::MacroDef { def, .. } => match macro_kind {
756-
MacroKind::Bang => clean::MacroItem(clean::Macro {
758+
// FIXME: handle attributes and derives that aren't proc macros, and macros with multiple
759+
// kinds
760+
LoadedMacro::MacroDef { def, .. } => match macro_kinds {
761+
MacroKinds::BANG => clean::MacroItem(clean::Macro {
757762
source: utils::display_macro_source(cx, name, &def),
758763
macro_rules: def.macro_rules,
759764
}),
760-
MacroKind::Derive | MacroKind::Attr => {
761-
clean::ProcMacroItem(clean::ProcMacro { kind: macro_kind, helpers: Vec::new() })
762-
}
765+
MacroKinds::DERIVE => clean::ProcMacroItem(clean::ProcMacro {
766+
kind: MacroKind::Derive,
767+
helpers: Vec::new(),
768+
}),
769+
MacroKinds::ATTR => clean::ProcMacroItem(clean::ProcMacro {
770+
kind: MacroKind::Attr,
771+
helpers: Vec::new(),
772+
}),
773+
_ => todo!("Handle macros with multiple kinds"),
763774
},
764-
LoadedMacro::ProcMacro(ext) => clean::ProcMacroItem(clean::ProcMacro {
765-
kind: ext.macro_kind(),
766-
helpers: ext.helper_attrs,
767-
}),
775+
LoadedMacro::ProcMacro(ext) => {
776+
// Proc macros can only have a single kind
777+
let kind = match ext.macro_kinds() {
778+
MacroKinds::BANG => MacroKind::Bang,
779+
MacroKinds::ATTR => MacroKind::Attr,
780+
MacroKinds::DERIVE => MacroKind::Derive,
781+
_ => unreachable!(),
782+
};
783+
clean::ProcMacroItem(clean::ProcMacro { kind, helpers: ext.helper_attrs })
784+
}
768785
}
769786
}
770787

src/librustdoc/clean/mod.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet, In
4040
use rustc_errors::codes::*;
4141
use rustc_errors::{FatalError, struct_span_code_err};
4242
use rustc_hir::attrs::AttributeKind;
43-
use rustc_hir::def::{CtorKind, DefKind, Res};
43+
use rustc_hir::def::{CtorKind, DefKind, MacroKinds, Res};
4444
use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LOCAL_CRATE, LocalDefId};
4545
use rustc_hir::{LangItem, PredicateOrigin, find_attr};
4646
use rustc_hir_analysis::hir_ty_lowering::FeedConstTy;
@@ -2845,11 +2845,19 @@ fn clean_maybe_renamed_item<'tcx>(
28452845
generics: clean_generics(generics, cx),
28462846
fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
28472847
}),
2848-
ItemKind::Macro(_, macro_def, MacroKind::Bang) => MacroItem(Macro {
2848+
// FIXME: handle attributes and derives that aren't proc macros, and macros with
2849+
// multiple kinds
2850+
ItemKind::Macro(_, macro_def, MacroKinds::BANG) => MacroItem(Macro {
28492851
source: display_macro_source(cx, name, macro_def),
28502852
macro_rules: macro_def.macro_rules,
28512853
}),
2852-
ItemKind::Macro(_, _, macro_kind) => clean_proc_macro(item, &mut name, macro_kind, cx),
2854+
ItemKind::Macro(_, _, MacroKinds::ATTR) => {
2855+
clean_proc_macro(item, &mut name, MacroKind::Attr, cx)
2856+
}
2857+
ItemKind::Macro(_, _, MacroKinds::DERIVE) => {
2858+
clean_proc_macro(item, &mut name, MacroKind::Derive, cx)
2859+
}
2860+
ItemKind::Macro(_, _, _) => todo!("Handle macros with multiple kinds"),
28532861
// proc macros can have a name set by attributes
28542862
ItemKind::Fn { ref sig, generics, body: body_id, .. } => {
28552863
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)

src/librustdoc/formats/item_type.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::fmt;
44

5-
use rustc_hir::def::{CtorOf, DefKind};
5+
use rustc_hir::def::{CtorOf, DefKind, MacroKinds};
66
use rustc_span::hygiene::MacroKind;
77
use serde::{Serialize, Serializer};
88

@@ -134,9 +134,10 @@ impl ItemType {
134134
DefKind::Trait => Self::Trait,
135135
DefKind::TyAlias => Self::TypeAlias,
136136
DefKind::TraitAlias => Self::TraitAlias,
137-
DefKind::Macro(MacroKind::Bang) => ItemType::Macro,
138-
DefKind::Macro(MacroKind::Attr) => ItemType::ProcAttribute,
139-
DefKind::Macro(MacroKind::Derive) => ItemType::ProcDerive,
137+
DefKind::Macro(MacroKinds::BANG) => ItemType::Macro,
138+
DefKind::Macro(MacroKinds::ATTR) => ItemType::ProcAttribute,
139+
DefKind::Macro(MacroKinds::DERIVE) => ItemType::ProcDerive,
140+
DefKind::Macro(_) => todo!("Handle macros with multiple kinds"),
140141
DefKind::ForeignTy => Self::ForeignType,
141142
DefKind::Variant => Self::Variant,
142143
DefKind::Field => Self::StructField,

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
1313
use rustc_data_structures::intern::Interned;
1414
use rustc_errors::{Applicability, Diag, DiagMessage};
1515
use rustc_hir::def::Namespace::*;
16-
use rustc_hir::def::{DefKind, Namespace, PerNS};
16+
use rustc_hir::def::{DefKind, MacroKinds, Namespace, PerNS};
1717
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LOCAL_CRATE};
1818
use rustc_hir::{Mutability, Safety};
1919
use rustc_middle::ty::{Ty, TyCtxt};
@@ -25,7 +25,6 @@ use rustc_resolve::rustdoc::{
2525
use rustc_session::config::CrateType;
2626
use rustc_session::lint::Lint;
2727
use rustc_span::BytePos;
28-
use rustc_span::hygiene::MacroKind;
2928
use rustc_span::symbol::{Ident, Symbol, sym};
3029
use smallvec::{SmallVec, smallvec};
3130
use tracing::{debug, info, instrument, trace};
@@ -115,9 +114,11 @@ impl Res {
115114

116115
let prefix = match kind {
117116
DefKind::Fn | DefKind::AssocFn => return Suggestion::Function,
118-
DefKind::Macro(MacroKind::Bang) => return Suggestion::Macro,
117+
// FIXME: handle macros with multiple kinds, and attribute/derive macros that aren't
118+
// proc macros
119+
DefKind::Macro(MacroKinds::BANG) => return Suggestion::Macro,
119120

120-
DefKind::Macro(MacroKind::Derive) => "derive",
121+
DefKind::Macro(MacroKinds::DERIVE) => "derive",
121122
DefKind::Struct => "struct",
122123
DefKind::Enum => "enum",
123124
DefKind::Trait => "trait",
@@ -881,9 +882,12 @@ fn trait_impls_for<'a>(
881882
fn is_derive_trait_collision<T>(ns: &PerNS<Result<Vec<(Res, T)>, ResolutionFailure<'_>>>) -> bool {
882883
if let (Ok(type_ns), Ok(macro_ns)) = (&ns.type_ns, &ns.macro_ns) {
883884
type_ns.iter().any(|(res, _)| matches!(res, Res::Def(DefKind::Trait, _)))
884-
&& macro_ns
885-
.iter()
886-
.any(|(res, _)| matches!(res, Res::Def(DefKind::Macro(MacroKind::Derive), _)))
885+
&& macro_ns.iter().any(|(res, _)| {
886+
matches!(
887+
res,
888+
Res::Def(DefKind::Macro(kinds), _) if kinds.contains(MacroKinds::DERIVE)
889+
)
890+
})
887891
} else {
888892
false
889893
}
@@ -1662,11 +1666,11 @@ impl Disambiguator {
16621666

16631667
let suffixes = [
16641668
// If you update this list, please also update the relevant rustdoc book section!
1665-
("!()", DefKind::Macro(MacroKind::Bang)),
1666-
("!{}", DefKind::Macro(MacroKind::Bang)),
1667-
("![]", DefKind::Macro(MacroKind::Bang)),
1669+
("!()", DefKind::Macro(MacroKinds::BANG)),
1670+
("!{}", DefKind::Macro(MacroKinds::BANG)),
1671+
("![]", DefKind::Macro(MacroKinds::BANG)),
16681672
("()", DefKind::Fn),
1669-
("!", DefKind::Macro(MacroKind::Bang)),
1673+
("!", DefKind::Macro(MacroKinds::BANG)),
16701674
];
16711675

16721676
if let Some(idx) = link.find('@') {
@@ -1685,7 +1689,7 @@ impl Disambiguator {
16851689
safety: Safety::Safe,
16861690
}),
16871691
"function" | "fn" | "method" => Kind(DefKind::Fn),
1688-
"derive" => Kind(DefKind::Macro(MacroKind::Derive)),
1692+
"derive" => Kind(DefKind::Macro(MacroKinds::DERIVE)),
16891693
"field" => Kind(DefKind::Field),
16901694
"variant" => Kind(DefKind::Variant),
16911695
"type" => NS(Namespace::TypeNS),

src/librustdoc/visit_ast.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ use std::mem;
55

66
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
77
use rustc_hir as hir;
8-
use rustc_hir::def::{DefKind, Res};
8+
use rustc_hir::def::{DefKind, MacroKinds, Res};
99
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, LocalDefIdSet};
1010
use rustc_hir::intravisit::{Visitor, walk_body, walk_item};
1111
use rustc_hir::{CRATE_HIR_ID, Node};
1212
use rustc_middle::hir::nested_filter;
1313
use rustc_middle::ty::TyCtxt;
1414
use rustc_span::Span;
1515
use rustc_span::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
16-
use rustc_span::hygiene::MacroKind;
1716
use rustc_span::symbol::{Symbol, kw, sym};
1817
use tracing::debug;
1918

@@ -325,7 +324,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
325324

326325
let is_bang_macro = matches!(
327326
item,
328-
Node::Item(&hir::Item { kind: hir::ItemKind::Macro(_, _, MacroKind::Bang), .. })
327+
Node::Item(&hir::Item { kind: hir::ItemKind::Macro(_, _, kinds), .. }) if kinds.contains(MacroKinds::BANG)
329328
);
330329

331330
if !self.view_item_stack.insert(res_did) && !is_bang_macro {
@@ -406,7 +405,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
406405
// attribute can still be visible.
407406
|| match item.kind {
408407
hir::ItemKind::Impl(..) => true,
409-
hir::ItemKind::Macro(_, _, MacroKind::Bang) => {
408+
hir::ItemKind::Macro(_, _, _) => {
410409
self.cx.tcx.has_attr(item.owner_id.def_id, sym::macro_export)
411410
}
412411
_ => false,

0 commit comments

Comments
 (0)