Skip to content

Commit f364011

Browse files
eholkfmease
andcommitted
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 <[email protected]>
1 parent a55d063 commit f364011

8 files changed

+57
-38
lines changed

compiler/rustc_ast/src/token.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ impl NonterminalKind {
909909
},
910910
sym::pat_param => NonterminalKind::PatParam { inferred: false },
911911
sym::expr => NonterminalKind::Expr,
912-
sym::expr_2021 if edition() >= Edition::Edition2021 => NonterminalKind::Expr2021,
912+
sym::expr_2021 if edition().at_least_rust_2021() => NonterminalKind::Expr2021,
913913
sym::ty => NonterminalKind::Ty,
914914
sym::ident => NonterminalKind::Ident,
915915
sym::lifetime => NonterminalKind::Lifetime,

compiler/rustc_expand/messages.ftl

-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ expand_explain_doc_comment_inner =
3939
expand_explain_doc_comment_outer =
4040
outer doc comments expand to `#[doc = "..."]`, which is what this macro attempted to match
4141
42-
expand_expr_2021_is_experimental =
43-
expr_2021 is experimental
44-
4542
expand_expr_repeat_no_syntax_vars =
4643
attempted to repeat an expression containing no syntax variables matched as repeating at this depth
4744

compiler/rustc_expand/src/errors.rs

-7
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,3 @@ pub struct ExpectedParenOrBrace<'a> {
433433
pub span: Span,
434434
pub token: Cow<'a, str>,
435435
}
436-
437-
#[derive(Diagnostic)]
438-
#[diag(expand_expr_2021_is_experimental)]
439-
pub struct Expr2021IsExperimental {
440-
#[primary_span]
441-
pub span: Span,
442-
}

compiler/rustc_expand/src/mbe/quoted.rs

+46-23
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ use rustc_span::Span;
1616
const VALID_FRAGMENT_NAMES_MSG: &str = "valid fragment specifiers are \
1717
`ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, \
1818
`literal`, `path`, `meta`, `tt`, `item` and `vis`";
19+
const VALID_FRAGMENT_NAMES_MSG_2021: &str = "valid fragment specifiers are \
20+
`ident`, `block`, `stmt`, `expr`, `expr_2021`, `pat`, \
21+
`ty`, `lifetime`, `literal`, `path`, `meta`, `tt`, \
22+
`item` and `vis`";
1923

2024
/// Takes a `tokenstream::TokenStream` and returns a `Vec<self::TokenTree>`. Specifically, this
2125
/// 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(
6367
Some(tokenstream::TokenTree::Token(token, _)) => match token.ident() {
6468
Some((fragment, _)) => {
6569
let span = token.span.with_lo(start_sp.lo());
66-
70+
let edition = || {
71+
// FIXME(#85708) - once we properly decode a foreign
72+
// crate's `SyntaxContext::root`, then we can replace
73+
// this with just `span.edition()`. A
74+
// `SyntaxContext::root()` from the current crate will
75+
// have the edition of the current crate, and a
76+
// `SyntaxContext::root()` from a foreign crate will
77+
// have the edition of that crate (which we manually
78+
// retrieve via the `edition` parameter).
79+
if !span.from_expansion() {
80+
edition
81+
} else {
82+
span.edition()
83+
}
84+
};
6785
let kind =
68-
token::NonterminalKind::from_symbol(fragment.name, || {
69-
// FIXME(#85708) - once we properly decode a foreign
70-
// crate's `SyntaxContext::root`, then we can replace
71-
// this with just `span.edition()`. A
72-
// `SyntaxContext::root()` from the current crate will
73-
// have the edition of the current crate, and a
74-
// `SyntaxContext::root()` from a foreign crate will
75-
// have the edition of that crate (which we manually
76-
// retrieve via the `edition` parameter).
77-
if !span.from_expansion() {
78-
edition
79-
} else {
80-
span.edition()
81-
}
82-
})
83-
.unwrap_or_else(
84-
|| {
86+
token::NonterminalKind::from_symbol(fragment.name, edition)
87+
.unwrap_or_else(|| {
88+
let help = match fragment.name {
89+
sym::expr_2021 => {
90+
format!(
91+
"fragment specifier `expr_2021` \
92+
requires Rust 2021 or later\n\
93+
{VALID_FRAGMENT_NAMES_MSG}"
94+
)
95+
}
96+
_ if edition().at_least_rust_2021()
97+
&& features
98+
.expr_fragment_specifier_2024 =>
99+
{
100+
VALID_FRAGMENT_NAMES_MSG_2021.into()
101+
}
102+
_ => VALID_FRAGMENT_NAMES_MSG.into(),
103+
};
85104
sess.dcx().emit_err(
86105
errors::InvalidFragmentSpecifier {
87106
span,
88107
fragment,
89-
help: VALID_FRAGMENT_NAMES_MSG.into(),
108+
help,
90109
},
91110
);
92111
token::NonterminalKind::Ident
93-
},
94-
);
112+
});
95113
if kind == token::NonterminalKind::Expr2021
96114
&& !features.expr_fragment_specifier_2024
97115
{
98-
sess.dcx()
99-
.emit_err(errors::Expr2021IsExperimental { span });
116+
rustc_session::parse::feature_err(
117+
sess,
118+
sym::expr_fragment_specifier_2024,
119+
span,
120+
"fragment specifier `expr_2021` is unstable",
121+
)
122+
.emit();
100123
}
101124
result.push(TokenTree::MetaVarDecl(span, ident, Some(kind)));
102125
continue;

tests/ui/macros/expr_2021_old_edition.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ compile-flags: --edition=2018
22

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

55
macro_rules! m {
66
($e:expr_2021) => { //~ ERROR: invalid fragment specifier `expr_2021`

tests/ui/macros/expr_2021_old_edition.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ error: invalid fragment specifier `expr_2021`
44
LL | ($e:expr_2021) => {
55
| ^^^^^^^^^^^^
66
|
7-
= help: valid fragment specifiers are `ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, `literal`, `path`, `meta`, `tt`, `item` and `vis`
7+
= help: fragment specifier `expr_2021` requires Rust 2021 or later
8+
valid fragment specifiers are `ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, `literal`, `path`, `meta`, `tt`, `item` and `vis`
89

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

tests/ui/macros/feature-gate-expr_fragment_specifier_2024.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@ compile-flags: --edition=2024 -Z unstable-options
22

33
macro_rules! m {
4-
($e:expr_2021) => { //~ ERROR: expr_2021 is experimental
4+
($e:expr_2021) => { //~ ERROR: fragment specifier `expr_2021` is unstable
55
$e
66
};
77
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
error: expr_2021 is experimental
1+
error[E0658]: fragment specifier `expr_2021` is unstable
22
--> $DIR/feature-gate-expr_fragment_specifier_2024.rs:4:6
33
|
44
LL | ($e:expr_2021) => {
55
| ^^^^^^^^^^^^
6+
|
7+
= note: see issue #123742 <https://github.com/rust-lang/rust/issues/123742> for more information
8+
= help: add `#![feature(expr_fragment_specifier_2024)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
610

711
error: aborting due to 1 previous error
812

13+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)