8000 [WIP] Migrate rustc_ast_passes diagnostics to SessionDiagnostic and translatable messages (second part) by finalchild · Pull Request #101657 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

[WIP] Migrate rustc_ast_passes diagnostics to SessionDiagnostic and translatable messages (second part) #101657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d9e8e42
Migrate check_type_no_bounds
finalchild Sep 9, 2022
ae28c07
Rename (assoc/foreign)_type to (assoc/foreign)_ty
finalchild Sep 9, 2022
8635da7
Rename AssocTyWithoutBody to ImplAssocTyWithoutBody
finalchild Sep 9, 2022
92b96e6
Migrate check_foreign_ty_genericless
finalchild Sep 9, 2022
69cb057
Migrate check_foreign_kind_bodyless and fix prev
finalchild Sep 10, 2022
8384368
Tidy
finalchild Sep 10, 2022
79ad624
Migrate ForeignFnWith(Body/Qualifier) and fix prev
finalchild Sep 10, 2022
2aea631
Migrate ForeignItemNonAscii, ForbiddenCVarArgs, UnnamedAssocConst, No…
finalchild Sep 10, 2022
00d739b
Migrate AutoTraitWith(GenericParam/SuperTraitOrWhereClause/AssocItem)
finalchild Sep 10, 2022
c8de335
Migrate GenericArgAfterConstraint and fix prev
finalchild Sep 10, 2022
18cd17d
Migrate FnPtrTyWithPat, MultipleExplicitLifetimeBound, ImplTraitTyInP…
finalchild Sep 10, 2022
baab6c1
Migrate GenericParamWrongOrder
finalchild Sep 10, 2022
560d445
Fix rebase
finalchild Sep 11, 2022
834db8b
Migrate the rest of ast_validation.rs
finalchild Sep 11, 2022
af29599
Fix raw identifiers not handled correctly in EqualityConstraintToAsso… 8000
finalchild Sep 12, 2022
798064c
Fix rebase mistake
finalchild Sep 29, 2022
4f4248f
Fix rebase mistake
finalchild Sep 29, 2022
e3c6279
resolve rebase error
finalchild Oct 13, 2022
2a0f2cd
Remove redundant change
finalchild Oct 14, 2022
7ca0eb8
fix rebase mistakes
finalchild Oct 21, 2022
facf6ac
fix rebase mistake
finalchild Oct 21, 2022
8575873
fix rebase
finalchild Oct 26, 2022
56d7426
fix rebase
finalchild Jan 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Migrate GenericParamWrongOrder
  • Loading branch information
finalchild committed Jan 25, 2023
commit baab6c175cf5cee2f0e16d6450f6f5b3acdd561c
31 changes: 10 additions & 21 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,11 +686,7 @@ impl<'a> AstValidator<'a> {

/// Checks that generic parameters are in the correct order,
/// which is lifetimes, then types and then consts. (`<'a, T, const N: usize>`)
fn validate_generic_param_order(
handler: &rustc_errors::Handler,
generics: &[GenericParam],
span: Span,
) {
fn validate_generic_param_order(session: &Session, generics: &[GenericParam], span: Span) {
let mut max_param: Option<ParamKindOrd> = None;
let mut out_of_order = FxHashMap::default();
let mut param_idents = Vec::with_capacity(generics.len());
Expand Down Expand Up @@ -749,21 +745,14 @@ fn validate_generic_param_order(

ordered_params += ">";

for (param_ord, (max_param, spans)) in &out_of_order {
let mut err = handler.struct_span_err(
spans.clone(),
&format!(
"{} parameters must be declared prior to {} parameters",
param_ord, max_param,
),
);
err.span_suggestion(
span,
"reorder the parameters: lifetimes, then consts and types",
&ordered_params,
Applicability::MachineApplicable,
);
err.emit();
for (param_ord, (max_param, spans)) in out_of_order {
session.emit_err(GenericParamWrongOrder {
spans,
param_kind: param_ord,
max_param_kind: max_param,
replace_span: span,
correct_order: ordered_params.clone(),
});
}
}
}
Expand Down Expand Up @@ -1186,7 +1175,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}
}

validate_generic_param_order(self.err_handler(), &generics.params, generics.span);
validate_generic_param_order(self.session, &generics.params, generics.span);

for predicate in &generics.where_clause.predicates {
if let WherePredicate::EqPredicate(predicate) = predicate {
Expand Down
54 changes: 34 additions & 20 deletions compiler/rustc_ast_passes/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Errors emitted by ast_passes.

use rustc_ast::ParamKindOrd;
use rustc_errors::{fluent, AddToDiagnostic, Applicability, Diagnostic, SubdiagnosticMessage};
use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::{Span, Symbol};

Expand Down Expand Up @@ -246,7 +248,7 @@ pub struct ImplAssocTyWithBound {
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::foreign_ty_with_generic_param)]
#[note(ast_passes::more_extern_note)]
pub struct ForeignTyWithGenericParam {
Expand All @@ -257,7 +259,7 @@ pub struct ForeignTyWithGenericParam {
pub extern_span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::foreign_ty_with_where_clause)]
#[note(ast_passes::more_extern_note)]
pub struct ForeignTyWithWhereClause {
Expand All @@ -268,7 +270,7 @@ pub struct ForeignTyWithWhereClause {
pub extern_span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::foreign_ty_with_body)]
#[note(ast_passes::more_extern_note)]
pub struct ForeignTyWithBody {
Expand All @@ -281,7 +283,7 @@ pub struct ForeignTyWithBody {
pub extern_span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::foreign_static_with_body)]
#[note(ast_passes::more_extern_note)]
pub struct ForeignStaticWithBody {
Expand All @@ -294,7 +296,7 @@ pub struct ForeignStaticWithBody {
pub extern_span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::foreign_fn_with_body)]
#[help]
#[note(ast_passes::more_extern_note)]
Expand All @@ -308,7 +310,7 @@ pub struct ForeignFnWithBody {
pub extern_span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::foreign_fn_with_qualifier)]
pub struct ForeignFnWithQualifier {
#[primary_span]
Expand All @@ -319,7 +321,7 @@ pub struct ForeignFnWithQualifier {
pub replace_span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::foreign_item_non_ascii)]
#[note]
pub struct ForeignItemNonAscii {
Expand All @@ -329,29 +331,29 @@ pub struct ForeignItemNonAscii {
pub extern_span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::forbidden_c_var_args)]
pub struct ForbiddenCVarArgs {
#[primary_span]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::unnamed_assoc_const)]
pub struct UnnamedAssocConst {
#[primary_span]
#[label]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::nomangle_item_non_ascii, code = "E0754")]
pub struct NomangleItemNonAscii {
#[primary_span]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::mod_file_item_non_ascii, code = "E0754")]
#[help]
pub struct ModFileItemNonAscii {
Expand All @@ -360,7 +362,7 @@ pub struct ModFileItemNonAscii {
pub name: Symbol,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::auto_trait_with_generic_param, code = "E0567")]
pub struct AutoTraitWithGenericParam {
#[primary_span]
Expand All @@ -370,7 +372,7 @@ pub struct AutoTraitWithGenericParam {
pub ident_span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::auto_trait_with_super_trait_or_where_clause, code = "E0568")]
pub struct AutoTraitWithSuperTraitOrWhereClause {
#[primary_span]
Expand All @@ -380,7 +382,7 @@ pub struct AutoTraitWithSuperTraitOrWhereClause {
pub ident_span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::auto_trait_with_assoc_item, code = "E0380")]
pub struct AutoTraitWithAssocItem {
#[primary_span]
Expand All @@ -391,7 +393,7 @@ pub struct AutoTraitWithAssocItem {
pub ident_span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::generic_arg_after_constraint)]
pub struct GenericArgAfterConstraint {
#[primary_span]
Expand All @@ -409,28 +411,28 @@ pub struct GenericArgAfterConstraint {
pub correct_order: String,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::fn_ptr_ty_with_pat, code = "E0561")]
pub struct FnPtrTyWithPat {
#[primary_span]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::multiple_explicit_lifetime_bound, code = "E0226")]
pub struct MultipleExplicitLifetimeBound {
#[primary_span]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::impl_trait_ty_in_path_param, code = "E0667")]
pub struct ImplTraitTyInPathParam {
#[primary_span]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::impl_trait_ty_nested, code = "E0666")]
pub struct ImplTraitTyNested {
#[primary_span]
Expand All @@ -440,9 +442,21 @@ pub struct ImplTraitTyNested {
pub outer_span: Span,
}

#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(ast_passes::impl_trait_ty_without_trait_bound)]
pub struct ImplTraitTyWithoutTraitBound {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(ast_passes::generic_param_wrong_order)]
pub struct GenericParamWrongOrder {
#[primary_span]
pub spans: Vec<Span>,
pub param_kind: ParamKindOrd,
pub max_param_kind: ParamKindOrd,
#[suggestion(code = "{correct_order}", applicability = "machine-applicable")]
pub replace_span: Span,
pub correct_order: String,
}
12 changes: 12 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/ast_passes.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,15 @@ ast_passes_impl_trait_ty_without_trait_bound =

ast_passes_deprecated_extern_missing_abi =
extern declarations without an explicit ABI are deprecated

ast_passes_generic_param_wrong_order =
{ $param_kind ->
[lifetime] lifetime
[type] type
*[const] const
} parameters must be declared prior to { $max_param_kind ->
[lifetime] lifetime
[type] type
*[const] const
} parameters
.suggestion = reorder the parameters: lifetimes, then consts and types
1 change: 1 addition & 0 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
CodeSuggestion, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Level, MultiSpan,
SubdiagnosticMessage, Substitution, SubstitutionPart, SuggestionStyle,
};

use rustc_data_structures::fx::FxHashMap;
use rustc_error_messages::fluent_value_from_str_list_sep_by_and;
use rustc_error_messages::FluentValue;
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_errors/src/diagnostic_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ impl IntoDiagnosticArg for hir::ConstContext {
}
}

impl IntoDiagnosticArg for ast::ParamKindOrd {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Borrowed(match self {
ast::ParamKindOrd::Lifetime => "lifetime",
ast::ParamKindOrd::Type => "type",
ast::ParamKindOrd::Const => "const",
ast::ParamKindOrd::Infer => "infer",
}))
}
}

impl IntoDiagnosticArg for ast::Path {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(pprust::path_to_string(&self)))
Expand Down
0