Skip to content

Commit 0d0d369

Browse files
committed
Make "use latest edition" subdiagnostic translatable
1 parent a476683 commit 0d0d369

File tree

9 files changed

+75
-58
lines changed

9 files changed

+75
-58
lines changed

compiler/rustc_error_messages/locales/en-US/hir_typeck.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ hir_typeck_lang_start_incorrect_param = parameter {$param_num} of the `start` la
5757
5858
hir_typeck_lang_start_incorrect_ret_ty = the return type of the `start` lang item is incorrect
5959
.suggestion = change the type from `{$found_ty}` to `{$expected_ty}`
60+
61+
hir_typeck_help_set_edition_cargo = set `edition = "{$edition}"` in `Cargo.toml`
62+
hir_typeck_help_set_edition_standalone = pass `--edition {$edition}` to `rustc`
63+
hir_typeck_note_edition_guide = for more on editions, read https://doc.rust-lang.org/edition-guide

compiler/rustc_error_messages/locales/en-US/parse.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -574,3 +574,7 @@ parse_negative_bounds_not_supported = negative bounds are not supported
574574
[one] remove the bound
575575
*[other] remove the bounds
576576
}
577+
578+
parse_help_set_edition_cargo = set `edition = "{$edition}"` in `Cargo.toml`
579+
parse_help_set_edition_standalone = pass `--edition {$edition}` to `rustc`
580+
parse_note_edition_guide = for more on editions, read https://doc.rust-lang.org/edition-guide

compiler/rustc_errors/src/diagnostic.rs

-37
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_data_structures::fx::FxHashMap;
77
use rustc_error_messages::fluent_value_from_str_list_sep_by_and;
88
use rustc_error_messages::FluentValue;
99
use rustc_lint_defs::{Applicability, LintExpectationId};
10-
use rustc_span::edition::LATEST_STABLE_EDITION;
1110
use rustc_span::symbol::Symbol;
1211
use rustc_span::{Span, DUMMY_SP};
1312
use std::borrow::Cow;
@@ -1071,39 +1070,3 @@ impl PartialEq for Diagnostic {
10711070
self.keys() == other.keys()
10721071
}
10731072
}
1074-
1075-
pub enum HelpUseLatestEdition {
1076-
Cargo,
1077-
Standalone,
1078-
}
1079-
1080-
impl HelpUseLatestEdition {
1081-
pub fn new() -> Self {
1082-
if std::env::var_os("CARGO").is_some() { Self::Cargo } else { Self::Standalone }
1083-
}
1084-
}
1085-
1086-
impl AddToDiagnostic for HelpUseLatestEdition {
1087-
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, f: F)
1088-
where
1089-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
1090-
{
1091-
let msg = f(
1092-
diag,
1093-
match self {
1094-
Self::Cargo => {
1095-
format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION)
1096-
}
1097-
Self::Standalone => {
1098-
format!("pass `--edition {}` to `rustc`", LATEST_STABLE_EDITION)
1099-
}
1100-
}
1101-
.into(),
1102-
);
1103-
diag.help(msg);
1104-
1105-
let msg =
1106-
f(diag, "for more on editions, read https://doc.rust-lang.org/edition-guide".into());
1107-
diag.note(msg);
1108-
}
1109-
}

compiler/rustc_errors/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ pub struct DelayedBugPanic;
378378

379379
pub use diagnostic::{
380380
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgValue, DiagnosticId,
381-
DiagnosticStyledString, HelpUseLatestEdition, IntoDiagnosticArg, SubDiagnostic,
381+
DiagnosticStyledString, IntoDiagnosticArg, SubDiagnostic,
382382
};
383383
pub use diagnostic_builder::{DiagnosticBuilder, EmissionGuarantee, Noted};
384384
pub use diagnostic_impls::{DiagnosticArgFromDisplay, DiagnosticSymbolList};

compiler/rustc_hir_typeck/src/errors.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
use rustc_errors::{AddToDiagnostic, Applicability, Diagnostic, MultiSpan, SubdiagnosticMessage};
33
use rustc_macros::{Diagnostic, Subdiagnostic};
44
use rustc_middle::ty::Ty;
5-
use rustc_span::{symbol::Ident, Span};
5+
use rustc_span::{
6+
edition::{Edition, LATEST_STABLE_EDITION},
7+
symbol::Ident,
8+
Span,
9+
};
610

711
#[derive(Diagnostic)]
812
#[diag(hir_typeck_field_multiply_specified_in_initializer, code = "E0062")]
@@ -205,3 +209,24 @@ pub struct LangStartIncorrectRetTy<'tcx> {
205209
pub expected_ty: Ty<'tcx>,
206210
pub found_ty: Ty<'tcx>,
207211
}
212+
213+
#[derive(Subdiagnostic)]
214+
pub enum HelpUseLatestEdition {
215+
#[help(hir_typeck_help_set_edition_cargo)]
216+
#[note(hir_typeck_note_edition_guide)]
217+
Cargo { edition: Edition },
218+
#[help(hir_typeck_help_set_edition_standalone)]
219+
#[note(hir_typeck_note_edition_guide)]
220+
Standalone { edition: Edition },
221+
}
222+
223+
impl HelpUseLatestEdition {
224+
pub fn new() -> Self {
225+
let edition = LATEST_STABLE_EDITION;
226+
if std::env::var_os("CARGO").is_some() {
227+
Self::Cargo { edition }
228+
} else {
229+
Self::Standalone { edition }
230+
}
231+
}
232+
}

compiler/rustc_hir_typeck/src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::coercion::DynamicCoerceMany;
88
use crate::errors::TypeMismatchFruTypo;
99
use crate::errors::{AddressOfTemporaryTaken, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive};
1010
use crate::errors::{
11-
FieldMultiplySpecifiedInInitializer, FunctionalRecordUpdateOnNonStruct,
11+
FieldMultiplySpecifiedInInitializer, FunctionalRecordUpdateOnNonStruct, HelpUseLatestEdition,
1212
YieldExprOutsideOfGenerator,
1313
};
1414
use crate::fatally_break_rust;
@@ -24,7 +24,7 @@ use rustc_data_structures::fx::FxHashMap;
2424
use rustc_data_structures::stack::ensure_sufficient_stack;
2525
use rustc_errors::{
2626
pluralize, struct_span_err, AddToDiagnostic, Applicability, Diagnostic, DiagnosticBuilder,
27-
DiagnosticId, ErrorGuaranteed, HelpUseLatestEdition, StashKey,
27+
DiagnosticId, ErrorGuaranteed, StashKey,
2828
};
2929
use rustc_hir as hir;
3030
use rustc_hir::def::{CtorKind, DefKind, Res};

compiler/rustc_parse/src/errors.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use rustc_ast::token::Token;
22
use rustc_ast::{Path, Visibility};
3-
use rustc_errors::{
4-
fluent, AddToDiagnostic, Applicability, EmissionGuarantee, HelpUseLatestEdition, IntoDiagnostic,
5-
};
3+
use rustc_errors::{fluent, AddToDiagnostic, Applicability, EmissionGuarantee, IntoDiagnostic};
64
use rustc_macros::{Diagnostic, Subdiagnostic};
75
use rustc_session::errors::ExprParenthesesNeeded;
6+
use rustc_span::edition::{Edition, LATEST_STABLE_EDITION};
87
use rustc_span::symbol::Ident;
98
use rustc_span::{Span, Symbol};
109

@@ -1916,3 +1915,24 @@ pub(crate) struct NegativeBoundsNotSupportedSugg {
19161915
pub num_bounds: usize,
19171916
pub fixed: String,
19181917
}
1918+
1919+
#[derive(Subdiagnostic)]
1920+
pub enum HelpUseLatestEdition {
1921+
#[help(parse_help_set_edition_cargo)]
1922+
#[note(parse_note_edition_guide)]
1923+
Cargo { edition: Edition },
1924+
#[help(parse_help_set_edition_standalone)]
1925+
#[note(parse_note_edition_guide)]
1926+
Standalone { edition: Edition },
1927+
}
1928+
1929+
impl HelpUseLatestEdition {
1930+
pub fn new() -> Self {
1931+
let edition = LATEST_STABLE_EDITION;
1932+
if std::env::var_os("CARGO").is_some() {
1933+
Self::Cargo { edition }
1934+
} else {
1935+
Self::Standalone { edition }
1936+
}
1937+
}
1938+
}

compiler/rustc_parse/src/parser/expr.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ use crate::errors::{
1111
ComparisonInterpretedAsGeneric, ComparisonOrShiftInterpretedAsGenericSugg,
1212
DoCatchSyntaxRemoved, DotDotDot, EqFieldInit, ExpectedElseBlock, ExpectedEqForLetExpr,
1313
ExpectedExpressionFoundLet, FieldExpressionWithGeneric, FloatLiteralRequiresIntegerPart,
14-
FoundExprWouldBeStmt, IfExpressionLetSomeSub, IfExpressionMissingCondition,
15-
IfExpressionMissingThenBlock, IfExpressionMissingThenBlockSub, InvalidBlockMacroSegment,
16-
InvalidComparisonOperator, InvalidComparisonOperatorSub, InvalidInterpolatedExpression,
17-
InvalidLiteralSuffixOnTupleIndex, InvalidLogicalOperator, InvalidLogicalOperatorSub,
18-
LabeledLoopInBreak, LeadingPlusNotSupported, LeftArrowOperator, LifetimeInBorrowExpression,
19-
MacroInvocationWithQualifiedPath, MalformedLoopLabel, MatchArmBodyWithoutBraces,
20-
MatchArmBodyWithoutBracesSugg, MissingCommaAfterMatchArm, MissingDotDot, MissingInInForLoop,
21-
MissingInInForLoopSub, MissingSemicolonBeforeArray, NoFieldsForFnCall, NotAsNegationOperator,
22-
NotAsNegationOperatorSub, OuterAttributeNotAllowedOnIfElse, ParenthesesWithStructFields,
14+
FoundExprWouldBeStmt, HelpUseLatestEdition, IfExpressionLetSomeSub,
15+
IfExpressionMissingCondition, IfExpressionMissingThenBlock, IfExpressionMissingThenBlockSub,
16+
InvalidBlockMacroSegment, InvalidComparisonOperator, InvalidComparisonOperatorSub,
17+
InvalidInterpolatedExpression, InvalidLiteralSuffixOnTupleIndex, InvalidLogicalOperator,
18+
InvalidLogicalOperatorSub, LabeledLoopInBreak, LeadingPlusNotSupported, LeftArrowOperator,
19+
LifetimeInBorrowExpression, MacroInvocationWithQualifiedPath, MalformedLoopLabel,
20+
MatchArmBodyWithoutBraces, MatchArmBodyWithoutBracesSugg, MissingCommaAfterMatchArm,
21+
MissingDotDot, MissingInInForLoop, MissingInInForLoopSub, MissingSemicolonBeforeArray,
22+
NoFieldsForFnCall, NotAsNegationOperator, NotAsNegationOperatorSub,
23+
OuterAttributeNotAllowedOnIfElse, ParenthesesWithStructFields,
2324
RequireColonAfterLabeledExpression, ShiftInterpretedAsGeneric, StructLiteralNotAllowedHere,
2425
StructLiteralNotAllowedHereSugg, TildeAsUnaryOperator, UnexpectedIfWithIf,
2526
UnexpectedTokenAfterLabel, UnexpectedTokenAfterLabelSugg, WrapExpressionInParentheses,
@@ -39,8 +40,8 @@ use rustc_ast::{Arm, Async, BlockCheckMode, Expr, ExprKind, Label, Movability, R
3940
use rustc_ast::{ClosureBinder, MetaItemLit, StmtKind};
4041
use rustc_ast_pretty::pprust;
4142
use rustc_errors::{
42-
AddToDiagnostic, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
43-
HelpUseLatestEdition, IntoDiagnostic, PResult, StashKey,
43+
AddToDiagnostic, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic,
44+
PResult, StashKey,
4445
};
4546
use rustc_session::errors::{report_lit_error, ExprParenthesesNeeded};
4647
use rustc_session::lint::builtin::BREAK_WITH_LABEL_AND_LOOP;

compiler/rustc_parse/src/parser/item.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::errors::{
33
BoundsNotAllowedOnTraitAliases, ConstGlobalCannotBeMutable, ConstLetMutuallyExclusive,
44
DefaultNotFollowedByItem, DocCommentDoesNotDocumentAnything, EnumStructMutuallyExclusive,
55
ExpectedTraitInTraitImplFoundType, ExternCrateNameWithDashes, ExternCrateNameWithDashesSugg,
6-
ExternItemCannotBeConst, MissingConstType, MissingForInTraitImpl,
6+
ExternItemCannotBeConst, HelpUseLatestEdition, MissingConstType, MissingForInTraitImpl,
77
MissingKeywordForItemDefinition, MissingTraitInTraitImpl, SelfArgumentPointer,
88
TraitAliasCannotBeAuto, TraitAliasCannotBeUnsafe, UnexpectedTokenAfterStructName,
99
UseEmptyBlockNotSemi, VisibilityNotFollowedByItem,
@@ -26,8 +26,8 @@ use rustc_ast::{FnHeader, ForeignItem, Path, PathSegment, Visibility, Visibility
2626
use rustc_ast::{MacCall, MacDelimiter};
2727
use rustc_ast_pretty::pprust;
2828
use rustc_errors::{
29-
struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed, HelpUseLatestEdition,
30-
IntoDiagnostic, PResult, StashKey,
29+
struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic, PResult,
30+
StashKey,
3131
};
3232
use rustc_span::edition::Edition;
3333
use rustc_span::lev_distance::lev_distance;

0 commit comments

Comments
 (0)