Skip to content

Commit 12ce9a9

Browse files
committed
Make diagnostics more translatable
1 parent 7f84bd4 commit 12ce9a9

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
@@ -377,12 +377,12 @@ parse_inclusive_range_no_end = inclusive range with no end
377377
parse_incorrect_parens_trait_bounds = incorrect parentheses around trait bounds
378378
parse_incorrect_parens_trait_bounds_sugg = fix the parentheses
379379
380-
parse_incorrect_restriction = incorrect {$noun} restriction
381-
.help = some possible {$noun} restrictions are:
382-
`{$keyword}(crate)`: {$adjective} only in the current crate
383-
`{$keyword}(super)`: {$adjective} only in the current module's parent
384-
`{$keyword}(in path::to::module)`: {$adjective} only in the specified path
385-
.suggestion = make this {$adjective} only to module `{$inner_str}` with `in`
380+
parse_incorrect_restriction = incorrect {parse_restriction_noun} restriction
381+
.help = some possible {parse_restriction_noun} restrictions are:
382+
`{$keyword}(crate)`: {parse_restriction_adjective} only in the current crate
383+
`{$keyword}(super)`: {parse_restriction_adjective} only in the current module's parent
384+
`{$keyword}(in path::to::module)`: {parse_restriction_adjective} only in the specified path
385+
.suggestion = make this {parse_restriction_adjective} only to module `{$path}` with `in`
386386
387387
parse_incorrect_semicolon =
388388
expected item, found `;`
@@ -790,6 +790,18 @@ parse_reserved_string = invalid string literal
790790
.note = unprefixed guarded string literals are reserved for future use since Rust 2024
791791
.suggestion_whitespace = consider inserting whitespace here
792792
793+
# internal use only
794+
parse_restriction_adjective = { $keyword ->
795+
[impl] implementable
796+
*[DEFAULT_IS_BUG] BUG
797+
}
798+
799+
# internal use only
800+
parse_restriction_noun = { $keyword ->
801+
[impl] impl
802+
*[DEFAULT_IS_BUG] BUG
803+
}
804+
793805
parse_return_types_use_thin_arrow = return types are denoted using `->`
794806
.suggestion = use `->` instead
795807

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
@@ -931,13 +931,8 @@ impl<'a> Parser<'a> {
931931

932932
/// Parses `[ impl(in path) ]? unsafe? auto? trait Foo { ... }` or `trait Foo = Bar;`.
933933
fn parse_item_trait(&mut self, attrs: &mut AttrVec, lo: Span) -> PResult<'a, ItemKind> {
934-
let impl_restriction = self.parse_restriction(
935-
exp!(Impl),
936-
Some(sym::impl_restriction),
937-
"implementable",
938-
"impl",
939-
FollowedByType::No,
940-
)?;
934+
let impl_restriction =
935+
self.parse_restriction(exp!(Impl), Some(sym::impl_restriction), FollowedByType::No)?;
941936
let safety = self.parse_safety(Case::Sensitive);
942937
// Parse optional `auto` prefix.
943938
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
@@ -1522,8 +1522,6 @@ impl<'a> Parser<'a> {
15221522
&mut self,
15231523
kw: ExpKeywordPair,
15241524
feature_gate: Option<Symbol>,
1525-
action: &'static str,
1526-
description: &'static str,
15271525
fbt: FollowedByType,
15281526
) -> PResult<'a, Restriction> {
15291527
if !self.eat_keyword(kw) {
@@ -1567,7 +1565,7 @@ impl<'a> Parser<'a> {
15671565
} else if let FollowedByType::No = fbt {
15681566
// Provide this diagnostic if a type cannot follow;
15691567
// in particular, if this is not a tuple struct.
1570-
self.recover_incorrect_restriction(kw.kw.as_str(), action, description)?;
1568+
self.recover_incorrect_restriction(kw.kw.as_str())?;
15711569
// Emit diagnostic, but continue unrestricted.
15721570
}
15731571
}
@@ -1576,24 +1574,13 @@ impl<'a> Parser<'a> {
15761574
}
15771575

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

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

15981585
Ok(())
15991586
}

0 commit comments

Comments
 (0)