Skip to content

Commit 5060bf9

Browse files
committed
Make diagnostics more translatable
1 parent dfd6b8e commit 5060bf9

File tree

4 files changed

+25
-33
lines changed

4 files changed

+25
-33
lines changed

compiler/rustc_parse/messages.ftl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,12 @@ parse_inclusive_range_no_end = inclusive range with no end
353353
parse_incorrect_parens_trait_bounds = incorrect parentheses around trait bounds
354354
parse_incorrect_parens_trait_bounds_sugg = fix the parentheses
355355
356-
parse_incorrect_restriction = incorrect {$noun} restriction
357-
.help = some possible {$noun} restrictions are:
358-
`{$keyword}(crate)`: {$adjective} only in the current crate
359-
`{$keyword}(super)`: {$adjective} only in the current module's parent
360-
`{$keyword}(in path::to::module)`: {$adjective} only in the specified path
361-
.suggestion = make this {$adjective} only to module `{$inner_str}` with `in`
356+
parse_incorrect_restriction = incorrect {parse_restriction_noun} restriction
357+
.help = some possible {parse_restriction_noun} restrictions are:
358+
`{$keyword}(crate)`: {parse_restriction_adjective} only in the current crate
359+
`{$keyword}(super)`: {parse_restriction_adjective} only in the current module's parent
360+
`{$keyword}(in path::to::module)`: {parse_restriction_adjective} only in the specified path
361+
.suggestion = make this {parse_restriction_adjective} only to module `{$path}` with `in`
362362
363363
parse_incorrect_semicolon =
364364
expected item, found `;`
@@ -766,6 +766,18 @@ parse_reserved_string = invalid string literal
766766
.note = unprefixed guarded string literals are reserved for future use since Rust 2024
767767
.suggestion_whitespace = consider inserting whitespace here
768768
769+
# internal use only
770+
parse_restriction_adjective = { $keyword ->
771+
[impl] implementable
772+
*[DEFAULT_IS_BUG] BUG
773+
}
774+
775+
# internal use only
776+
parse_restriction_noun = { $keyword ->
777+
[impl] impl
778+
*[DEFAULT_IS_BUG] BUG
779+
}
780+
769781
parse_return_types_use_thin_arrow = return types are denoted using `->`
770782
.suggestion = use `->` instead
771783

compiler/rustc_parse/src/errors.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,12 +1046,10 @@ pub(crate) struct IncorrectVisibilityRestriction {
10461046
#[help]
10471047
pub(crate) struct IncorrectRestriction<'kw> {
10481048
#[primary_span]
1049-
#[suggestion(code = "in {inner_str}", applicability = "machine-applicable", style = "verbose")]
1049+
#[suggestion(code = "in {path}", applicability = "machine-applicable", style = "verbose")]
10501050
pub span: Span,
1051-
pub inner_str: String,
1051+
pub path: String,
10521052
pub keyword: &'kw str,
1053-
pub adjective: &'static str,
1054-
pub noun: &'static str,
10551053
}
10561054

10571055
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/item.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -925,13 +925,8 @@ impl<'a> Parser<'a> {
925925

926926
/// Parses `[ impl(in path) ]? unsafe? auto? trait Foo { ... }` or `trait Foo = Bar;`.
927927
fn parse_item_trait(&mut self, attrs: &mut AttrVec, lo: Span) -> PResult<'a, ItemKind> {
928-
let impl_restriction = self.parse_restriction(
929-
exp!(Impl),
930-
Some(sym::impl_restriction),
931-
"implementable",
932-
"impl",
933-
FollowedByType::No,
934-
)?;
928+
let impl_restriction =
929+
self.parse_restriction(exp!(Impl), Some(sym::impl_restriction), FollowedByType::No)?;
935930
let safety = self.parse_safety(Case::Sensitive);
936931
// Parse optional `auto` prefix.
937932
let is_auto = if self.eat_keyword(exp!(Auto)) {

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,8 +1521,6 @@ impl<'a> Parser<'a> {
15211521
&mut self,
15221522
kw: ExpKeywordPair,
15231523
feature_gate: Option<Symbol>,
1524-
action: &'static str,
1525-
description: &'static str,
15261524
fbt: FollowedByType,
15271525
) -> PResult<'a, Restriction> {
15281526
if !self.eat_keyword(kw) {
@@ -1566,7 +1564,7 @@ impl<'a> Parser<'a> {
15661564
} else if let FollowedByType::No = fbt {
15671565
// Provide this diagnostic if a type cannot follow;
15681566
// in particular, if this is not a tuple struct.
1569-
self.recover_incorrect_restriction(kw.kw.as_str(), action, description)?;
1567+
self.recover_incorrect_restriction(kw.kw.as_str())?;
15701568
// Emit diagnostic, but continue unrestricted.
15711569
}
15721570
}
@@ -1575,24 +1573,13 @@ impl<'a> Parser<'a> {
15751573
}
15761574

15771575
/// Recovery for e.g. `kw(something) fn ...` or `struct X { kw(something) y: Z }`
1578-
fn recover_incorrect_restriction<'kw>(
1579-
&mut self,
1580-
kw: &'kw str,
1581-
action: &'static str,
1582-
description: &'static str,
1583-
) -> PResult<'a, ()> {
1576+
fn recover_incorrect_restriction<'kw>(&mut self, kw: &'kw str) -> PResult<'a, ()> {
15841577
self.bump(); // `(`
15851578
let path = self.parse_path(PathStyle::Mod)?;
15861579
self.expect(exp!(CloseParen))?; // `)`
15871580

15881581
let path_str = pprust::path_to_string(&path);
1589-
self.dcx().emit_err(IncorrectRestriction {
1590-
span: path.span,
1591-
inner_str: path_str,
1592-
keyword: kw,
1593-
adjective: action,
1594-
noun: description,
1595-
});
1582+
self.dcx().emit_err(IncorrectRestriction { span: path.span, path: path_str, keyword: kw });
15961583

15971584
Ok(())
15981585
}

0 commit comments

Comments
 (0)