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

Use correct edition when parsing :pat matchers #85709

Merged
merged 1 commit into from
May 30, 2021
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
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
27 changes: 22 additions & 5 deletions compiler/rustc_expand/src/mbe/quoted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use rustc_feature::Features;
use rustc_session::parse::ParseSess;
use rustc_span::symbol::{kw, Ident};

use rustc_span::Span;
use rustc_span::edition::Edition;
use rustc_span::{Span, SyntaxContext};

use rustc_data_structures::sync::Lrc;

Expand All @@ -32,6 +33,7 @@ const VALID_FRAGMENT_NAMES_MSG: &str = "valid fragment specifiers are \
/// - `sess`: the parsing session. Any errors will be emitted to this session.
/// - `node_id`: the NodeId of the macro we are parsing.
/// - `features`: language features so we can do feature gating.
/// - `edition`: the edition of the crate defining the macro
///
/// # Returns
///
Expand All @@ -42,6 +44,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 +55,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 @@ -64,7 +67,19 @@ pub(super) fn parse(

let kind =
token::NonterminalKind::from_symbol(frag.name, || {
span.edition()
// FIXME(#85708) - once we properly decode a foreign
// crate's `SyntaxContext::root`, then we can replace
// this with just `span.edition()`. A
// `SyntaxContext::root()` from the current crate will
// have the edition of the current crate, and a
// `SyntaxxContext::root()` from a foreign crate will
// have the edition of that crate (which we manually
// retrieve via the `edition` parameter).
if span.ctxt() == SyntaxContext::root() {
edition
} else {
span.edition()
}
})
.unwrap_or_else(
|| {
Expand Down Expand Up @@ -117,13 +132,15 @@ pub(super) fn parse(
/// - `expect_matchers`: same as for `parse` (see above).
/// - `sess`: the parsing session. Any errors will be emitted to this session.
/// - `features`: language features so we can do feature gating.
/// - `edition` - the edition of the crate defining the macro
fn parse_tree(
tree: tokenstream::TokenTree,
outer_trees: &mut impl Iterator<Item = tokenstream::TokenTree>,
expect_matchers: bool,
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 @@ -151,7 +168,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 @@ -204,7 +221,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
11 changes: 11 additions & 0 deletions src/test/ui/macros/auxiliary/foreign-crate-macro-pat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// edition:2018

#[macro_export]
macro_rules! custom_matches {
($expression:expr, $( $pattern:pat )|+ $( if $guard: expr )? $(,)?) => {
match $expression {
$( $pattern )|+ $( if $guard )? => true,
_ => false
}
}
}
12 changes: 12 additions & 0 deletions src/test/ui/macros/cross-crate-pat-span.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// edition:2021
// check-pass
// aux-build: foreign-crate-macro-pat.rs
//
// Tests that the edition of the foreign crate is used
// when determining the behavior of the `:pat` matcher.

extern crate foreign_crate_macro_pat;

fn main() {
let _b = foreign_crate_macro_pat::custom_matches!(b'3', b'0' ..= b'9');
}
9 changes: 9 additions & 0 deletions src/test/ui/macros/issue-84429-matches-edition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// edition:2021
// check-pass
//
// Regression test for issue #84429
// Tests that we can properly invoke `matches!` from a 2021-edition crate.

fn main() {
let _b = matches!(b'3', b'0' ..= b'9');
}