Skip to content

Commit

Permalink
Rollup merge of rust-lang#121067 - tshepang:make-expand-translatable,…
Browse files Browse the repository at this point in the history
… r=fmease

make "invalid fragment specifier" translatable
  • Loading branch information
CKingX authored Feb 18, 2024
2 parents c465cfa + e3859d2 commit 3aff0b0
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 27 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_expand/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ expand_invalid_cfg_multiple_predicates = multiple `cfg` predicates are specified
expand_invalid_cfg_no_parens = `cfg` is not followed by parentheses
expand_invalid_cfg_no_predicate = `cfg` predicate is not specified
expand_invalid_cfg_predicate_literal = `cfg` predicate key cannot be a literal
expand_invalid_fragment_specifier =
invalid fragment specifier `{$fragment}`
.help = {$help}
expand_macro_body_stability =
macros cannot have body stability attributes
.label = invalid body stability attribute
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_expand/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,13 @@ pub struct DuplicateMatcherBinding {
#[label(expand_label2)]
pub prev: Span,
}

#[derive(Diagnostic)]
#[diag(expand_invalid_fragment_specifier)]
#[help]
pub struct InvalidFragmentSpecifier {
#[primary_span]
pub span: Span,
pub fragment: Ident,
pub help: String,
}
18 changes: 9 additions & 9 deletions compiler/rustc_expand/src/mbe/quoted.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::errors;
use crate::mbe::macro_parser::count_metavar_decls;
use crate::mbe::{Delimited, KleeneOp, KleeneToken, MetaVarExpr, SequenceRepetition, TokenTree};

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

let kind =
token::NonterminalKind::from_symbol(frag.name, || {
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
Expand All @@ -81,14 +82,13 @@ pub(super) fn parse(
})
.unwrap_or_else(
|| {
let msg = format!(
"invalid fragment specifier `{}`",
frag.name
sess.dcx().emit_err(
errors::InvalidFragmentSpecifier {
span,
fragment,
help: VALID_FRAGMENT_NAMES_MSG.into(),
},
);
sess.dcx()
.struct_span_err(span, msg)
.with_help(VALID_FRAGMENT_NAMES_MSG)
.emit();
token::NonterminalKind::Ident
},
);
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/macros/invalid-fragment-specifier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
macro_rules! test {
($wrong:id) => {};
} //~^ ERROR: invalid fragment specifier `id`

// guard against breaking raw identifier diagnostic
macro_rules! test_raw_identifer {
($wrong:r#if) => {};
} //~^ ERROR: invalid fragment specifier `r#if`

fn main() {}
18 changes: 18 additions & 0 deletions tests/ui/macros/invalid-fragment-specifier.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: invalid fragment specifier `id`
--> $DIR/invalid-fragment-specifier.rs:2:6
|
LL | ($wrong:id) => {};
| ^^^^^^^^^
|
= help: valid fragment specifiers are `ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, `literal`, `path`, `meta`, `tt`, `item` and `vis`

error: invalid fragment specifier `r#if`
--> $DIR/invalid-fragment-specifier.rs:7:6
|
LL | ($wrong:r#if) => {};
| ^^^^^^^^^^^
|
= help: valid fragment specifiers are `ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, `literal`, `path`, `meta`, `tt`, `item` and `vis`

error: aborting due to 2 previous errors

8 changes: 0 additions & 8 deletions tests/ui/macros/macro-invalid-fragment-spec.rs

This file was deleted.

10 changes: 0 additions & 10 deletions tests/ui/macros/macro-invalid-fragment-spec.stderr

This file was deleted.

0 comments on commit 3aff0b0

Please sign in to comment.