Skip to content

Commit f494645

Browse files
committed
fix raw string issue in diagnostics macro
1 parent 90ba930 commit f494645

File tree

3 files changed

+32
-35
lines changed

3 files changed

+32
-35
lines changed

compiler/rustc_errors/src/translation.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,9 @@ pub trait Translate {
6565
trace!(?message, ?args);
6666
let (identifier, attr) = match message {
6767
DiagnosticMessage::Str(msg) | DiagnosticMessage::Eager(msg) => {
68-
if args.iter().next().is_none() || (!msg.contains("$") && !msg.contains("`{")) {
69-
return Ok(Cow::Borrowed(msg));
70-
} else {
71-
// FIXME(yukang): A hack for raw fluent content for new diagnostics proc format
68+
// FIXME(yukang): A hack for raw fluent content for new diagnostics proc format
69+
let trimed = msg.replace(" ", "");
70+
if trimed.contains("$") || trimed.contains("{\"") || trimed.contains("\"}") {
7271
let fluent_text = format!("dummy = {}", msg);
7372
if let Ok(resource) = FluentResource::try_new(fluent_text) {
7473
let mut bundle = RawFluentBundle::new(vec![langid!("en-US")]);
@@ -79,11 +78,9 @@ pub trait Translate {
7978
return Ok(Cow::Owned(
8079
res.to_string().replace("\u{2068}", "").replace("\u{2069}", ""),
8180
));
82-
} else {
83-
// If the message is not a valid Fluent resource, just return the original
84-
return Ok(Cow::Borrowed(msg));
8581
}
8682
}
83+
return Ok(Cow::Borrowed(msg));
8784
}
8885
DiagnosticMessage::FluentIdentifier(identifier, attr) => (identifier, attr),
8986
};

compiler/rustc_macros/src/diagnostics/utils.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ pub(crate) fn new_code_ident() -> syn::Ident {
3030
}
3131

3232
pub(crate) fn convert_to_litstr(lit: &proc_macro2::Literal) -> LitStr {
33-
let lit_content = format!("{}", lit);
34-
LitStr::new(&lit_content[1..lit_content.len() - 1], lit.span())
33+
let s = format!("{}", lit);
34+
let s = if s.starts_with("r#\"") && s.ends_with("\"#") && s.len() >= 5 {
35+
s[3..s.len() - 2].to_string()
36+
} else {
37+
s[1..s.len() - 1].to_string()
38+
};
39+
40+
LitStr::new(&s, lit.span())
3541
}
3642

3743
/// Checks whether the type name of `ty` matches `name`.

compiler/rustc_parse/src/errors.rs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,10 @@ pub(crate) struct FieldExpressionWithGeneric(#[primary_span] pub Span);
317317
pub(crate) struct MacroInvocationWithQualifiedPath(#[primary_span] pub Span);
318318

319319
#[derive(Diagnostic)]
320-
#[diag("expected `while`, `for`, `loop` or `{` after a label")]
320+
#[diag(r#"expected `while`, `for`, `loop` or `{"{"}` after a label"#)]
321321
pub(crate) struct UnexpectedTokenAfterLabel {
322322
#[primary_span]
323-
#[label("expected `while`, `for`, `loop` or `{` after a label")]
323+
#[label(r#"expected `while`, `for`, `loop` or `{"{"}` after a label"#)]
324324
pub span: Span,
325325
#[suggestion(label = "consider removing the label", style = "verbose", code = "")]
326326
pub remove_label: Option<Span>,
@@ -532,7 +532,7 @@ pub(crate) struct ExpectedEqForLetExpr {
532532
}
533533

534534
#[derive(Diagnostic)]
535-
#[diag(label = r#"expected `{"{"}`, found {$first_tok}"#)]
535+
#[diag(r#"expected `{"{"}`, found {$first_tok}"#)]
536536
pub(crate) struct ExpectedElseBlock {
537537
#[primary_span]
538538
pub first_tok_span: Span,
@@ -548,10 +548,10 @@ pub(crate) struct ExpectedElseBlock {
548548
}
549549

550550
#[derive(Diagnostic)]
551-
#[diag(label = r#"expected one of `,`, `:`, or `{"}"}`, found `{$token}`"#)]
551+
#[diag(r#"expected one of `,`, `:`, or `{"}"}`, found `{$token}`"#)]
552552
pub(crate) struct ExpectedStructField {
553553
#[primary_span]
554-
#[label("expected one of `,`, `:`, or `}`")]
554+
#[label(r#"expected one of `,`, `:`, or `{"}"}`"#)]
555555
pub span: Span,
556556
pub token: Token,
557557
#[label("while parsing this struct field")]
@@ -651,7 +651,7 @@ pub(crate) struct CatchAfterTry {
651651

652652
#[derive(Diagnostic)]
653653
#[diag("`gen` functions are not yet implemented")]
654-
#[help("for now you can use `gen {}` blocks and return `impl Iterator` instead")]
654+
#[help(r#"for now you can use `gen {"{}"}` blocks and return `impl Iterator` instead"#)]
655655
pub(crate) struct GenFn {
656656
#[primary_span]
657657
pub span: Span,
@@ -747,11 +747,11 @@ pub(crate) struct UseEqInstead {
747747
}
748748

749749
#[derive(Diagnostic)]
750-
#[diag("expected `{}`, found `;`")]
750+
#[diag(r#"expected { "`{}`" }, found `;`"#)]
751751
pub(crate) struct UseEmptyBlockNotSemi {
752752
#[primary_span]
753753
#[suggestion(
754-
label = "try using `{}` instead",
754+
label = r#"try using { "`{}`" } instead"#,
755755
style = "hidden",
756756
applicability = "machine-applicable",
757757
code = "{{}}"
@@ -1086,7 +1086,7 @@ pub(crate) struct IncorrectVisibilityRestriction {
10861086
}
10871087

10881088
#[derive(Diagnostic)]
1089-
#[diag("<assignment> ... else { ... } is not allowed")]
1089+
#[diag(r#"<assignment> ... else {"{"} ... {"}"} is not allowed"#)]
10901090
pub(crate) struct AssignmentElseNotAllowed {
10911091
#[primary_span]
10921092
pub span: Span,
@@ -1128,7 +1128,7 @@ pub(crate) struct InvalidExpressionInLetElse {
11281128
}
11291129

11301130
#[derive(Diagnostic)]
1131-
#[diag("right curly brace `}` before `else` in a `let...else` statement not allowed")]
1131+
#[diag(r#"right curly brace `{"}"}` before `else` in a `let...else` statement not allowed"#)]
11321132
pub(crate) struct InvalidCurlyInLetElse {
11331133
#[primary_span]
11341134
pub span: Span,
@@ -1860,7 +1860,7 @@ pub struct UnexpectedTokenAfterDot<'a> {
18601860

18611861
#[derive(Diagnostic)]
18621862
#[diag("visibility `{$vis}` is not followed by an item")]
1863-
#[help("you likely meant to define an item, e.g., `{$vis} fn foo() {\"{}\"}`")]
1863+
#[help(r#"you likely meant to define an item, e.g., `{$vis} fn foo() {"{}"}`"#)]
18641864
pub(crate) struct VisibilityNotFollowedByItem {
18651865
#[primary_span]
18661866
#[label("the visibility")]
@@ -2106,45 +2106,39 @@ pub(crate) struct EnumStructMutuallyExclusive {
21062106

21072107
#[derive(Diagnostic)]
21082108
pub(crate) enum UnexpectedTokenAfterStructName {
2109-
#[diag(
2110-
label = r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found reserved identifier `{$token}`"#
2111-
)]
2109+
#[diag(r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found reserved identifier `{$token}`"#)]
21122110
ReservedIdentifier {
21132111
#[primary_span]
21142112
#[label(r#"expected `where`, `{"{"}`, `(`, or `;` after struct name"#)]
21152113
span: Span,
21162114
token: Token,
21172115
},
21182116
#[diag(
2119-
label = r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found keyword `{$token}`"#
2117+
r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found keyword `{$token}`"#
21202118
)]
21212119
Keyword {
21222120
#[primary_span]
21232121
#[label(r#"expected `where`, `{"{"}`, `(`, or `;` after struct name"#)]
21242122
span: Span,
21252123
token: Token,
21262124
},
2127-
#[diag(
2128-
label = r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found reserved keyword `{$token}`"#
2129-
)]
2125+
#[diag(r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found reserved keyword `{$token}`"#)]
21302126
ReservedKeyword {
21312127
#[primary_span]
21322128
#[label(r#"expected `where`, `{"{"}`, `(`, or `;` after struct name"#)]
21332129
span: Span,
21342130
token: Token,
21352131
},
21362132
#[diag(
2137-
label = r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found doc comment `{$token}`"#
2133+
r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found doc comment `{$token}`"#
21382134
)]
21392135
DocComment {
21402136
#[primary_span]
21412137
#[label(r#"expected `where`, `{"{"}`, `(`, or `;` after struct name"#)]
21422138
span: Span,
21432139
token: Token,
21442140
},
2145-
#[diag(
2146-
label = r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found `{$token}`"#
2147-
)]
2141+
#[diag(r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found `{$token}`"#)]
21482142
Other {
21492143
#[primary_span]
21502144
#[label(r#"expected `where`, `{"{"}`, `(`, or `;` after struct name"#)]
@@ -2487,7 +2481,7 @@ pub enum UnescapeError {
24872481
#[diag("unterminated unicode escape")]
24882482
UnclosedUnicodeEscape(
24892483
#[primary_span]
2490-
#[label("missing a closing `}`")]
2484+
#[label(r#"missing a closing `{"}"}`"#)]
24912485
Span,
24922486
#[suggestion(
24932487
label = "terminate the unicode escape",
@@ -2630,7 +2624,7 @@ pub enum NoBraceUnicodeSub {
26302624
span: Span,
26312625
suggestion: String,
26322626
},
2633-
#[help(r#"format of unicode escape sequences is `\u{...}`"#)]
2627+
#[help(r#"format of unicode escape sequences is `\u{"{...}"}`"#)]
26342628
Help,
26352629
}
26362630

@@ -2952,7 +2946,7 @@ pub(crate) struct InvalidDynKeyword {
29522946

29532947
#[derive(Subdiagnostic)]
29542948
pub enum HelpUseLatestEdition {
2955-
#[help("set `edition = \"{$edition}\"` in `Cargo.toml`")]
2949+
#[help(r#"set `edition = "{$edition}"` in `Cargo.toml`"#)]
29562950
#[note("for more on editions, read https://doc.rust-lang.org/edition-guide")]
29572951
Cargo { edition: Edition },
29582952
#[help("pass `--edition {$edition}` to `rustc`")]
@@ -3261,7 +3255,7 @@ pub(crate) struct FunctionBodyEqualsExpr {
32613255

32623256
#[derive(Subdiagnostic)]
32633257
#[multipart_suggestion(
3264-
label = "surround the expression with `{` and `}` instead of `=` and `;`",
3258+
label = r#"surround the expression with `{"{"}` and `{"}"}` instead of `=` and `;`"#,
32653259
applicability = "machine-applicable"
32663260
)]
32673261
pub(crate) struct FunctionBodyEqualsExprSugg {

0 commit comments

Comments
 (0)