Skip to content

Commit

Permalink
Auto merge of rust-lang#17535 - Veykril:macro-def-syn, r=Veykril
Browse files Browse the repository at this point in the history
fix: Fix up the syntax tree for macro 2.0

Fixes rust-lang#10266

This change is trivial now that we don't have a token map anymore
  • Loading branch information
bors committed Jul 3, 2024
2 parents 848e0c4 + 013b6a8 commit 39e6a8e
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 161 deletions.
25 changes: 20 additions & 5 deletions crates/hir-expand/src/declarative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,30 @@ impl DeclarativeMacroExpander {
),
ast::Macro::MacroDef(macro_def) => (
match macro_def.body() {
Some(arg) => {
let tt = mbe::syntax_node_to_token_tree(
arg.syntax(),
Some(body) => {
let span =
map.span_for_range(macro_def.macro_token().unwrap().text_range());
let args = macro_def.args().map(|args| {
mbe::syntax_node_to_token_tree(
args.syntax(),
map.as_ref(),
span,
DocCommentDesugarMode::Mbe,
)
});
let body = mbe::syntax_node_to_token_tree(
body.syntax(),
map.as_ref(),
map.span_for_range(macro_def.macro_token().unwrap().text_range()),
span,
DocCommentDesugarMode::Mbe,
);

mbe::DeclarativeMacro::parse_macro2(&tt, edition, new_meta_vars)
mbe::DeclarativeMacro::parse_macro2(
args.as_ref(),
&body,
edition,
new_meta_vars,
)
}
None => mbe::DeclarativeMacro::from_err(mbe::ParseError::Expected(
"expected a token tree".into(),
Expand Down
45 changes: 22 additions & 23 deletions crates/mbe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl DeclarativeMacro {
let mut err = None;

while src.len() > 0 {
let rule = match Rule::parse(edition, &mut src, true, new_meta_vars) {
let rule = match Rule::parse(edition, &mut src, new_meta_vars) {
Ok(it) => it,
Err(e) => {
err = Some(Box::new(e));
Expand Down Expand Up @@ -184,19 +184,34 @@ impl DeclarativeMacro {

/// The new, unstable `macro m {}` flavor.
pub fn parse_macro2(
tt: &tt::Subtree<Span>,
args: Option<&tt::Subtree<Span>>,
body: &tt::Subtree<Span>,
edition: impl Copy + Fn(SyntaxContextId) -> Edition,
// FIXME: Remove this once we drop support for rust 1.76 (defaults to true then)
new_meta_vars: bool,
) -> DeclarativeMacro {
let mut src = TtIter::new(tt);
let mut rules = Vec::new();
let mut err = None;

if tt::DelimiterKind::Brace == tt.delimiter.kind {
if let Some(args) = args {
cov_mark::hit!(parse_macro_def_simple);

let rule = (|| {
let lhs = MetaTemplate::parse_pattern(edition, args)?;
let rhs = MetaTemplate::parse_template(edition, body, new_meta_vars)?;

Ok(crate::Rule { lhs, rhs })
})();

match rule {
Ok(rule) => rules.push(rule),
Err(e) => err = Some(Box::new(e)),
}
} else {
cov_mark::hit!(parse_macro_def_rules);
let mut src = TtIter::new(body);
while src.len() > 0 {
let rule = match Rule::parse(edition, &mut src, true, new_meta_vars) {
let rule = match Rule::parse(edition, &mut src, new_meta_vars) {
Ok(it) => it,
Err(e) => {
err = Some(Box::new(e));
Expand All @@ -213,19 +228,6 @@ impl DeclarativeMacro {
break;
}
}
} else {
cov_mark::hit!(parse_macro_def_simple);
match Rule::parse(edition, &mut src, false, new_meta_vars) {
Ok(rule) => {
if src.len() != 0 {
err = Some(Box::new(ParseError::expected("remaining tokens in macro def")));
}
rules.push(rule);
}
Err(e) => {
err = Some(Box::new(e));
}
}
}

for Rule { lhs, .. } in &rules {
Expand Down Expand Up @@ -262,14 +264,11 @@ impl Rule {
fn parse(
edition: impl Copy + Fn(SyntaxContextId) -> Edition,
src: &mut TtIter<'_, Span>,
expect_arrow: bool,
new_meta_vars: bool,
) -> Result<Self, ParseError> {
let lhs = src.expect_subtree().map_err(|()| ParseError::expected("expected subtree"))?;
if expect_arrow {
src.expect_char('=').map_err(|()| ParseError::expected("expected `=`"))?;
src.expect_char('>').map_err(|()| ParseError::expected("expected `>`"))?;
}
src.expect_char('=').map_err(|()| ParseError::expected("expected `=`"))?;
src.expect_char('>').map_err(|()| ParseError::expected("expected `>`"))?;
let rhs = src.expect_subtree().map_err(|()| ParseError::expected("expected subtree"))?;

let lhs = MetaTemplate::parse_pattern(edition, lhs)?;
Expand Down
2 changes: 0 additions & 2 deletions crates/parser/src/grammar/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,11 @@ fn macro_def(p: &mut Parser<'_>, m: Marker) {
// macro m { ($i:ident) => {} }
token_tree(p);
} else if p.at(T!['(']) {
let m = p.start();
token_tree(p);
match p.current() {
T!['{'] | T!['['] | T!['('] => token_tree(p),
_ => p.error("expected `{`, `[`, `(`"),
}
m.complete(p, TOKEN_TREE);
} else {
p.error("unmatched `(`");
}
Expand Down
21 changes: 10 additions & 11 deletions crates/parser/test_data/parser/inline/ok/0147_macro_def.rast
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ SOURCE_FILE
NAME
IDENT "m"
TOKEN_TREE
TOKEN_TREE
L_PAREN "("
DOLLAR "$"
IDENT "i"
COLON ":"
IDENT "ident"
R_PAREN ")"
WHITESPACE " "
TOKEN_TREE
L_CURLY "{"
R_CURLY "}"
L_PAREN "("
DOLLAR "$"
IDENT "i"
COLON ":"
IDENT "ident"
R_PAREN ")"
WHITESPACE " "
TOKEN_TREE
L_CURLY "{"
R_CURLY "}"
WHITESPACE "\n"
19 changes: 9 additions & 10 deletions crates/parser/test_data/parser/ok/0012_visibility.rast
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ SOURCE_FILE
NAME
IDENT "m"
TOKEN_TREE
TOKEN_TREE
L_PAREN "("
DOLLAR "$"
COLON ":"
IDENT "ident"
R_PAREN ")"
WHITESPACE " "
TOKEN_TREE
L_CURLY "{"
R_CURLY "}"
L_PAREN "("
DOLLAR "$"
COLON ":"
IDENT "ident"
R_PAREN ")"
WHITESPACE " "
TOKEN_TREE
L_CURLY "{"
R_CURLY "}"
WHITESPACE "\n"
FN
VISIBILITY
Expand Down
Loading

0 comments on commit 39e6a8e

Please sign in to comment.