Skip to content
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

Fix edition for pat fragments #84452

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 2 additions & 7 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,17 +710,12 @@ pub enum NonterminalKind {
}

impl NonterminalKind {
/// The `edition` closure is used to get the edition for the given symbol. Doing
/// `span.edition()` is expensive, so we do it lazily.
pub fn from_symbol(
symbol: Symbol,
edition: impl FnOnce() -> Edition,
) -> Option<NonterminalKind> {
pub fn from_symbol(symbol: Symbol, edition: Edition) -> Option<NonterminalKind> {
Some(match symbol {
sym::item => NonterminalKind::Item,
sym::block => NonterminalKind::Block,
sym::stmt => NonterminalKind::Stmt,
sym::pat => match edition() {
sym::pat => match edition {
Edition::Edition2015 | Edition::Edition2018 => {
NonterminalKind::Pat2015 { inferred: true }
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ pub fn compile_declarative_macro(
&sess.parse_sess,
def.id,
features,
edition,
)
.pop()
.unwrap();
Expand All @@ -492,6 +493,7 @@ pub fn compile_declarative_macro(
&sess.parse_sess,
def.id,
features,
edition,
)
.pop()
.unwrap();
Expand Down
19 changes: 9 additions & 10 deletions compiler/rustc_expand/src/mbe/quoted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rustc_ast::{NodeId, DUMMY_NODE_ID};
use rustc_ast_pretty::pprust;
use rustc_feature::Features;
use rustc_session::parse::{feature_err, ParseSess};
use rustc_span::edition::Edition;
use rustc_span::symbol::{kw, sym, Ident};

use rustc_span::Span;
Expand Down Expand Up @@ -42,6 +43,7 @@ pub(super) fn parse(
sess: &ParseSess,
node_id: NodeId,
features: &Features,
edition: Edition,
) -> Vec<TokenTree> {
// Will contain the final collection of `self::TokenTree`
let mut result = Vec::new();
Expand All @@ -52,7 +54,7 @@ pub(super) fn parse(
while let Some(tree) = trees.next() {
// Given the parsed tree, if there is a metavar and we are expecting matchers, actually
// parse out the matcher (i.e., in `$id:ident` this would parse the `:` and `ident`).
let tree = parse_tree(tree, &mut trees, expect_matchers, sess, node_id, features);
let tree = parse_tree(tree, &mut trees, expect_matchers, sess, node_id, features, edition);
match tree {
TokenTree::MetaVar(start_sp, ident) if expect_matchers => {
let span = match trees.next() {
Expand All @@ -78,11 +80,8 @@ pub(super) fn parse(
}

let kind =
token::NonterminalKind::from_symbol(frag.name, || {
span.edition()
})
.unwrap_or_else(
|| {
token::NonterminalKind::from_symbol(frag.name, edition)
.unwrap_or_else(|| {
let msg = format!(
"invalid fragment specifier `{}`",
frag.name
Expand All @@ -92,8 +91,7 @@ pub(super) fn parse(
.help(VALID_FRAGMENT_NAMES_MSG)
.emit();
token::NonterminalKind::Ident
},
);
});
result.push(TokenTree::MetaVarDecl(span, ident, Some(kind)));
continue;
}
Expand Down Expand Up @@ -139,6 +137,7 @@ fn parse_tree(
sess: &ParseSess,
node_id: NodeId,
features: &Features,
edition: Edition,
) -> TokenTree {
// Depending on what `tree` is, we could be parsing different parts of a macro
match tree {
Expand Down Expand Up @@ -166,7 +165,7 @@ fn parse_tree(
sess.span_diagnostic.span_err(span.entire(), &msg);
}
// Parse the contents of the sequence itself
let sequence = parse(tts, expect_matchers, sess, node_id, features);
let sequence = parse(tts, expect_matchers, sess, node_id, features, edition);
// Get the Kleene operator and optional separator
let (separator, kleene) =
parse_sep_and_kleene_op(&mut trees, span.entire(), sess);
Expand Down Expand Up @@ -219,7 +218,7 @@ fn parse_tree(
span,
Lrc::new(Delimited {
delim,
tts: parse(tts, expect_matchers, sess, node_id, features),
tts: parse(tts, expect_matchers, sess, node_id, features, edition),
}),
),
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/macros/auxiliary/issue-84429.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[macro_export]
alexcrichton marked this conversation as resolved.
Show resolved Hide resolved
macro_rules! foo {
($x:pat | $y:pat) => {}
}
9 changes: 9 additions & 0 deletions src/test/ui/macros/issue-84429.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// aux-build:issue-84429.rs
// edition:2021
// check-pass

extern crate issue_84429;

fn main() {
issue_84429::foo!(1 | 2);
}