1
Fork 0

Apply code review suggestions

- use feature_err to report unstable expr_2021
- Update downlevel expr_2021 diagnostics

Co-authored-by: León Orell Valerian Liehr <me@fmease.dev>
This commit is contained in:
Eric Holk 2024-04-30 16:32:26 -07:00 committed by Eric Holk
parent a55d06323a
commit f364011955
No known key found for this signature in database
GPG key ID: 8EA6B43ED4CE0911
8 changed files with 57 additions and 38 deletions

View file

@ -909,7 +909,7 @@ impl NonterminalKind {
}, },
sym::pat_param => NonterminalKind::PatParam { inferred: false }, sym::pat_param => NonterminalKind::PatParam { inferred: false },
sym::expr => NonterminalKind::Expr, sym::expr => NonterminalKind::Expr,
sym::expr_2021 if edition() >= Edition::Edition2021 => NonterminalKind::Expr2021, sym::expr_2021 if edition().at_least_rust_2021() => NonterminalKind::Expr2021,
sym::ty => NonterminalKind::Ty, sym::ty => NonterminalKind::Ty,
sym::ident => NonterminalKind::Ident, sym::ident => NonterminalKind::Ident,
sym::lifetime => NonterminalKind::Lifetime, sym::lifetime => NonterminalKind::Lifetime,

View file

@ -39,9 +39,6 @@ expand_explain_doc_comment_inner =
expand_explain_doc_comment_outer = expand_explain_doc_comment_outer =
outer doc comments expand to `#[doc = "..."]`, which is what this macro attempted to match outer doc comments expand to `#[doc = "..."]`, which is what this macro attempted to match
expand_expr_2021_is_experimental =
expr_2021 is experimental
expand_expr_repeat_no_syntax_vars = expand_expr_repeat_no_syntax_vars =
attempted to repeat an expression containing no syntax variables matched as repeating at this depth attempted to repeat an expression containing no syntax variables matched as repeating at this depth

View file

@ -433,10 +433,3 @@ pub struct ExpectedParenOrBrace<'a> {
pub span: Span, pub span: Span,
pub token: Cow<'a, str>, pub token: Cow<'a, str>,
} }
#[derive(Diagnostic)]
#[diag(expand_expr_2021_is_experimental)]
pub struct Expr2021IsExperimental {
#[primary_span]
pub span: Span,
}

View file

@ -16,6 +16,10 @@ use rustc_span::Span;
const VALID_FRAGMENT_NAMES_MSG: &str = "valid fragment specifiers are \ const VALID_FRAGMENT_NAMES_MSG: &str = "valid fragment specifiers are \
`ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, \ `ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, \
`literal`, `path`, `meta`, `tt`, `item` and `vis`"; `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 `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 /// takes a generic `TokenStream`, such as is used in the rest of the compiler, and returns a
@ -63,40 +67,59 @@ pub(super) fn parse(
Some(tokenstream::TokenTree::Token(token, _)) => match token.ident() { Some(tokenstream::TokenTree::Token(token, _)) => match token.ident() {
Some((fragment, _)) => { Some((fragment, _)) => {
let span = token.span.with_lo(start_sp.lo()); 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 = let kind =
token::NonterminalKind::from_symbol(fragment.name, || { token::NonterminalKind::from_symbol(fragment.name, edition)
// FIXME(#85708) - once we properly decode a foreign .unwrap_or_else(|| {
// crate's `SyntaxContext::root`, then we can replace let help = match fragment.name {
// this with just `span.edition()`. A sym::expr_2021 => {
// `SyntaxContext::root()` from the current crate will format!(
// have the edition of the current crate, and a "fragment specifier `expr_2021` \
// `SyntaxContext::root()` from a foreign crate will requires Rust 2021 or later\n\
// have the edition of that crate (which we manually {VALID_FRAGMENT_NAMES_MSG}"
// retrieve via the `edition` parameter). )
if !span.from_expansion() { }
edition _ if edition().at_least_rust_2021()
} else { && features
span.edition() .expr_fragment_specifier_2024 =>
} {
}) VALID_FRAGMENT_NAMES_MSG_2021.into()
.unwrap_or_else( }
|| { _ => VALID_FRAGMENT_NAMES_MSG.into(),
};
sess.dcx().emit_err( sess.dcx().emit_err(
errors::InvalidFragmentSpecifier { errors::InvalidFragmentSpecifier {
span, span,
fragment, fragment,
help: VALID_FRAGMENT_NAMES_MSG.into(), help,
}, },
); );
token::NonterminalKind::Ident token::NonterminalKind::Ident
}, });
);
if kind == token::NonterminalKind::Expr2021 if kind == token::NonterminalKind::Expr2021
&& !features.expr_fragment_specifier_2024 && !features.expr_fragment_specifier_2024
{ {
sess.dcx() rustc_session::parse::feature_err(
.emit_err(errors::Expr2021IsExperimental { span }); sess,
sym::expr_fragment_specifier_2024,
span,
"fragment specifier `expr_2021` is unstable",
)
.emit();
} }
result.push(TokenTree::MetaVarDecl(span, ident, Some(kind))); result.push(TokenTree::MetaVarDecl(span, ident, Some(kind)));
continue; continue;

View file

@ -1,6 +1,6 @@
//@ compile-flags: --edition=2018 //@ 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 { macro_rules! m {
($e:expr_2021) => { //~ ERROR: invalid fragment specifier `expr_2021` ($e:expr_2021) => { //~ ERROR: invalid fragment specifier `expr_2021`

View file

@ -4,7 +4,8 @@ error: invalid fragment specifier `expr_2021`
LL | ($e: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 `(` error: no rules expected the token `(`
--> $DIR/expr_2021_old_edition.rs:12:8 --> $DIR/expr_2021_old_edition.rs:12:8

View file

@ -1,7 +1,7 @@
//@ compile-flags: --edition=2024 -Z unstable-options //@ compile-flags: --edition=2024 -Z unstable-options
macro_rules! m { macro_rules! m {
($e:expr_2021) => { //~ ERROR: expr_2021 is experimental ($e:expr_2021) => { //~ ERROR: fragment specifier `expr_2021` is unstable
$e $e
}; };
} }

View file

@ -1,8 +1,13 @@
error: expr_2021 is experimental error[E0658]: fragment specifier `expr_2021` is unstable
--> $DIR/feature-gate-expr_fragment_specifier_2024.rs:4:6 --> $DIR/feature-gate-expr_fragment_specifier_2024.rs:4:6
| |
LL | ($e:expr_2021) => { LL | ($e:expr_2021) => {
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
|
= note: see issue #123742 <https://github.com/rust-lang/rust/issues/123742> for more information
= help: add `#![feature(expr_fragment_specifier_2024)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 1 previous error error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0658`.