Skip to content

Commit

Permalink
Update downlevel expr_2021 diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
eholk committed May 1, 2024
1 parent 50a46b9 commit acbc58c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
60 changes: 39 additions & 21 deletions compiler/rustc_expand/src/mbe/quoted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ use rustc_span::Span;
const VALID_FRAGMENT_NAMES_MSG: &str = "valid fragment specifiers are \
`ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, \
`literal`, `path`, `meta`, `tt`, `item` and `vis`";
const VALID_FRAGMENT_NAMES_MSG_2021: &str = "valid fragment specifiers are \
`ident`, `block`, `stmt`, `expr`, `expr_2021`, `pat`, \
`ty`, `lifetime`, `literal`, `path`, `meta`, `tt`, \
`item` and `vis`";

/// Takes a `tokenstream::TokenStream` and returns a `Vec<self::TokenTree>`. Specifically, this
/// takes a generic `TokenStream`, such as is used in the rest of the compiler, and returns a
Expand Down Expand Up @@ -63,35 +67,49 @@ pub(super) fn parse(
Some(tokenstream::TokenTree::Token(token, _)) => match token.ident() {
Some((fragment, _)) => {
let span = token.span.with_lo(start_sp.lo());

let 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
// `SyntaxContext::root()` from a foreign crate will
// have the edition of that crate (which we manually
// retrieve via the `edition` parameter).
if !span.from_expansion() {
edition
} else {
span.edition()
}
};
let kind =
token::NonterminalKind::from_symbol(fragment.name, || {
// 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
// `SyntaxContext::root()` from a foreign crate will
// have the edition of that crate (which we manually
// retrieve via the `edition` parameter).
if !span.from_expansion() {
edition
} else {
span.edition()
}
})
.unwrap_or_else(
|| {
token::NonterminalKind::from_symbol(fragment.name, edition)
.unwrap_or_else(|| {
let help = match fragment.name {
sym::expr_2021 => {
format!(
"fragment specifier `expr_2021` \
requires Rust 2021 or later\n\
{VALID_FRAGMENT_NAMES_MSG}"
)
}
_ if edition().at_least_rust_2021()
&& features
.expr_fragment_specifier_2024 =>
{
VALID_FRAGMENT_NAMES_MSG_2021.into()
}
_ => VALID_FRAGMENT_NAMES_MSG.into(),
};
sess.dcx().emit_err(
errors::InvalidFragmentSpecifier {
span,
fragment,
help: VALID_FRAGMENT_NAMES_MSG.into(),
help,
},
);
token::NonterminalKind::Ident
},
);
});
if kind == token::NonterminalKind::Expr2021
&& !features.expr_fragment_specifier_2024
{
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/expr_2021_old_edition.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ compile-flags: --edition=2018

// This test ensures that expr_2021 is not allowed on pre-2024 editions
// This test ensures that expr_2021 is not allowed on pre-2021 editions

macro_rules! m {
($e:expr_2021) => { //~ ERROR: invalid fragment specifier `expr_2021`
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/macros/expr_2021_old_edition.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ error: invalid fragment specifier `expr_2021`
LL | ($e:expr_2021) => {
| ^^^^^^^^^^^^
|
= help: valid fragment specifiers are `ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, `literal`, `path`, `meta`, `tt`, `item` and `vis`
= help: fragment specifier `expr_2021` requires Rust 2021 or later
valid fragment specifiers are `ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, `literal`, `path`, `meta`, `tt`, `item` and `vis`

error: no rules expected the token `(`
--> $DIR/expr_2021_old_edition.rs:12:8
Expand Down

0 comments on commit acbc58c

Please sign in to comment.