Skip to content

Commit 9c27678

Browse files
committed
fix raw string issue in diagnostics macro
1 parent 6867afe commit 9c27678

File tree

3 files changed

+32
-35
lines changed

3 files changed

+32
-35
lines changed

compiler/rustc_errors/src/translation.rs

+4-7
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

+8-2
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

+20-26
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,10 @@ pub(crate) struct FieldExpressionWithGeneric(#[primary_span] pub Span);
309309
pub(crate) struct MacroInvocationWithQualifiedPath(#[primary_span] pub Span);
310310

311311
#[derive(Diagnostic)]
312-
#[diag("expected `while`, `for`, `loop` or `{` after a label")]
312+
#[diag(r#"expected `while`, `for`, `loop` or `{"{"}` after a label"#)]
313313
pub(crate) struct UnexpectedTokenAfterLabel {
314314
#[primary_span]
315-
#[label("expected `while`, `for`, `loop` or `{` after a label")]
315+
#[label(r#"expected `while`, `for`, `loop` or `{"{"}` after a label"#)]
316316
pub span: Span,
317317
#[suggestion(label = "consider removing the label", style = "verbose", code = "")]
318318
pub remove_label: Option<Span>,
@@ -498,7 +498,7 @@ pub(crate) struct ExpectedEqForLetExpr {
498498
}
499499

500500
#[derive(Diagnostic)]
501-
#[diag(label = r#"expected `{"{"}`, found {$first_tok}"#)]
501+
#[diag(r#"expected `{"{"}`, found {$first_tok}"#)]
502502
pub(crate) struct ExpectedElseBlock {
503503
#[primary_span]
504504
pub first_tok_span: Span,
@@ -514,10 +514,10 @@ pub(crate) struct ExpectedElseBlock {
514514
}
515515

516516
#[derive(Diagnostic)]
517-
#[diag(label = r#"expected one of `,`, `:`, or `{"}"}`, found `{$token}`"#)]
517+
#[diag(r#"expected one of `,`, `:`, or `{"}"}`, found `{$token}`"#)]
518518
pub(crate) struct ExpectedStructField {
519519
#[primary_span]
520-
#[label("expected one of `,`, `:`, or `}`")]
520+
#[label(r#"expected one of `,`, `:`, or `{"}"}`"#)]
521521
pub span: Span,
522522
pub token: Token,
523523
#[label("while parsing this struct field")]
@@ -617,7 +617,7 @@ pub(crate) struct CatchAfterTry {
617617

618618
#[derive(Diagnostic)]
619619
#[diag("`gen` functions are not yet implemented")]
620-
#[help("for now you can use `gen {}` blocks and return `impl Iterator` instead")]
620+
#[help(r#"for now you can use `gen {"{}"}` blocks and return `impl Iterator` instead"#)]
621621
pub(crate) struct GenFn {
622622
#[primary_span]
623623
pub span: Span,
@@ -706,11 +706,11 @@ pub(crate) struct UseEqInstead {
706706
}
707707

708708
#[derive(Diagnostic)]
709-
#[diag("expected `{}`, found `;`")]
709+
#[diag(r#"expected { "`{}`" }, found `;`"#)]
710710
pub(crate) struct UseEmptyBlockNotSemi {
711711
#[primary_span]
712712
#[suggestion(
713-
label = "try using `{}` instead",
713+
label = r#"try using { "`{}`" } instead"#,
714714
style = "hidden",
715715
applicability = "machine-applicable",
716716
code = "{{}}"
@@ -1045,7 +1045,7 @@ pub(crate) struct IncorrectVisibilityRestriction {
10451045
}
10461046

10471047
#[derive(Diagnostic)]
1048-
#[diag("<assignment> ... else { ... } is not allowed")]
1048+
#[diag(r#"<assignment> ... else {"{"} ... {"}"} is not allowed"#)]
10491049
pub(crate) struct AssignmentElseNotAllowed {
10501050
#[primary_span]
10511051
pub span: Span,
@@ -1087,7 +1087,7 @@ pub(crate) struct InvalidExpressionInLetElse {
10871087
}
10881088

10891089
#[derive(Diagnostic)]
1090-
#[diag("right curly brace `}` before `else` in a `let...else` statement not allowed")]
1090+
#[diag(r#"right curly brace `{"}"}` before `else` in a `let...else` statement not allowed"#)]
10911091
pub(crate) struct InvalidCurlyInLetElse {
10921092
#[primary_span]
10931093
pub span: Span,
@@ -1803,7 +1803,7 @@ pub struct UnexpectedTokenAfterDot<'a> {
18031803

18041804
#[derive(Diagnostic)]
18051805
#[diag("visibility `{$vis}` is not followed by an item")]
1806-
#[help("you likely meant to define an item, e.g., `{$vis} fn foo() {\"{}\"}`")]
1806+
#[help(r#"you likely meant to define an item, e.g., `{$vis} fn foo() {"{}"}`"#)]
18071807
pub(crate) struct VisibilityNotFollowedByItem {
18081808
#[primary_span]
18091809
#[label("the visibility")]
@@ -2049,45 +2049,39 @@ pub(crate) struct EnumStructMutuallyExclusive {
20492049

20502050
#[derive(Diagnostic)]
20512051
pub(crate) enum UnexpectedTokenAfterStructName {
2052-
#[diag(
2053-
label = r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found reserved identifier `{$token}`"#
2054-
)]
2052+
#[diag(r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found reserved identifier `{$token}`"#)]
20552053
ReservedIdentifier {
20562054
#[primary_span]
20572055
#[label(r#"expected `where`, `{"{"}`, `(`, or `;` after struct name"#)]
20582056
span: Span,
20592057
token: Token,
20602058
},
20612059
#[diag(
2062-
label = r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found keyword `{$token}`"#
2060+
r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found keyword `{$token}`"#
20632061
)]
20642062
Keyword {
20652063
#[primary_span]
20662064
#[label(r#"expected `where`, `{"{"}`, `(`, or `;` after struct name"#)]
20672065
span: Span,
20682066
token: Token,
20692067
},
2070-
#[diag(
2071-
label = r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found reserved keyword `{$token}`"#
2072-
)]
2068+
#[diag(r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found reserved keyword `{$token}`"#)]
20732069
ReservedKeyword {
20742070
#[primary_span]
20752071
#[label(r#"expected `where`, `{"{"}`, `(`, or `;` after struct name"#)]
20762072
span: Span,
20772073
token: Token,
20782074
},
20792075
#[diag(
2080-
label = r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found doc comment `{$token}`"#
2076+
r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found doc comment `{$token}`"#
20812077
)]
20822078
DocComment {
20832079
#[primary_span]
20842080
#[label(r#"expected `where`, `{"{"}`, `(`, or `;` after struct name"#)]
20852081
span: Span,
20862082
token: Token,
20872083
},
2088-
#[diag(
2089-
label = r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found `{$token}`"#
2090-
)]
2084+
#[diag(r#"expected `where`, `{"{"}`, `(`, or `;` after struct name, found `{$token}`"#)]
20912085
Other {
20922086
#[primary_span]
20932087
#[label(r#"expected `where`, `{"{"}`, `(`, or `;` after struct name"#)]
@@ -2430,7 +2424,7 @@ pub enum UnescapeError {
24302424
#[diag("unterminated unicode escape")]
24312425
UnclosedUnicodeEscape(
24322426
#[primary_span]
2433-
#[label("missing a closing `}`")]
2427+
#[label(r#"missing a closing `{"}"}`"#)]
24342428
Span,
24352429
#[suggestion(
24362430
label = "terminate the unicode escape",
@@ -2573,7 +2567,7 @@ pub enum NoBraceUnicodeSub {
25732567
span: Span,
25742568
suggestion: String,
25752569
},
2576-
#[help(r#"format of unicode escape sequences is `\u{...}`"#)]
2570+
#[help(r#"format of unicode escape sequences is `\u{"{...}"}`"#)]
25772571
Help,
25782572
}
25792573

@@ -2895,7 +2889,7 @@ pub(crate) struct InvalidDynKeyword {
28952889

28962890
#[derive(Subdiagnostic)]
28972891
pub enum HelpUseLatestEdition {
2898-
#[help("set `edition = \"{$edition}\"` in `Cargo.toml`")]
2892+
#[help(r#"set `edition = "{$edition}"` in `Cargo.toml`"#)]
28992893
#[note("for more on editions, read https://doc.rust-lang.org/edition-guide")]
29002894
Cargo { edition: Edition },
29012895
#[help("pass `--edition {$edition}` to `rustc`")]
@@ -3204,7 +3198,7 @@ pub(crate) struct FunctionBodyEqualsExpr {
32043198

32053199
#[derive(Subdiagnostic)]
32063200
#[multipart_suggestion(
3207-
label = "surround the expression with `{` and `}` instead of `=` and `;`",
3201+
label = r#"surround the expression with `{"{"}` and `{"}"}` instead of `=` and `;`"#,
32083202
applicability = "machine-applicable"
32093203
)]
32103204
pub(crate) struct FunctionBodyEqualsExprSugg {

0 commit comments

Comments
 (0)