Skip to content

Commit df2de34

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 a76339f commit df2de34

File tree

5 files changed

+70
-39
lines changed

5 files changed

+70
-39
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_metadata::creader::{CStore, LoadedMacro};
1212
use rustc_middle::ty::fast_reject::SimplifiedType;
1313
use rustc_middle::ty::{self, TyCtxt};
1414
use rustc_span::def_id::LOCAL_CRATE;
15-
use rustc_span::hygiene::MacroKind;
15+
use rustc_span::hygiene::{MacroKind, MacroKinds};
1616
use rustc_span::symbol::{Symbol, sym};
1717
use thin_vec::{ThinVec, thin_vec};
1818
use tracing::{debug, trace};
@@ -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
@@ -50,7 +50,7 @@ use rustc_middle::middle::resolve_bound_vars as rbv;
5050
use rustc_middle::ty::{self, AdtKind, GenericArgsRef, Ty, TyCtxt, TypeVisitableExt, TypingMode};
5151
use rustc_middle::{bug, span_bug};
5252
use rustc_span::ExpnKind;
53-
use rustc_span::hygiene::{AstPass, MacroKind};
53+
use rustc_span::hygiene::{AstPass, MacroKind, MacroKinds};
5454
use rustc_span::symbol::{Ident, Symbol, kw, sym};
5555
use rustc_trait_selection::traits::wf::object_region_bounds;
5656
use thin_vec::ThinVec;
@@ -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
@@ -3,7 +3,7 @@
33
use std::fmt;
44

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

99
use crate::clean;
@@ -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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ 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;
28+
use rustc_span::hygiene::MacroKinds;
2929
use rustc_span::symbol::{Ident, Symbol, sym};
3030
use smallvec::{SmallVec, smallvec};
3131
use tracing::{debug, info, instrument, trace};
@@ -115,9 +115,11 @@ impl Res {
115115

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

120-
DefKind::Macro(MacroKind::Derive) => "derive",
122+
DefKind::Macro(MacroKinds::DERIVE) => "derive",
121123
DefKind::Struct => "struct",
122124
DefKind::Enum => "enum",
123125
DefKind::Trait => "trait",
@@ -881,9 +883,12 @@ fn trait_impls_for<'a>(
881883
fn is_derive_trait_collision<T>(ns: &PerNS<Result<Vec<(Res, T)>, ResolutionFailure<'_>>>) -> bool {
882884
if let (Ok(type_ns), Ok(macro_ns)) = (&ns.type_ns, &ns.macro_ns) {
883885
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), _)))
886+
&& macro_ns.iter().any(|(res, _)| {
887+
matches!(
888+
res,
889+
Res::Def(DefKind::Macro(kinds), _) if kinds.contains(MacroKinds::DERIVE)
890+
)
891+
})
887892
} else {
888893
false
889894
}
@@ -1662,11 +1667,11 @@ impl Disambiguator {
16621667

16631668
let suffixes = [
16641669
// 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)),
1670+
("!()", DefKind::Macro(MacroKinds::BANG)),
1671+
("!{}", DefKind::Macro(MacroKinds::BANG)),
1672+
("![]", DefKind::Macro(MacroKinds::BANG)),
16681673
("()", DefKind::Fn),
1669-
("!", DefKind::Macro(MacroKind::Bang)),
1674+
("!", DefKind::Macro(MacroKinds::BANG)),
16701675
];
16711676

16721677
if let Some(idx) = link.find('@') {
@@ -1685,7 +1690,7 @@ impl Disambiguator {
16851690
safety: Safety::Safe,
16861691
}),
16871692
"function" | "fn" | "method" => Kind(DefKind::Fn),
1688-
"derive" => Kind(DefKind::Macro(MacroKind::Derive)),
1693+
"derive" => Kind(DefKind::Macro(MacroKinds::DERIVE)),
16891694
"field" => Kind(DefKind::Field),
16901695
"variant" => Kind(DefKind::Variant),
16911696
"type" => NS(Namespace::TypeNS),

src/librustdoc/visit_ast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ 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;
16+
use rustc_span::hygiene::MacroKinds;
1717
use rustc_span::symbol::{Symbol, kw, sym};
1818
use tracing::debug;
1919

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

326326
let is_bang_macro = matches!(
327327
item,
328-
Node::Item(&hir::Item { kind: hir::ItemKind::Macro(_, _, MacroKind::Bang), .. })
328+
Node::Item(&hir::Item { kind: hir::ItemKind::Macro(_, _, kinds), .. }) if kinds.contains(MacroKinds::BANG)
329329
);
330330

331331
if !self.view_item_stack.insert(res_did) && !is_bang_macro {
@@ -406,7 +406,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
406406
// attribute can still be visible.
407407
|| match item.kind {
408408
hir::ItemKind::Impl(..) => true,
409-
hir::ItemKind::Macro(_, _, MacroKind::Bang) => {
409+
hir::ItemKind::Macro(_, _, _) => {
410410
self.cx.tcx.has_attr(item.owner_id.def_id, sym::macro_export)
411411
}
412412
_ => false,

0 commit comments

Comments
 (0)