Skip to content

Commit 3558ad9

Browse files
committed
Make expand_invalid_fragment_specifier translatable
1 parent 871f723 commit 3558ad9

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

compiler/rustc_expand/messages.ftl

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ expand_invalid_cfg_predicate_literal = `cfg` predicate key cannot be a literal
8282
8383
expand_invalid_fragment_specifier =
8484
invalid fragment specifier `{$fragment}`
85-
.help = {$help}
85+
.help_expr_2021 = fragment specifier `expr_2021` requires Rust 2021 or later
86+
.help_valid_names_2021 = valid fragment specifiers are `ident`, `block`, `stmt`, `expr`, `expr_2021`, `pat`, `ty`, `lifetime`, `literal`, `path`, `meta`, `tt`, `item` and `vis`
87+
.help_valid_names_other = valid fragment specifiers are `ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, `literal`, `path`, `meta`, `tt`, `item` and `vis`
8688
8789
expand_macro_body_stability =
8890
macros cannot have body stability attributes

compiler/rustc_expand/src/errors.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -417,14 +417,24 @@ pub struct DuplicateMatcherBinding {
417417
pub prev: Span,
418418
}
419419

420+
#[derive(Subdiagnostic)]
421+
pub enum InvalidFragmentSpecifierValidNames {
422+
#[help(expand_help_valid_names_2021)]
423+
Edition2021,
424+
#[help(expand_help_valid_names_other)]
425+
Other,
426+
}
427+
420428
#[derive(Diagnostic)]
421429
#[diag(expand_invalid_fragment_specifier)]
422-
#[help]
423430
pub struct InvalidFragmentSpecifier {
424431
#[primary_span]
425432
pub span: Span,
433+
#[help(expand_help_expr_2021)]
434+
pub help_expr_2021: bool,
435+
#[subdiagnostic]
436+
pub help_valid_names: InvalidFragmentSpecifierValidNames,
426437
pub fragment: Ident,
427-
pub help: String,
428438
}
429439

430440
#[derive(Diagnostic)]

compiler/rustc_expand/src/mbe/quoted.rs

+16-22
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ use rustc_span::symbol::{kw, sym, Ident};
1313
use rustc_span::edition::Edition;
1414
use rustc_span::Span;
1515

16-
const VALID_FRAGMENT_NAMES_MSG: &str = "valid fragment specifiers are \
17-
`ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, \
18-
`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`";
23-
2416
/// Takes a `tokenstream::TokenStream` and returns a `Vec<self::TokenTree>`. Specifically, this
2517
/// takes a generic `TokenStream`, such as is used in the rest of the compiler, and returns a
2618
/// collection of `TokenTree` for use in parsing a macro.
@@ -85,27 +77,29 @@ pub(super) fn parse(
8577
let kind =
8678
token::NonterminalKind::from_symbol(fragment.name, edition)
8779
.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-
}
80+
let (help_expr_2021, help_valid_names) = match fragment.name {
81+
sym::expr_2021 => (
82+
true,
83+
errors::InvalidFragmentSpecifierValidNames::Other,
84+
),
9685
_ if edition().at_least_rust_2021()
9786
&& features
98-
.expr_fragment_specifier_2024 =>
99-
{
100-
VALID_FRAGMENT_NAMES_MSG_2021.into()
101-
}
102-
_ => VALID_FRAGMENT_NAMES_MSG.into(),
87+
.expr_fragment_specifier_2024 => (
88+
false,
89+
errors::InvalidFragmentSpecifierValidNames::Edition2021,
90+
),
91+
_ => (
92+
false,
93+
errors::InvalidFragmentSpecifierValidNames::Other,
94+
)
95+
10396
};
10497
sess.dcx().emit_err(
10598
errors::InvalidFragmentSpecifier {
10699
span,
107100
fragment,
108-
help,
101+
help_expr_2021,
102+
help_valid_names,
109103
},
110104
);
111105
token::NonterminalKind::Ident

tests/ui/macros/expr_2021_old_edition.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | ($e:expr_2021) => {
55
| ^^^^^^^^^^^^
66
|
77
= 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`
8+
= help: valid fragment specifiers are `ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, `literal`, `path`, `meta`, `tt`, `item` and `vis`
99

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

0 commit comments

Comments
 (0)