Skip to content
Open
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
32 changes: 12 additions & 20 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::expand::{AstFragment, AstFragmentKind, ensure_complete_parse, parse_a
use crate::mbe::macro_check::check_meta_variables;
use crate::mbe::macro_parser::{Error, ErrorReported, Failure, MatcherLoc, Success, TtParser};
use crate::mbe::quoted::{RulePart, parse_one_tt};
use crate::mbe::transcribe::transcribe;
use crate::mbe::transcribe::{MacroRhs, transcribe};
use crate::mbe::{self, KleeneOp};

pub(crate) struct ParserAnyMacro<'a, 'b> {
Expand Down Expand Up @@ -148,18 +148,18 @@ impl<'a, 'b> ParserAnyMacro<'a, 'b> {

pub(crate) enum MacroRule {
/// A function-style rule, for use with `m!()`
Func { lhs: Vec<MatcherLoc>, lhs_span: Span, rhs: mbe::TokenTree },
Func { lhs: Vec<MatcherLoc>, lhs_span: Span, rhs: MacroRhs },
/// An attr rule, for use with `#[m]`
Attr {
unsafe_rule: bool,
args: Vec<MatcherLoc>,
args_span: Span,
body: Vec<MatcherLoc>,
body_span: Span,
rhs: mbe::TokenTree,
rhs: MacroRhs,
},
/// A derive rule, for use with `#[m]`
Derive { body: Vec<MatcherLoc>, body_span: Span, rhs: mbe::TokenTree },
Derive { body: Vec<MatcherLoc>, body_span: Span, rhs: MacroRhs },
}

pub struct MacroRulesMacroExpander {
Expand All @@ -183,7 +183,7 @@ impl MacroRulesMacroExpander {
}
MacroRule::Derive { body_span, ref rhs, .. } => (MultiSpan::from_span(body_span), rhs),
};
if has_compile_error_macro(rhs) { None } else { Some((&self.name, span)) }
if has_compile_error_macro(&rhs.tt) { None } else { Some((&self.name, span)) }
}

pub fn kinds(&self) -> MacroKinds {
Expand Down Expand Up @@ -220,12 +220,9 @@ impl MacroRulesMacroExpander {
let MacroRule::Derive { rhs, .. } = rule else {
panic!("try_match_macro_derive returned non-derive rule");
};
let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
cx.dcx().span_bug(sp, "malformed macro derive rhs");
};

let id = cx.current_expansion.id;
let tts = transcribe(psess, &named_matches, rhs, *rhs_span, self.transparency, id)
let tts = transcribe(psess, &named_matches, rhs, self.transparency, id)
.map_err(|e| e.emit())?
.to_token_stream();

Expand Down Expand Up @@ -399,14 +396,11 @@ fn expand_macro<'cx, 'a: 'cx>(
let MacroRule::Func { lhs, rhs, .. } = rule else {
panic!("try_match_macro returned non-func rule");
};
let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
cx.dcx().span_bug(sp, "malformed macro rhs");
};
let arm_span = rhs_span.entire();
let arm_span = rhs.tt.span();

// rhs has holes ( `$id` and `$(...)` that need filled)
let id = cx.current_expansion.id;
let flat = match transcribe(psess, &named_matches, rhs, *rhs_span, transparency, id) {
let flat = match transcribe(psess, &named_matches, rhs, transparency, id) {
Ok(flat) => flat,
Err(err) => {
let guar = err.emit();
Expand Down Expand Up @@ -484,9 +478,6 @@ fn expand_macro_attr(
let MacroRule::Attr { rhs, unsafe_rule, .. } = rule else {
panic!("try_macro_match_attr returned non-attr rule");
};
let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
cx.dcx().span_bug(sp, "malformed macro rhs");
};

match (safety, unsafe_rule) {
(Safety::Default, false) | (Safety::Unsafe(_), true) => {}
Expand All @@ -502,7 +493,7 @@ fn expand_macro_attr(
}

let id = cx.current_expansion.id;
let tts = transcribe(psess, &named_matches, rhs, *rhs_span, transparency, id)
let tts = transcribe(psess, &named_matches, rhs, transparency, id)
.map_err(|e| e.emit())?
.to_token_stream();

Expand Down Expand Up @@ -838,11 +829,12 @@ pub fn compile_declarative_macro(
};
let args = mbe::macro_parser::compute_locs(&delimited.tts);
let body_span = lhs_span;
let rhs = MacroRhs::new(rhs);
rules.push(MacroRule::Attr { unsafe_rule, args, args_span, body: lhs, body_span, rhs });
} else if is_derive {
rules.push(MacroRule::Derive { body: lhs, body_span: lhs_span, rhs });
rules.push(MacroRule::Derive { body: lhs, body_span: lhs_span, rhs: MacroRhs::new(rhs) });
} else {
rules.push(MacroRule::Func { lhs, lhs_span, rhs });
rules.push(MacroRule::Func { lhs, lhs_span, rhs: MacroRhs::new(rhs) });
}
if p.token == token::Eof {
break;
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_expand/src/mbe/metavar_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub(crate) const RAW_IDENT_ERR: &str = "`${concat(..)}` currently does not suppo
pub(crate) const UNSUPPORTED_CONCAT_ELEM_ERR: &str = "expected identifier or string literal";

/// A meta-variable expression, for expansions based on properties of meta-variables.
#[derive(Debug, PartialEq, Encodable, Decodable)]
#[derive(Clone, Debug, PartialEq, Encodable, Decodable)]
pub(crate) enum MetaVarExpr {
/// Unification of two or more identifiers.
Concat(Box<[MetaVarExprConcatElem]>),
Expand Down Expand Up @@ -157,7 +157,7 @@ fn iter_span(iter: &TokenStreamIter<'_>) -> Option<Span> {

/// Indicates what is placed in a `concat` parameter. For example, literals
/// (`${concat("foo", "bar")}`) or adhoc identifiers (`${concat(foo, bar)}`).
#[derive(Debug, Decodable, Encodable, PartialEq)]
#[derive(Clone, Debug, Decodable, Encodable, PartialEq)]
pub(crate) enum MetaVarExprConcatElem {
/// Identifier WITHOUT a preceding dollar sign, which means that this identifier should be
/// interpreted as a literal.
Expand Down
Loading
Loading