Skip to content

Commit ddf6c6d

Browse files
authored
Rollup merge of #121067 - tshepang:make-expand-translatable, r=fmease
make "invalid fragment specifier" translatable
2 parents 99560a4 + e3859d2 commit ddf6c6d

7 files changed

+52
-27
lines changed

compiler/rustc_expand/messages.ftl

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ expand_invalid_cfg_multiple_predicates = multiple `cfg` predicates are specified
6161
expand_invalid_cfg_no_parens = `cfg` is not followed by parentheses
6262
expand_invalid_cfg_no_predicate = `cfg` predicate is not specified
6363
expand_invalid_cfg_predicate_literal = `cfg` predicate key cannot be a literal
64+
65+
expand_invalid_fragment_specifier =
66+
invalid fragment specifier `{$fragment}`
67+
.help = {$help}
68+
6469
expand_macro_body_stability =
6570
macros cannot have body stability attributes
6671
.label = invalid body stability attribute

compiler/rustc_expand/src/errors.rs

+10
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,13 @@ pub struct DuplicateMatcherBinding {
408408
#[label(expand_label2)]
409409
pub prev: Span,
410410
}
411+
412+
#[derive(Diagnostic)]
413+
#[diag(expand_invalid_fragment_specifier)]
414+
#[help]
415+
pub struct InvalidFragmentSpecifier {
416+
#[primary_span]
417+
pub span: Span,
418+
pub fragment: Ident,
419+
pub help: String,
420+
}

compiler/rustc_expand/src/mbe/quoted.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::errors;
12
use crate::mbe::macro_parser::count_metavar_decls;
23
use crate::mbe::{Delimited, KleeneOp, KleeneToken, MetaVarExpr, SequenceRepetition, TokenTree};
34

@@ -60,11 +61,11 @@ pub(super) fn parse(
6061
Some(&tokenstream::TokenTree::Token(Token { kind: token::Colon, span }, _)) => {
6162
match trees.next() {
6263
Some(tokenstream::TokenTree::Token(token, _)) => match token.ident() {
63-
Some((frag, _)) => {
64+
Some((fragment, _)) => {
6465
let span = token.span.with_lo(start_sp.lo());
6566

6667
let kind =
67-
token::NonterminalKind::from_symbol(frag.name, || {
68+
token::NonterminalKind::from_symbol(fragment.name, || {
6869
// FIXME(#85708) - once we properly decode a foreign
6970
// crate's `SyntaxContext::root`, then we can replace
7071
// this with just `span.edition()`. A
@@ -81,14 +82,13 @@ pub(super) fn parse(
8182
})
8283
.unwrap_or_else(
8384
|| {
84-
let msg = format!(
85-
"invalid fragment specifier `{}`",
86-
frag.name
85+
sess.dcx().emit_err(
86+
errors::InvalidFragmentSpecifier {
87+
span,
88+
fragment,
89+
help: VALID_FRAGMENT_NAMES_MSG.into(),
90+
},
8791
);
88-
sess.dcx()
89-
.struct_span_err(span, msg)
90-
.with_help(VALID_FRAGMENT_NAMES_MSG)
91-
.emit();
9292
token::NonterminalKind::Ident
9393
},
9494
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
macro_rules! test {
2+
($wrong:id) => {};
3+
} //~^ ERROR: invalid fragment specifier `id`
4+
5+
// guard against breaking raw identifier diagnostic
6+
macro_rules! test_raw_identifer {
7+
($wrong:r#if) => {};
8+
} //~^ ERROR: invalid fragment specifier `r#if`
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: invalid fragment specifier `id`
2+
--> $DIR/invalid-fragment-specifier.rs:2:6
3+
|
4+
LL | ($wrong:id) => {};
5+
| ^^^^^^^^^
6+
|
7+
= help: valid fragment specifiers are `ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, `literal`, `path`, `meta`, `tt`, `item` and `vis`
8+
9+
error: invalid fragment specifier `r#if`
10+
--> $DIR/invalid-fragment-specifier.rs:7:6
11+
|
12+
LL | ($wrong:r#if) => {};
13+
| ^^^^^^^^^^^
14+
|
15+
= help: valid fragment specifiers are `ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, `literal`, `path`, `meta`, `tt`, `item` and `vis`
16+
17+
error: aborting due to 2 previous errors
18+

tests/ui/macros/macro-invalid-fragment-spec.rs

-8
This file was deleted.

tests/ui/macros/macro-invalid-fragment-spec.stderr

-10
This file was deleted.

0 commit comments

Comments
 (0)