Skip to content

Handle macros with multiple kinds, and improve errors #145153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3889,6 +3889,7 @@ dependencies = [
name = "rustc_hir"
version = "0.0.0"
dependencies = [
"bitflags",
"odht",
"rustc_abi",
"rustc_arena",
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,14 +464,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
let body = Box::new(self.lower_delim_args(body));
let def_id = self.local_def_id(id);
let def_kind = self.tcx.def_kind(def_id);
let DefKind::Macro(macro_kind) = def_kind else {
let DefKind::Macro(macro_kinds) = def_kind else {
unreachable!(
"expected DefKind::Macro for macro item, found {}",
def_kind.descr(def_id.to_def_id())
);
};
let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules });
hir::ItemKind::Macro(ident, macro_def, macro_kind)
hir::ItemKind::Macro(ident, macro_def, macro_kinds)
}
ItemKind::Delegation(box delegation) => {
let delegation_results = self.lower_delegation(delegation, id, false);
Expand Down
48 changes: 42 additions & 6 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed, PResult};
use rustc_feature::Features;
use rustc_hir as hir;
use rustc_hir::attrs::{AttributeKind, CfgEntry, Deprecation};
use rustc_hir::def::MacroKinds;
use rustc_hir::{Stability, find_attr};
use rustc_lint_defs::{BufferedEarlyLint, RegisteredTools};
use rustc_parse::MACRO_ARGUMENTS;
Expand Down Expand Up @@ -718,6 +719,9 @@ impl MacResult for DummyResult {
/// A syntax extension kind.
#[derive(Clone)]
pub enum SyntaxExtensionKind {
/// A `macro_rules!` macro that can work as any `MacroKind`
MacroRules(Arc<crate::MacroRulesMacroExpander>),

/// A token-based function-like macro.
Bang(
/// An expander with signature TokenStream -> TokenStream.
Expand Down Expand Up @@ -772,9 +776,39 @@ pub enum SyntaxExtensionKind {
),

/// A glob delegation.
///
/// This is for delegated function implementations, and has nothing to do with glob imports.
GlobDelegation(Arc<dyn GlobDelegationExpander + sync::DynSync + sync::DynSend>),
}

impl SyntaxExtensionKind {
/// Returns `Some(expander)` for a macro usable as a `LegacyBang`; otherwise returns `None`
///
/// This includes a `MacroRules` with function-like rules.
pub fn as_legacy_bang(&self) -> Option<&(dyn TTMacroExpander + sync::DynSync + sync::DynSend)> {
match self {
SyntaxExtensionKind::LegacyBang(exp) => Some(exp.as_ref()),
SyntaxExtensionKind::MacroRules(exp) if exp.kinds().contains(MacroKinds::BANG) => {
Some(exp.as_ref())
}
_ => None,
}
}

/// Returns `Some(expander)` for a macro usable as an `Attr`; otherwise returns `None`
///
/// This includes a `MacroRules` with `attr` rules.
pub fn as_attr(&self) -> Option<&(dyn AttrProcMacro + sync::DynSync + sync::DynSend)> {
match self {
SyntaxExtensionKind::Attr(exp) => Some(exp.as_ref()),
SyntaxExtensionKind::MacroRules(exp) if exp.kinds().contains(MacroKinds::ATTR) => {
Some(exp.as_ref())
}
_ => None,
}
}
}

/// A struct representing a macro definition in "lowered" form ready for expansion.
pub struct SyntaxExtension {
/// A syntax extension kind.
Expand Down Expand Up @@ -804,18 +838,19 @@ pub struct SyntaxExtension {
}

impl SyntaxExtension {
/// Returns which kind of macro calls this syntax extension.
pub fn macro_kind(&self) -> MacroKind {
/// Returns which kinds of macro call this syntax extension.
pub fn macro_kinds(&self) -> MacroKinds {
match self.kind {
SyntaxExtensionKind::Bang(..)
| SyntaxExtensionKind::LegacyBang(..)
| SyntaxExtensionKind::GlobDelegation(..) => MacroKind::Bang,
| SyntaxExtensionKind::GlobDelegation(..) => MacroKinds::BANG,
SyntaxExtensionKind::Attr(..)
| SyntaxExtensionKind::LegacyAttr(..)
| SyntaxExtensionKind::NonMacroAttr => MacroKind::Attr,
| SyntaxExtensionKind::NonMacroAttr => MacroKinds::ATTR,
SyntaxExtensionKind::Derive(..) | SyntaxExtensionKind::LegacyDerive(..) => {
MacroKind::Derive
MacroKinds::DERIVE
}
SyntaxExtensionKind::MacroRules(ref m) => m.kinds(),
}
}

Expand Down Expand Up @@ -1027,11 +1062,12 @@ impl SyntaxExtension {
parent: LocalExpnId,
call_site: Span,
descr: Symbol,
kind: MacroKind,
macro_def_id: Option<DefId>,
parent_module: Option<DefId>,
) -> ExpnData {
ExpnData::new(
ExpnKind::Macro(self.macro_kind(), descr),
ExpnKind::Macro(kind, descr),
parent.to_expn_id(),
call_site,
self.span,
Expand Down
27 changes: 13 additions & 14 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {

let (fragment_kind, span) = (invoc.fragment_kind, invoc.span());
ExpandResult::Ready(match invoc.kind {
InvocationKind::Bang { mac, span } => match ext {
SyntaxExtensionKind::Bang(expander) => {
InvocationKind::Bang { mac, span } => {
if let SyntaxExtensionKind::Bang(expander) = ext {
match expander.expand(self.cx, span, mac.args.tokens.clone()) {
Ok(tok_result) => {
let fragment =
Expand All @@ -755,8 +755,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
}
Err(guar) => return ExpandResult::Ready(fragment_kind.dummy(span, guar)),
}
}
SyntaxExtensionKind::LegacyBang(expander) => {
} else if let Some(expander) = ext.as_legacy_bang() {
let tok_result = match expander.expand(self.cx, span, mac.args.tokens.clone()) {
ExpandResult::Ready(tok_result) => tok_result,
ExpandResult::Retry(_) => {
Expand All @@ -776,11 +775,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
let guar = self.error_wrong_fragment_kind(fragment_kind, &mac, span);
fragment_kind.dummy(span, guar)
}
} else {
unreachable!();
}
_ => unreachable!(),
},
InvocationKind::Attr { attr, pos, mut item, derives } => match ext {
SyntaxExtensionKind::Attr(expander) => {
}
InvocationKind::Attr { attr, pos, mut item, derives } => {
if let Some(expander) = ext.as_attr() {
self.gate_proc_macro_input(&item);
self.gate_proc_macro_attr_item(span, &item);
let tokens = match &item {
Expand Down Expand Up @@ -835,8 +835,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
}
Err(guar) => return ExpandResult::Ready(fragment_kind.dummy(span, guar)),
}
}
SyntaxExtensionKind::LegacyAttr(expander) => {
} else if let SyntaxExtensionKind::LegacyAttr(expander) = ext {
match validate_attr::parse_meta(&self.cx.sess.psess, &attr) {
Ok(meta) => {
let item_clone = macro_stats.then(|| item.clone());
Expand Down Expand Up @@ -878,15 +877,15 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
fragment_kind.expect_from_annotatables(iter::once(item))
}
}
}
SyntaxExtensionKind::NonMacroAttr => {
} else if let SyntaxExtensionKind::NonMacroAttr = ext {
// `-Zmacro-stats` ignores these because they don't do any real expansion.
self.cx.expanded_inert_attrs.mark(&attr);
item.visit_attrs(|attrs| attrs.insert(pos, attr));
fragment_kind.expect_from_annotatables(iter::once(item))
} else {
unreachable!();
}
_ => unreachable!(),
},
}
InvocationKind::Derive { path, item, is_const } => match ext {
SyntaxExtensionKind::Derive(expander)
| SyntaxExtensionKind::LegacyDerive(expander) => {
Expand Down
12 changes: 0 additions & 12 deletions compiler/rustc_expand/src/mbe/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,6 @@ pub(super) fn failed_to_match_macro(

let Some(BestFailure { token, msg: label, remaining_matcher, .. }) = tracker.best_failure
else {
// FIXME: we should report this at macro resolution time, as we do for
// `resolve_macro_cannot_use_as_attr`. We can do that once we track multiple macro kinds for a
// Def.
if attr_args.is_none() && !rules.iter().any(|rule| matches!(rule, MacroRule::Func { .. })) {
let msg = format!("macro has no rules for function-like invocation `{name}!`");
let mut err = psess.dcx().struct_span_err(sp, msg);
if !def_head_span.is_dummy() {
let msg = "this macro has no rules for function-like invocation";
err.span_label(def_head_span, msg);
}
return (sp, err.emit());
}
return (sp, psess.dcx().span_delayed_bug(sp, "failed to match a macro"));
};

Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_expand/src/mbe/macro_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,10 @@ enum NestedMacroState {
/// The token `macro_rules` was processed.
MacroRules,
/// The tokens `macro_rules!` were processed.
MacroRulesNot,
MacroRulesBang,
/// The tokens `macro_rules!` followed by a name were processed. The name may be either directly
/// an identifier or a meta-variable (that hopefully would be instantiated by an identifier).
MacroRulesNotName,
MacroRulesBangName,
/// The keyword `macro` was processed.
Macro,
/// The keyword `macro` followed by a name was processed.
Expand Down Expand Up @@ -408,24 +408,24 @@ fn check_nested_occurrences(
NestedMacroState::MacroRules,
&TokenTree::Token(Token { kind: TokenKind::Bang, .. }),
) => {
state = NestedMacroState::MacroRulesNot;
state = NestedMacroState::MacroRulesBang;
}
(
NestedMacroState::MacroRulesNot,
NestedMacroState::MacroRulesBang,
&TokenTree::Token(Token { kind: TokenKind::Ident(..), .. }),
) => {
state = NestedMacroState::MacroRulesNotName;
state = NestedMacroState::MacroRulesBangName;
}
(NestedMacroState::MacroRulesNot, &TokenTree::MetaVar(..)) => {
state = NestedMacroState::MacroRulesNotName;
(NestedMacroState::MacroRulesBang, &TokenTree::MetaVar(..)) => {
state = NestedMacroState::MacroRulesBangName;
// We check that the meta-variable is correctly used.
check_occurrences(psess, node_id, tt, macros, binders, ops, guar);
}
(NestedMacroState::MacroRulesNotName, TokenTree::Delimited(.., del))
(NestedMacroState::MacroRulesBangName, TokenTree::Delimited(.., del))
| (NestedMacroState::MacroName, TokenTree::Delimited(.., del))
if del.delim == Delimiter::Brace =>
{
let macro_rules = state == NestedMacroState::MacroRulesNotName;
let macro_rules = state == NestedMacroState::MacroRulesBangName;
state = NestedMacroState::Empty;
let rest =
check_nested_macro(psess, node_id, macro_rules, &del.tts, &nested_macros, guar);
Expand Down
26 changes: 15 additions & 11 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan};
use rustc_feature::Features;
use rustc_hir as hir;
use rustc_hir::attrs::AttributeKind;
use rustc_hir::def::MacroKinds;
use rustc_hir::find_attr;
use rustc_lint_defs::BuiltinLintDiag;
use rustc_lint_defs::builtin::{
Expand Down Expand Up @@ -144,6 +145,7 @@ pub struct MacroRulesMacroExpander {
name: Ident,
span: Span,
transparency: Transparency,
kinds: MacroKinds,
rules: Vec<MacroRule>,
}

Expand All @@ -158,6 +160,10 @@ impl MacroRulesMacroExpander {
};
if has_compile_error_macro(rhs) { None } else { Some((&self.name, span)) }
}

pub fn kinds(&self) -> MacroKinds {
self.kinds
}
}

impl TTMacroExpander for MacroRulesMacroExpander {
Expand Down Expand Up @@ -540,13 +546,13 @@ pub fn compile_declarative_macro(
span: Span,
node_id: NodeId,
edition: Edition,
) -> (SyntaxExtension, Option<Arc<SyntaxExtension>>, usize) {
) -> (SyntaxExtension, usize) {
let mk_syn_ext = |kind| {
let is_local = is_defined_in_current_crate(node_id);
SyntaxExtension::new(sess, kind, span, Vec::new(), edition, ident.name, attrs, is_local)
};
let mk_bang_ext = |expander| mk_syn_ext(SyntaxExtensionKind::LegacyBang(expander));
let dummy_syn_ext = |guar| (mk_bang_ext(Arc::new(DummyExpander(guar))), None, 0);
let dummy_syn_ext =
|guar| (mk_syn_ext(SyntaxExtensionKind::LegacyBang(Arc::new(DummyExpander(guar)))), 0);

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

let mut has_attr_rules = false;
let mut kinds = MacroKinds::empty();
let mut rules = Vec::new();

while p.token != token::Eof {
let args = if p.eat_keyword_noexpect(sym::attr) {
has_attr_rules = true;
kinds |= MacroKinds::ATTR;
if !features.macro_attr() {
feature_err(sess, sym::macro_attr, span, "`macro_rules!` attributes are unstable")
.emit();
Expand All @@ -581,6 +587,7 @@ pub fn compile_declarative_macro(
}
Some(args)
} else {
kinds |= MacroKinds::BANG;
None
};
let lhs_tt = p.parse_token_tree();
Expand Down Expand Up @@ -627,6 +634,7 @@ pub fn compile_declarative_macro(
let guar = sess.dcx().span_err(span, "macros must contain at least one rule");
return dummy_syn_ext(guar);
}
assert!(!kinds.is_empty());

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

let exp = Arc::new(MacroRulesMacroExpander { name: ident, span, node_id, transparency, rules });
let opt_attr_ext = has_attr_rules.then(|| {
let exp = Arc::clone(&exp);
Arc::new(mk_syn_ext(SyntaxExtensionKind::Attr(exp)))
});
(mk_bang_ext(exp), opt_attr_ext, nrules)
let exp = MacroRulesMacroExpander { name: ident, kinds, span, node_id, transparency, rules };
(mk_syn_ext(SyntaxExtensionKind::MacroRules(Arc::new(exp))), nrules)
}

fn check_no_eof(sess: &Session, p: &Parser<'_>, msg: &'static str) -> Option<ErrorGuaranteed> {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2024"

[dependencies]
# tidy-alphabetical-start
bitflags = "2.9.1"
odht = { version = "0.3.1", features = ["nightly"] }
rustc_abi = { path = "../rustc_abi" }
rustc_arena = { path = "../rustc_arena" }
Expand Down
Loading
Loading