Skip to content

Commit baab6c1

Browse files
committed
Migrate GenericParamWrongOrder
1 parent 18cd17d commit baab6c1

File tree

5 files changed

+68
-41
lines changed

5 files changed

+68
-41
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -686,11 +686,7 @@ impl<'a> AstValidator<'a> {
686686

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

750746
ordered_params += ">";
751747

752-
for (param_ord, (max_param, spans)) in &out_of_order {
753-
let mut err = handler.struct_span_err(
754-
spans.clone(),
755-
&format!(
756-
"{} parameters must be declared prior to {} parameters",
757-
param_ord, max_param,
758-
),
759-
);
760-
err.span_suggestion(
761-
span,
762-
"reorder the parameters: lifetimes, then consts and types",
763-
&ordered_params,
764-
Applicability::MachineApplicable,
765-
);
766-
err.emit();
748+
for (param_ord, (max_param, spans)) in out_of_order {
749+
session.emit_err(GenericParamWrongOrder {
750+
spans,
751+
param_kind: param_ord,
752+
max_param_kind: max_param,
753+
replace_span: span,
754+
correct_order: ordered_params.clone(),
755+
});
767756
}
768757
}
769758
}
@@ -1186,7 +1175,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11861175
}
11871176
}
11881177

1189-
validate_generic_param_order(self.err_handler(), &generics.params, generics.span);
1178+
validate_generic_param_order(self.session, &generics.params, generics.span);
11901179

11911180
for predicate in &generics.where_clause.predicates {
11921181
if let WherePredicate::EqPredicate(predicate) = predicate {

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Errors emitted by ast_passes.
22
3+
use rustc_ast::ParamKindOrd;
4+
use rustc_errors::{fluent, AddToDiagnostic, Applicability, Diagnostic, SubdiagnosticMessage};
35
use rustc_macros::{Diagnostic, Subdiagnostic};
46
use rustc_span::{Span, Symbol};
57

@@ -246,7 +248,7 @@ pub struct ImplAssocTyWithBound {
246248
pub span: Span,
247249
}
248250

249-
#[derive(SessionDiagnostic)]
251+
#[derive(Diagnostic)]
250252
#[diag(ast_passes::foreign_ty_with_generic_param)]
251253
#[note(ast_passes::more_extern_note)]
252254
pub struct ForeignTyWithGenericParam {
@@ -257,7 +259,7 @@ pub struct ForeignTyWithGenericParam {
257259
pub extern_span: Span,
258260
}
259261

260-
#[derive(SessionDiagnostic)]
262+
#[derive(Diagnostic)]
261263
#[diag(ast_passes::foreign_ty_with_where_clause)]
262264
#[note(ast_passes::more_extern_note)]
263265
pub struct ForeignTyWithWhereClause {
@@ -268,7 +270,7 @@ pub struct ForeignTyWithWhereClause {
268270
pub extern_span: Span,
269271
}
270272

271-
#[derive(SessionDiagnostic)]
273+
#[derive(Diagnostic)]
272274
#[diag(ast_passes::foreign_ty_with_body)]
273275
#[note(ast_passes::more_extern_note)]
274276
pub struct ForeignTyWithBody {
@@ -281,7 +283,7 @@ pub struct ForeignTyWithBody {
281283
pub extern_span: Span,
282284
}
283285

284-
#[derive(SessionDiagnostic)]
286+
#[derive(Diagnostic)]
285287
#[diag(ast_passes::foreign_static_with_body)]
286288
#[note(ast_passes::more_extern_note)]
287289
pub struct ForeignStaticWithBody {
@@ -294,7 +296,7 @@ pub struct ForeignStaticWithBody {
294296
pub extern_span: Span,
295297
}
296298

297-
#[derive(SessionDiagnostic)]
299+
#[derive(Diagnostic)]
298300
#[diag(ast_passes::foreign_fn_with_body)]
299301
#[help]
300302
#[note(ast_passes::more_extern_note)]
@@ -308,7 +310,7 @@ pub struct ForeignFnWithBody {
308310
pub extern_span: Span,
309311
}
310312

311-
#[derive(SessionDiagnostic)]
313+
#[derive(Diagnostic)]
312314
#[diag(ast_passes::foreign_fn_with_qualifier)]
313315
pub struct ForeignFnWithQualifier {
314316
#[primary_span]
@@ -319,7 +321,7 @@ pub struct ForeignFnWithQualifier {
319321
pub replace_span: Span,
320322
}
321323

322-
#[derive(SessionDiagnostic)]
324+
#[derive(Diagnostic)]
323325
#[diag(ast_passes::foreign_item_non_ascii)]
324326
#[note]
325327
pub struct ForeignItemNonAscii {
@@ -329,29 +331,29 @@ pub struct ForeignItemNonAscii {
329331
pub extern_span: Span,
330332
}
331333

332-
#[derive(SessionDiagnostic)]
334+
#[derive(Diagnostic)]
333335
#[diag(ast_passes::forbidden_c_var_args)]
334336
pub struct ForbiddenCVarArgs {
335337
#[primary_span]
336338
pub span: Span,
337339
}
338340

339-
#[derive(SessionDiagnostic)]
341+
#[derive(Diagnostic)]
340342
#[diag(ast_passes::unnamed_assoc_const)]
341343
pub struct UnnamedAssocConst {
342344
#[primary_span]
343345
#[label]
344346
pub span: Span,
345347
}
346348

347-
#[derive(SessionDiagnostic)]
349+
#[derive(Diagnostic)]
348350
#[diag(ast_passes::nomangle_item_non_ascii, code = "E0754")]
349351
pub struct NomangleItemNonAscii {
350352
#[primary_span]
351353
pub span: Span,
352354
}
353355

354-
#[derive(SessionDiagnostic)]
356+
#[derive(Diagnostic)]
355357
#[diag(ast_passes::mod_file_item_non_ascii, code = "E0754")]
356358
#[help]
357359
pub struct ModFileItemNonAscii {
@@ -360,7 +362,7 @@ pub struct ModFileItemNonAscii {
360362
pub name: Symbol,
361363
}
362364

363-
#[derive(SessionDiagnostic)]
365+
#[derive(Diagnostic)]
364366
#[diag(ast_passes::auto_trait_with_generic_param, code = "E0567")]
365367
pub struct AutoTraitWithGenericParam {
366368
#[primary_span]
@@ -370,7 +372,7 @@ pub struct AutoTraitWithGenericParam {
370372
pub ident_span: Span,
371373
}
372374

373-
#[derive(SessionDiagnostic)]
375+
#[derive(Diagnostic)]
374376
#[diag(ast_passes::auto_trait_with_super_trait_or_where_clause, code = "E0568")]
375377
pub struct AutoTraitWithSuperTraitOrWhereClause {
376378
#[primary_span]
@@ -380,7 +382,7 @@ pub struct AutoTraitWithSuperTraitOrWhereClause {
380382
pub ident_span: Span,
381383
}
382384

383-
#[derive(SessionDiagnostic)]
385+
#[derive(Diagnostic)]
384386
#[diag(ast_passes::auto_trait_with_assoc_item, code = "E0380")]
385387
pub struct AutoTraitWithAssocItem {
386388
#[primary_span]
@@ -391,7 +393,7 @@ pub struct AutoTraitWithAssocItem {
391393
pub ident_span: Span,
392394
}
393395

394-
#[derive(SessionDiagnostic)]
396+
#[derive(Diagnostic)]
395397
#[diag(ast_passes::generic_arg_after_constraint)]
396398
pub struct GenericArgAfterConstraint {
397399
#[primary_span]
@@ -409,28 +411,28 @@ pub struct GenericArgAfterConstraint {
409411
pub correct_order: String,
410412
}
411413

412-
#[derive(SessionDiagnostic)]
414+
#[derive(Diagnostic)]
413415
#[diag(ast_passes::fn_ptr_ty_with_pat, code = "E0561")]
414416
pub struct FnPtrTyWithPat {
415417
#[primary_span]
416418
pub span: Span,
417419
}
418420

419-
#[derive(SessionDiagnostic)]
421+
#[derive(Diagnostic)]
420422
#[diag(ast_passes::multiple_explicit_lifetime_bound, code = "E0226")]
421423
pub struct MultipleExplicitLifetimeBound {
422424
#[primary_span]
423425
pub span: Span,
424426
}
425427

426-
#[derive(SessionDiagnostic)]
428+
#[derive(Diagnostic)]
427429
#[diag(ast_passes::impl_trait_ty_in_path_param, code = "E0667")]
428430
pub struct ImplTraitTyInPathParam {
429431
#[primary_span]
430432
pub span: Span,
431433
}
432434

433-
#[derive(SessionDiagnostic)]
435+
#[derive(Diagnostic)]
434436
#[diag(ast_passes::impl_trait_ty_nested, code = "E0666")]
435437
pub struct ImplTraitTyNested {
436438
#[primary_span]
@@ -440,9 +442,21 @@ pub struct ImplTraitTyNested {
440442
pub outer_span: Span,
441443
}
442444

443-
#[derive(SessionDiagnostic)]
445+
#[derive(Diagnostic)]
444446
#[diag(ast_passes::impl_trait_ty_without_trait_bound)]
445447
pub struct ImplTraitTyWithoutTraitBound {
446448
#[primary_span]
447449
pub span: Span,
448450
}
451+
452+
#[derive(Diagnostic)]
453+
#[diag(ast_passes::generic_param_wrong_order)]
454+
pub struct GenericParamWrongOrder {
455+
#[primary_span]
456+
pub spans: Vec<Span>,
457+
pub param_kind: ParamKindOrd,
458+
pub max_param_kind: ParamKindOrd,
459+
#[suggestion(code = "{correct_order}", applicability = "machine-applicable")]
460+
pub replace_span: Span,
461+
pub correct_order: String,
462+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,15 @@ ast_passes_impl_trait_ty_without_trait_bound =
215215
216216
ast_passes_deprecated_extern_missing_abi =
217217
extern declarations without an explicit ABI are deprecated
218+
219+
ast_passes_generic_param_wrong_order =
220+
{ $param_kind ->
221+
[lifetime] lifetime
222+
[type] type
223+
*[const] const
224+
} parameters must be declared prior to { $max_param_kind ->
225+
[lifetime] lifetime
226+
[type] type
227+
*[const] const
228+
} parameters
229+
.suggestion = reorder the parameters: lifetimes, then consts and types

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{
33
CodeSuggestion, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Level, MultiSpan,
44
SubdiagnosticMessage, Substitution, SubstitutionPart, SuggestionStyle,
55
};
6+
67
use rustc_data_structures::fx::FxHashMap;
78
use rustc_error_messages::fluent_value_from_str_list_sep_by_and;
89
use rustc_error_messages::FluentValue;

compiler/rustc_errors/src/diagnostic_impls.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ impl IntoDiagnosticArg for hir::ConstContext {
147147
}
148148
}
149149

150+
impl IntoDiagnosticArg for ast::ParamKindOrd {
151+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
152+
DiagnosticArgValue::Str(Cow::Borrowed(match self {
153+
ast::ParamKindOrd::Lifetime => "lifetime",
154+
ast::ParamKindOrd::Type => "type",
155+
ast::ParamKindOrd::Const => "const",
156+
ast::ParamKindOrd::Infer => "infer",
157+
}))
158+
}
159+
}
160+
150161
impl IntoDiagnosticArg for ast::Path {
151162
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
152163
DiagnosticArgValue::Str(Cow::Owned(pprust::path_to_string(&self)))

0 commit comments

Comments
 (0)