Skip to content

Commit 4b58358

Browse files
committed
Switch to a bitflags MacroKinds to support macros with more than one kind
Review everything that uses `MacroKind`, and switch anything that could refer to more than one kind to use `MacroKinds`. Add a new `SyntaxExtensionKind::MacroRules` for `macro_rules!` macros, using the concrete `MacroRulesMacroExpander` type, and have it track which kinds it can handle. Eliminate the separate optional `attr_ext`, now that a `SyntaxExtension` can handle multiple macro kinds. This also avoids the need to downcast when calling methods on `MacroRulesMacroExpander`, such as `get_unused_rule`. Integrate macro kind checking into name resolution's `sub_namespace_match`, so that we only find a macro if it's the right type, and eliminate the special-case hack for attributes.
1 parent 6355cd3 commit 4b58358

File tree

20 files changed

+214
-120
lines changed

20 files changed

+214
-120
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4603,6 +4603,7 @@ dependencies = [
46034603
name = "rustc_span"
46044604
version = "0.0.0"
46054605
dependencies = [
4606+
"bitflags",
46064607
"blake3",
46074608
"derive-where",
46084609
"indexmap",

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,14 +464,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
464464
let body = Box::new(self.lower_delim_args(body));
465465
let def_id = self.local_def_id(id);
466466
let def_kind = self.tcx.def_kind(def_id);
467-
let DefKind::Macro(macro_kind) = def_kind else {
467+
let DefKind::Macro(macro_kinds) = def_kind else {
468468
unreachable!(
469469
"expected DefKind::Macro for macro item, found {}",
470470
def_kind.descr(def_id.to_def_id())
471471
);
472472
};
473473
let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules });
474-
hir::ItemKind::Macro(ident, macro_def, macro_kind)
474+
hir::ItemKind::Macro(ident, macro_def, macro_kinds)
475475
}
476476
ItemKind::Delegation(box delegation) => {
477477
let delegation_results = self.lower_delegation(delegation, id, false);

compiler/rustc_expand/src/base.rs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_session::parse::ParseSess;
2626
use rustc_session::{Limit, Session};
2727
use rustc_span::def_id::{CrateNum, DefId, LocalDefId};
2828
use rustc_span::edition::Edition;
29-
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId, MacroKind};
29+
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId, MacroKind, MacroKinds};
3030
use rustc_span::source_map::SourceMap;
3131
use rustc_span::{DUMMY_SP, FileName, Ident, Span, Symbol, kw, sym};
3232
use smallvec::{SmallVec, smallvec};
@@ -718,6 +718,9 @@ impl MacResult for DummyResult {
718718
/// A syntax extension kind.
719719
#[derive(Clone)]
720720
pub enum SyntaxExtensionKind {
721+
/// A `macro_rules!` macro that can work as any `MacroKind`
722+
MacroRules(Arc<crate::MacroRulesMacroExpander>),
723+
721724
/// A token-based function-like macro.
722725
Bang(
723726
/// An expander with signature TokenStream -> TokenStream.
@@ -775,6 +778,34 @@ pub enum SyntaxExtensionKind {
775778
GlobDelegation(Arc<dyn GlobDelegationExpander + sync::DynSync + sync::DynSend>),
776779
}
777780

781+
impl SyntaxExtensionKind {
782+
/// Returns `Some(expander)` for a macro usable as a `LegacyBang`; otherwise returns `None`
783+
///
784+
/// This includes a `MacroRules` with function-like rules.
785+
pub fn as_legacy_bang(&self) -> Option<&(dyn TTMacroExpander + sync::DynSync + sync::DynSend)> {
786+
match self {
787+
SyntaxExtensionKind::LegacyBang(exp) => Some(exp.as_ref()),
788+
SyntaxExtensionKind::MacroRules(exp) if exp.kinds().contains(MacroKinds::BANG) => {
789+
Some(exp.as_ref())
790+
}
791+
_ => None,
792+
}
793+
}
794+
795+
/// Returns `Some(expander)` for a macro usable as an `Attr`; otherwise returns `None`
796+
///
797+
/// This includes a `MacroRules` with `attr` rules.
798+
pub fn as_attr(&self) -> Option<&(dyn AttrProcMacro + sync::DynSync + sync::DynSend)> {
799+
match self {
800+
SyntaxExtensionKind::Attr(exp) => Some(exp.as_ref()),
801+
SyntaxExtensionKind::MacroRules(exp) if exp.kinds().contains(MacroKinds::ATTR) => {
802+
Some(exp.as_ref())
803+
}
804+
_ => None,
805+
}
806+
}
807+
}
808+
778809
/// A struct representing a macro definition in "lowered" form ready for expansion.
779810
pub struct SyntaxExtension {
780811
/// A syntax extension kind.
@@ -804,18 +835,19 @@ pub struct SyntaxExtension {
804835
}
805836

806837
impl SyntaxExtension {
807-
/// Returns which kind of macro calls this syntax extension.
808-
pub fn macro_kind(&self) -> MacroKind {
838+
/// Returns which kinds of macro call this syntax extension.
839+
pub fn macro_kinds(&self) -> MacroKinds {
809840
match self.kind {
810841
SyntaxExtensionKind::Bang(..)
811842
| SyntaxExtensionKind::LegacyBang(..)
812-
| SyntaxExtensionKind::GlobDelegation(..) => MacroKind::Bang,
843+
| SyntaxExtensionKind::GlobDelegation(..) => MacroKinds::BANG,
813844
SyntaxExtensionKind::Attr(..)
814845
| SyntaxExtensionKind::LegacyAttr(..)
815-
| SyntaxExtensionKind::NonMacroAttr => MacroKind::Attr,
846+
| SyntaxExtensionKind::NonMacroAttr => MacroKinds::ATTR,
816847
SyntaxExtensionKind::Derive(..) | SyntaxExtensionKind::LegacyDerive(..) => {
817-
MacroKind::Derive
848+
MacroKinds::DERIVE
818849
}
850+
SyntaxExtensionKind::MacroRules(ref m) => m.kinds(),
819851
}
820852
}
821853

@@ -1027,11 +1059,12 @@ impl SyntaxExtension {
10271059
parent: LocalExpnId,
10281060
call_site: Span,
10291061
descr: Symbol,
1062+
kind: MacroKind,
10301063
macro_def_id: Option<DefId>,
10311064
parent_module: Option<DefId>,
10321065
) -> ExpnData {
10331066
ExpnData::new(
1034-
ExpnKind::Macro(self.macro_kind(), descr),
1067+
ExpnKind::Macro(kind, descr),
10351068
parent.to_expn_id(),
10361069
call_site,
10371070
self.span,

compiler/rustc_expand/src/expand.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
736736

737737
let (fragment_kind, span) = (invoc.fragment_kind, invoc.span());
738738
ExpandResult::Ready(match invoc.kind {
739-
InvocationKind::Bang { mac, span } => match ext {
740-
SyntaxExtensionKind::Bang(expander) => {
739+
InvocationKind::Bang { mac, span } => {
740+
if let SyntaxExtensionKind::Bang(expander) = ext {
741741
match expander.expand(self.cx, span, mac.args.tokens.clone()) {
742742
Ok(tok_result) => {
743743
let fragment =
@@ -755,8 +755,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
755755
}
756756
Err(guar) => return ExpandResult::Ready(fragment_kind.dummy(span, guar)),
757757
}
758-
}
759-
SyntaxExtensionKind::LegacyBang(expander) => {
758+
} else if let Some(expander) = ext.as_legacy_bang() {
760759
let tok_result = match expander.expand(self.cx, span, mac.args.tokens.clone()) {
761760
ExpandResult::Ready(tok_result) => tok_result,
762761
ExpandResult::Retry(_) => {
@@ -776,11 +775,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
776775
let guar = self.error_wrong_fragment_kind(fragment_kind, &mac, span);
777776
fragment_kind.dummy(span, guar)
778777
}
778+
} else {
779+
unreachable!();
779780
}
780-
_ => unreachable!(),
781-
},
782-
InvocationKind::Attr { attr, pos, mut item, derives } => match ext {
783-
SyntaxExtensionKind::Attr(expander) => {
781+
}
782+
InvocationKind::Attr { attr, pos, mut item, derives } => {
783+
if let Some(expander) = ext.as_attr() {
784784
self.gate_proc_macro_input(&item);
785785
self.gate_proc_macro_attr_item(span, &item);
786786
let tokens = match &item {
@@ -835,8 +835,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
835835
}
836836
Err(guar) => return ExpandResult::Ready(fragment_kind.dummy(span, guar)),
837837
}
838-
}
839-
SyntaxExtensionKind::LegacyAttr(expander) => {
838+
} else if let SyntaxExtensionKind::LegacyAttr(expander) = ext {
840839
match validate_attr::parse_meta(&self.cx.sess.psess, &attr) {
841840
Ok(meta) => {
842841
let item_clone = macro_stats.then(|| item.clone());
@@ -878,15 +877,15 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
878877
fragment_kind.expect_from_annotatables(iter::once(item))
879878
}
880879
}
881-
}
882-
SyntaxExtensionKind::NonMacroAttr => {
880+
} else if let SyntaxExtensionKind::NonMacroAttr = ext {
883881
// `-Zmacro-stats` ignores these because they don't do any real expansion.
884882
self.cx.expanded_inert_attrs.mark(&attr);
885883
item.visit_attrs(|attrs| attrs.insert(pos, attr));
886884
fragment_kind.expect_from_annotatables(iter::once(item))
885+
} else {
886+
unreachable!();
887887
}
888-
_ => unreachable!(),
889-
},
888+
}
890889
InvocationKind::Derive { path, item, is_const } => match ext {
891890
SyntaxExtensionKind::Derive(expander)
892891
| SyntaxExtensionKind::LegacyDerive(expander) => {

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_parse::parser::{Parser, Recovery};
2525
use rustc_session::Session;
2626
use rustc_session::parse::{ParseSess, feature_err};
2727
use rustc_span::edition::Edition;
28-
use rustc_span::hygiene::Transparency;
28+
use rustc_span::hygiene::{MacroKinds, Transparency};
2929
use rustc_span::{Ident, Span, kw, sym};
3030
use tracing::{debug, instrument, trace, trace_span};
3131

@@ -144,6 +144,7 @@ pub struct MacroRulesMacroExpander {
144144
name: Ident,
145145
span: Span,
146146
transparency: Transparency,
147+
kinds: MacroKinds,
147148
rules: Vec<MacroRule>,
148149
}
149150

@@ -158,6 +159,10 @@ impl MacroRulesMacroExpander {
158159
};
159160
if has_compile_error_macro(rhs) { None } else { Some((&self.name, span)) }
160161
}
162+
163+
pub fn kinds(&self) -> MacroKinds {
164+
self.kinds
165+
}
161166
}
162167

163168
impl TTMacroExpander for MacroRulesMacroExpander {
@@ -540,13 +545,13 @@ pub fn compile_declarative_macro(
540545
span: Span,
541546
node_id: NodeId,
542547
edition: Edition,
543-
) -> (SyntaxExtension, Option<Arc<SyntaxExtension>>, usize) {
548+
) -> (SyntaxExtension, usize) {
544549
let mk_syn_ext = |kind| {
545550
let is_local = is_defined_in_current_crate(node_id);
546551
SyntaxExtension::new(sess, kind, span, Vec::new(), edition, ident.name, attrs, is_local)
547552
};
548-
let mk_bang_ext = |expander| mk_syn_ext(SyntaxExtensionKind::LegacyBang(expander));
549-
let dummy_syn_ext = |guar| (mk_bang_ext(Arc::new(DummyExpander(guar))), None, 0);
553+
let dummy_syn_ext =
554+
|guar| (mk_syn_ext(SyntaxExtensionKind::LegacyBang(Arc::new(DummyExpander(guar)))), 0);
550555

551556
let macro_rules = macro_def.macro_rules;
552557
let exp_sep = if macro_rules { exp!(Semi) } else { exp!(Comma) };
@@ -559,12 +564,12 @@ pub fn compile_declarative_macro(
559564
let mut guar = None;
560565
let mut check_emission = |ret: Result<(), ErrorGuaranteed>| guar = guar.or(ret.err());
561566

562-
let mut has_attr_rules = false;
567+
let mut kinds = MacroKinds::empty();
563568
let mut rules = Vec::new();
564569

565570
while p.token != token::Eof {
566571
let args = if p.eat_keyword_noexpect(sym::attr) {
567-
has_attr_rules = true;
572+
kinds |= MacroKinds::ATTR;
568573
if !features.macro_attr() {
569574
feature_err(sess, sym::macro_attr, span, "`macro_rules!` attributes are unstable")
570575
.emit();
@@ -581,6 +586,7 @@ pub fn compile_declarative_macro(
581586
}
582587
Some(args)
583588
} else {
589+
kinds |= MacroKinds::BANG;
584590
None
585591
};
586592
let lhs_tt = p.parse_token_tree();
@@ -627,6 +633,7 @@ pub fn compile_declarative_macro(
627633
let guar = sess.dcx().span_err(span, "macros must contain at least one rule");
628634
return dummy_syn_ext(guar);
629635
}
636+
assert!(!kinds.is_empty());
630637

631638
let transparency = find_attr!(attrs, AttributeKind::MacroTransparency(x) => *x)
632639
.unwrap_or(Transparency::fallback(macro_rules));
@@ -640,12 +647,8 @@ pub fn compile_declarative_macro(
640647
// Return the number of rules for unused rule linting, if this is a local macro.
641648
let nrules = if is_defined_in_current_crate(node_id) { rules.len() } else { 0 };
642649

643-
let exp = Arc::new(MacroRulesMacroExpander { name: ident, span, node_id, transparency, rules });
644-
let opt_attr_ext = has_attr_rules.then(|| {
645-
let exp = Arc::clone(&exp);
646-
Arc::new(mk_syn_ext(SyntaxExtensionKind::Attr(exp)))
647-
});
648-
(mk_bang_ext(exp), opt_attr_ext, nrules)
650+
let exp = MacroRulesMacroExpander { name: ident, kinds, span, node_id, transparency, rules };
651+
(mk_syn_ext(SyntaxExtensionKind::MacroRules(Arc::new(exp))), nrules)
649652
}
650653

651654
fn check_no_eof(sess: &Session, p: &Parser<'_>, msg: &'static str) -> Option<ErrorGuaranteed> {

compiler/rustc_hir/src/def.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_data_structures::unord::UnordMap;
88
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
99
use rustc_span::Symbol;
1010
use rustc_span::def_id::{DefId, LocalDefId};
11-
use rustc_span::hygiene::MacroKind;
11+
use rustc_span::hygiene::MacroKinds;
1212

1313
use crate::definitions::DefPathData;
1414
use crate::hir;
@@ -101,7 +101,7 @@ pub enum DefKind {
101101
AssocConst,
102102

103103
// Macro namespace
104-
Macro(MacroKind),
104+
Macro(MacroKinds),
105105

106106
// Not namespaced (or they are, but we don't treat them so)
107107
ExternCrate,
@@ -177,7 +177,7 @@ impl DefKind {
177177
DefKind::AssocConst => "associated constant",
178178
DefKind::TyParam => "type parameter",
179179
DefKind::ConstParam => "const parameter",
180-
DefKind::Macro(macro_kind) => macro_kind.descr(),
180+
DefKind::Macro(kinds) => kinds.descr(),
181181
DefKind::LifetimeParam => "lifetime parameter",
182182
DefKind::Use => "import",
183183
DefKind::ForeignMod => "foreign module",
@@ -208,7 +208,7 @@ impl DefKind {
208208
| DefKind::Use
209209
| DefKind::InlineConst
210210
| DefKind::ExternCrate => "an",
211-
DefKind::Macro(macro_kind) => macro_kind.article(),
211+
DefKind::Macro(kinds) => kinds.article(),
212212
_ => "a",
213213
}
214214
}
@@ -845,10 +845,10 @@ impl<Id> Res<Id> {
845845
)
846846
}
847847

848-
pub fn macro_kind(self) -> Option<MacroKind> {
848+
pub fn macro_kinds(self) -> Option<MacroKinds> {
849849
match self {
850-
Res::Def(DefKind::Macro(kind), _) => Some(kind),
851-
Res::NonMacroAttr(..) => Some(MacroKind::Attr),
850+
Res::Def(DefKind::Macro(kinds), _) => Some(kinds),
851+
Res::NonMacroAttr(..) => Some(MacroKinds::ATTR),
852852
_ => None,
853853
}
854854
}

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_data_structures::tagged_ptr::TaggedRef;
2020
use rustc_index::IndexVec;
2121
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
2222
use rustc_span::def_id::LocalDefId;
23-
use rustc_span::hygiene::MacroKind;
23+
use rustc_span::hygiene::MacroKinds;
2424
use rustc_span::source_map::Spanned;
2525
use rustc_span::{BytePos, DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
2626
use rustc_target::asm::InlineAsmRegOrRegClass;
@@ -4156,7 +4156,7 @@ impl<'hir> Item<'hir> {
41564156
expect_fn, (Ident, &FnSig<'hir>, &'hir Generics<'hir>, BodyId),
41574157
ItemKind::Fn { ident, sig, generics, body, .. }, (*ident, sig, generics, *body);
41584158

4159-
expect_macro, (Ident, &ast::MacroDef, MacroKind),
4159+
expect_macro, (Ident, &ast::MacroDef, MacroKinds),
41604160
ItemKind::Macro(ident, def, mk), (*ident, def, *mk);
41614161

41624162
expect_mod, (Ident, &'hir Mod<'hir>), ItemKind::Mod(ident, m), (*ident, m);
@@ -4335,7 +4335,7 @@ pub enum ItemKind<'hir> {
43354335
has_body: bool,
43364336
},
43374337
/// A MBE macro definition (`macro_rules!` or `macro`).
4338-
Macro(Ident, &'hir ast::MacroDef, MacroKind),
4338+
Macro(Ident, &'hir ast::MacroDef, MacroKinds),
43394339
/// A module.
43404340
Mod(Ident, &'hir Mod<'hir>),
43414341
/// An external module, e.g. `extern { .. }`.

compiler/rustc_lint/src/non_local_def.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_hir::{Body, HirId, Item, ItemKind, Node, Path, TyKind};
55
use rustc_middle::ty::TyCtxt;
66
use rustc_session::{declare_lint, impl_lint_pass};
77
use rustc_span::def_id::{DefId, LOCAL_CRATE};
8-
use rustc_span::{ExpnKind, MacroKind, Span, kw, sym};
8+
use rustc_span::{ExpnKind, Span, kw, sym};
99

1010
use crate::lints::{NonLocalDefinitionsCargoUpdateNote, NonLocalDefinitionsDiag};
1111
use crate::{LateContext, LateLintPass, LintContext, fluent_generated as fluent};
@@ -240,7 +240,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
240240
},
241241
)
242242
}
243-
ItemKind::Macro(_, _macro, MacroKind::Bang)
243+
ItemKind::Macro(_, _macro, _kinds)
244244
if cx.tcx.has_attr(item.owner_id.def_id, sym::macro_export) =>
245245
{
246246
cx.emit_span_lint(

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1981,7 +1981,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19811981
def_key.disambiguated_data.data = DefPathData::MacroNs(name);
19821982

19831983
let def_id = id.to_def_id();
1984-
self.tables.def_kind.set_some(def_id.index, DefKind::Macro(macro_kind));
1984+
self.tables.def_kind.set_some(def_id.index, DefKind::Macro(macro_kind.into()));
19851985
self.tables.proc_macro.set_some(def_id.index, macro_kind);
19861986
self.encode_attrs(id);
19871987
record!(self.tables.def_keys[def_id] <- def_key);

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use rustc_serialize::opaque::FileEncoder;
3535
use rustc_session::config::{SymbolManglingVersion, TargetModifier};
3636
use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib};
3737
use rustc_span::edition::Edition;
38-
use rustc_span::hygiene::{ExpnIndex, MacroKind, SyntaxContextKey};
38+
use rustc_span::hygiene::{ExpnIndex, MacroKind, MacroKinds, SyntaxContextKey};
3939
use rustc_span::{self, ExpnData, ExpnHash, ExpnId, Ident, Span, Symbol};
4040
use rustc_target::spec::{PanicStrategy, TargetTuple};
4141
use table::TableBuilder;

0 commit comments

Comments
 (0)