From d9e8e4250db8675361bc053b08d4c1b75b87ec9b Mon Sep 17 00:00:00 2001 From: finalchild Date: Sat, 10 Sep 2022 05:14:44 +0900 Subject: [PATCH 01/23] Migrate check_type_no_bounds --- .../rustc_ast_passes/src/ast_validation.rs | 17 ++++++++------- compiler/rustc_ast_passes/src/errors.rs | 21 +++++++++++++++++++ .../locales/en-US/ast_passes.ftl | 9 ++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 902b4b1a1ecfe..bbbd56a04b5d1 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -20,7 +20,7 @@ use rustc_session::lint::builtin::{ DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, PATTERNS_IN_FNS_WITHOUT_BODY, }; use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer}; -use rustc_session::Session; +use rustc_session::{Session, SessionDiagnostic}; use rustc_span::source_map::Spanned; use rustc_span::symbol::{kw, sym, Ident}; use rustc_span::Span; @@ -404,15 +404,16 @@ impl<'a> AstValidator<'a> { } } - fn check_type_no_bounds(&self, bounds: &[GenericBound], ctx: &str) { + fn check_type_no_bounds(&self, bounds: &[GenericBound], create_diag: impl FnOnce(Span) -> D) + where + D: SessionDiagnostic<'a>, + { let span = match bounds { [] => return, [b0] => b0.span(), [b0, .., bl] => b0.span().to(bl.span()), }; - self.err_handler() - .struct_span_err(span, &format!("bounds on `type`s in {} have no effect", ctx)) - .emit(); + self.session.emit_err(create_diag(span)); } fn check_foreign_ty_genericless(&self, generics: &Generics, where_span: Span) { @@ -1205,7 +1206,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { replace_span: self.ending_semi_or_hi(item.span), }); } - self.check_type_no_bounds(bounds, "this context"); + self.check_type_no_bounds(bounds, |span| TyAliasWithBound { span }); if where_clauses.1.0 { let mut err = self.err_handler().struct_span_err( where_clauses.1.1, @@ -1241,7 +1242,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { }) => { self.check_defaultness(fi.span, *defaultness); self.check_foreign_kind_bodyless(fi.ident, "type", ty.as_ref().map(|b| b.span)); - self.check_type_no_bounds(bounds, "`extern` blocks"); + self.check_type_no_bounds(bounds, |span| ForeignTypeWithBound { span }); self.check_foreign_ty_genericless(generics, where_clauses.0.1); self.check_foreign_item_ascii_only(fi.ident); } @@ -1539,7 +1540,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { replace_span: self.ending_semi_or_hi(item.span), }); } - self.check_type_no_bounds(bounds, "`impl`s"); + self.check_type_no_bounds(bounds, |span| ImplAssocTypeWithBound { span }); if ty.is_some() { self.check_gat_where( item.id, diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index 09e262452b11d..c1864b2b4558f 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -224,3 +224,24 @@ pub enum ExternBlockSuggestion { abi: Symbol, }, } + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::ty_alias_with_bound)] +pub struct TyAliasWithBound { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::foreign_type_with_bound)] +pub struct ForeignTypeWithBound { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::impl_assoc_type_with_bound)] +pub struct ImplAssocTypeWithBound { + #[primary_span] + pub span: Span, +} diff --git a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl index 5f28839f136d6..90ad5b93913d5 100644 --- a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl @@ -90,3 +90,12 @@ ast_passes_fn_without_body = .suggestion = provide a definition for the function ast_passes_extern_block_suggestion = if you meant to declare an externally defined function, use an `extern` block + +ast_passes_ty_alias_with_bound = + bounds on `type`s in this context have no effect + +ast_passes_foreign_type_with_bound = + bounds on `type`s in `extern` blocks have no effect + +ast_passes_impl_assoc_type_with_bound = + bounds on `type`s in `impl`s have no effect From ae28c07ea7851d3cae5d45678e004cd0656b823a Mon Sep 17 00:00:00 2001 From: finalchild Date: Sat, 10 Sep 2022 05:20:42 +0900 Subject: [PATCH 02/23] Rename (assoc/foreign)_type to (assoc/foreign)_ty --- .../rustc_ast_passes/src/ast_validation.rs | 6 +++--- compiler/rustc_ast_passes/src/errors.rs | 18 +++++++++--------- .../locales/en-US/ast_passes.ftl | 6 +++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index bbbd56a04b5d1..0ff120fb7e03a 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1242,7 +1242,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { }) => { self.check_defaultness(fi.span, *defaultness); self.check_foreign_kind_bodyless(fi.ident, "type", ty.as_ref().map(|b| b.span)); - self.check_type_no_bounds(bounds, |span| ForeignTypeWithBound { span }); + self.check_type_no_bounds(bounds, |span| ForeignTyWithBound { span }); self.check_foreign_ty_genericless(generics, where_clauses.0.1); self.check_foreign_item_ascii_only(fi.ident); } @@ -1535,12 +1535,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> { .. }) => { if ty.is_none() { - self.session.emit_err(AssocTypeWithoutBody { + self.session.emit_err(AssocTyWithoutBody { span: item.span, replace_span: self.ending_semi_or_hi(item.span), }); } - self.check_type_no_bounds(bounds, |span| ImplAssocTypeWithBound { span }); + self.check_type_no_bounds(bounds, |span| ImplAssocTyWithBound { span }); if ty.is_some() { self.check_gat_where( item.id, diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index c1864b2b4558f..7e88c55fe3568 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -160,7 +160,7 @@ pub struct AssocFnWithoutBody { } #[derive(Diagnostic)] -#[diag(ast_passes_assoc_type_without_body)] +#[diag(ast_passes_assoc_ty_without_body)] pub struct AssocTypeWithoutBody { #[primary_span] pub span: Span, @@ -225,23 +225,23 @@ pub enum ExternBlockSuggestion { }, } -#[derive(SessionDiagnostic)] -#[diag(ast_passes::ty_alias_with_bound)] +#[derive(Diagnostic)] +#[diag(ast_passes_ty_alias_with_bound)] pub struct TyAliasWithBound { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(ast_passes::foreign_type_with_bound)] -pub struct ForeignTypeWithBound { +#[derive(Diagnostic)] +#[diag(ast_passes_foreign_ty_with_bound)] +pub struct ForeignTyWithBound { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(ast_passes::impl_assoc_type_with_bound)] -pub struct ImplAssocTypeWithBound { +#[derive(Diagnostic)] +#[diag(ast_passes_impl_assoc_ty_with_bound)] +pub struct ImplAssocTyWithBound { #[primary_span] pub span: Span, } diff --git a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl index 90ad5b93913d5..c924559de9889 100644 --- a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl @@ -69,7 +69,7 @@ ast_passes_assoc_fn_without_body = associated function in `impl` without body .suggestion = provide a definition for the function -ast_passes_assoc_type_without_body = +ast_passes_assoc_ty_without_body = associated type in `impl` without body .suggestion = provide a definition for the type @@ -94,8 +94,8 @@ ast_passes_extern_block_suggestion = if you meant to declare an externally defin ast_passes_ty_alias_with_bound = bounds on `type`s in this context have no effect -ast_passes_foreign_type_with_bound = +ast_passes_foreign_ty_with_bound = bounds on `type`s in `extern` blocks have no effect -ast_passes_impl_assoc_type_with_bound = +ast_passes_impl_assoc_ty_with_bound = bounds on `type`s in `impl`s have no effect From 8635da7d8a1a4c777ba014403d48f0f780bc43a3 Mon Sep 17 00:00:00 2001 From: finalchild Date: Sat, 10 Sep 2022 05:24:01 +0900 Subject: [PATCH 03/23] Rename AssocTyWithoutBody to ImplAssocTyWithoutBody --- compiler/rustc_ast_passes/src/ast_validation.rs | 2 +- compiler/rustc_ast_passes/src/errors.rs | 4 ++-- compiler/rustc_error_messages/locales/en-US/ast_passes.ftl | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 0ff120fb7e03a..dc548ff7787a6 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1535,7 +1535,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { .. }) => { if ty.is_none() { - self.session.emit_err(AssocTyWithoutBody { + self.session.emit_err(ImplAssocTyWithoutBody { span: item.span, replace_span: self.ending_semi_or_hi(item.span), }); diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index 7e88c55fe3568..e9b0c5ed2b71f 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -160,8 +160,8 @@ pub struct AssocFnWithoutBody { } #[derive(Diagnostic)] -#[diag(ast_passes_assoc_ty_without_body)] -pub struct AssocTypeWithoutBody { +#[diag(ast_passes_impl_assoc_ty_without_body)] +pub struct ImplAssocTyWithoutBody { #[primary_span] pub span: Span, #[suggestion(code = " = ;", applicability = "has-placeholders")] diff --git a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl index c924559de9889..06ba026294962 100644 --- a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl @@ -69,7 +69,7 @@ ast_passes_assoc_fn_without_body = associated function in `impl` without body .suggestion = provide a definition for the function -ast_passes_assoc_ty_without_body = +ast_passes_impl_assoc_ty_without_body = associated type in `impl` without body .suggestion = provide a definition for the type From 92b96e66e09c07662625d6242b5c4b76d72b3010 Mon Sep 17 00:00:00 2001 From: finalchild Date: Sat, 10 Sep 2022 05:47:49 +0900 Subject: [PATCH 04/23] Migrate check_foreign_ty_genericless --- .../rustc_ast_passes/src/ast_validation.rs | 27 ++++++------------- compiler/rustc_ast_passes/src/errors.rs | 22 +++++++++++++++ .../locales/en-US/ast_passes.ftl | 13 +++++++++ 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index dc548ff7787a6..150bf230f6b49 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -417,29 +417,18 @@ impl<'a> AstValidator<'a> { } fn check_foreign_ty_genericless(&self, generics: &Generics, where_span: Span) { - let cannot_have = |span, descr, remove_descr| { - self.err_handler() - .struct_span_err( - span, - &format!("`type`s inside `extern` blocks cannot have {}", descr), - ) - .span_suggestion( - span, - &format!("remove the {}", remove_descr), - "", - Applicability::MaybeIncorrect, - ) - .span_label(self.current_extern_span(), "`extern` block begins here") - .note(MORE_EXTERN) - .emit(); - }; - if !generics.params.is_empty() { - cannot_have(generics.span, "generic parameters", "generic parameters"); + self.session.emit_err(ForeignTyWithGenericParam { + span: generics.span, + extern_span: self.current_extern_span(), + }); } if !generics.where_clause.predicates.is_empty() { - cannot_have(where_span, "`where` clauses", "`where` clause"); + self.session.emit_err(ForeignTyWithWhereClause { + span: where_span, + extern_span: self.current_extern_span(), + }); } } diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index e9b0c5ed2b71f..fa02cf3c69d42 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -245,3 +245,25 @@ pub struct ImplAssocTyWithBound { #[primary_span] pub span: Span, } + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::foreign_ty_with_generic_param)] +#[note(ast_passes::more_extern_note)] +pub struct ForeignTyWithGenericParam { + #[primary_span] + #[suggestion(code = "", applicability = "maybe-incorrect")] + pub span: Span, + #[label] + pub extern_span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::foreign_ty_with_where_clause)] +#[note(ast_passes::more_extern_note)] +pub struct ForeignTyWithWhereClause { + #[primary_span] + #[suggestion(code = "", applicability = "maybe-incorrect")] + pub span: Span, + #[label] + pub extern_span: Span, +} diff --git a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl index 06ba026294962..fec126f147129 100644 --- a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl @@ -1,3 +1,6 @@ +ast_passes_more_extern_note = + for more information, visit https://doc.rust-lang.org/std/keyword.extern.html + ast_passes_forbidden_let = `let` expressions are not supported here .note = only supported directly in conditions of `if` and `while` expressions @@ -99,3 +102,13 @@ ast_passes_foreign_ty_with_bound = ast_passes_impl_assoc_ty_with_bound = bounds on `type`s in `impl`s have no effect + +ast_passes_foreign_ty_with_generic_param = + `type`s inside `extern` blocks cannot have generic parameters + .suggestion = remove the generic parameters + .extern_block_label = `extern` block begins here + +ast_passes_foreign_ty_with_where_clause = + `type`s inside `extern` blocks cannot have `where` clauses + .suggestion = remove the `where` clause + .extern_block_label = `extern` block begins here From 69cb0571da8c03257830e0d4afec2985699f148e Mon Sep 17 00:00:00 2001 From: finalchild Date: Sat, 10 Sep 2022 16:12:03 +0900 Subject: [PATCH 05/23] Migrate check_foreign_kind_bodyless and fix prev --- .../rustc_ast_passes/src/ast_validation.rs | 24 +++++----------- compiler/rustc_ast_passes/src/errors.rs | 28 +++++++++++++++++-- .../locales/en-US/ast_passes.ftl | 18 +++++++++++- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 150bf230f6b49..0b7d15471918e 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -432,24 +432,14 @@ impl<'a> AstValidator<'a> { } } - fn check_foreign_kind_bodyless(&self, ident: Ident, kind: &str, body: Option) { + fn check_foreign_kind_bodyless(&self, ident: Ident, create_diag: impl FnOnce(Span, Span, Span) -> D, body: Option) + where + D: SessionDiagnostic<'a>, + { let Some(body) = body else { return; }; - self.err_handler() - .struct_span_err(ident.span, &format!("incorrect `{}` inside `extern` block", kind)) - .span_label(ident.span, "cannot have a body") - .span_label(body, "the invalid body") - .span_label( - self.current_extern_span(), - format!( - "`extern` blocks define existing foreign {0}s and {0}s \ - inside of them cannot have a body", - kind - ), - ) - .note(MORE_EXTERN) - .emit(); + self.session.emit_err(create_diag(ident.span, body, self.current_extern_span())); } /// An `fn` in `extern { ... }` cannot have a body `{ ... }`. @@ -1230,13 +1220,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> { .. }) => { self.check_defaultness(fi.span, *defaultness); - self.check_foreign_kind_bodyless(fi.ident, "type", ty.as_ref().map(|b| b.span)); + self.check_foreign_kind_bodyless(fi.ident, |span, body_span, extern_span| ForeignTyWithBody { span, body_span, extern_span }, ty.as_ref().map(|b| b.span)); self.check_type_no_bounds(bounds, |span| ForeignTyWithBound { span }); self.check_foreign_ty_genericless(generics, where_clauses.0.1); self.check_foreign_item_ascii_only(fi.ident); } ForeignItemKind::Static(_, _, body) => { - self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span)); + self.check_foreign_kind_bodyless(fi.ident, |span, body_span, extern_span| ForeignStaticWithBody { span, body_span, extern_span }, body.as_ref().map(|b| b.span)); self.check_foreign_item_ascii_only(fi.ident); } ForeignItemKind::MacCall(..) => {} diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index fa02cf3c69d42..767c4eaf67209 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -253,7 +253,7 @@ pub struct ForeignTyWithGenericParam { #[primary_span] #[suggestion(code = "", applicability = "maybe-incorrect")] pub span: Span, - #[label] + #[label(ast_passes::extern_block_label)] pub extern_span: Span, } @@ -264,6 +264,30 @@ pub struct ForeignTyWithWhereClause { #[primary_span] #[suggestion(code = "", applicability = "maybe-incorrect")] pub span: Span, - #[label] + #[label(ast_passes::extern_block_label)] + pub extern_span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::foreign_ty_with_body)] +#[note(ast_passes::more_extern_note)] +pub struct ForeignTyWithBody { + #[primary_span] + pub span: Span, + #[label(ast_passes::body_label)] + pub body_span: Span, + #[label(ast_passes::extern_block_label)] + pub extern_span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::foreign_static_with_body)] +#[note(ast_passes::more_extern_note)] +pub struct ForeignStaticWithBody { + #[primary_span] + pub span: Span, + #[label(ast_passes::body_label)] + pub body_span: Span, + #[label(ast_passes::extern_block_label)] pub extern_span: Span, } diff --git a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl index fec126f147129..672f6f89f7248 100644 --- a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl @@ -1,4 +1,4 @@ -ast_passes_more_extern_note = +-ast_passes_more_extern = for more information, visit https://doc.rust-lang.org/std/keyword.extern.html ast_passes_forbidden_let = @@ -107,8 +107,24 @@ ast_passes_foreign_ty_with_generic_param = `type`s inside `extern` blocks cannot have generic parameters .suggestion = remove the generic parameters .extern_block_label = `extern` block begins here + .more_extern_note = { -ast_passes_more_extern } ast_passes_foreign_ty_with_where_clause = `type`s inside `extern` blocks cannot have `where` clauses .suggestion = remove the `where` clause .extern_block_label = `extern` block begins here + .more_extern_note = { -ast_passes_more_extern } + +ast_passes_foreign_ty_with_body = + incorrect `type` inside `extern` block + .label = cannot have a body + .body_label = the invalid body + .extern_block_label = `extern` blocks define existing foreign types and types inside of them cannot have a body + .more_extern_note = { -ast_passes_more_extern } + +ast_passes_foreign_static_with_body = + incorrect `static` inside `extern` block + .label = cannot have a body + .body_label = the invalid body + .extern_block_label = `extern` blocks define existing foreign statics and statics inside of them cannot have a body + .more_extern_note = { -ast_passes_more_extern } From 83843680a38d90ce6f4b547f76e0e8affa1dfd28 Mon Sep 17 00:00:00 2001 From: finalchild Date: Sat, 10 Sep 2022 16:14:23 +0900 Subject: [PATCH 06/23] Tidy --- .../rustc_ast_passes/src/ast_validation.rs | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 0b7d15471918e..8d227249bacaa 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -432,8 +432,12 @@ impl<'a> AstValidator<'a> { } } - fn check_foreign_kind_bodyless(&self, ident: Ident, create_diag: impl FnOnce(Span, Span, Span) -> D, body: Option) - where + fn check_foreign_kind_bodyless( + &self, + ident: Ident, + create_diag: impl FnOnce(Span, Span, Span) -> D, + body: Option, + ) where D: SessionDiagnostic<'a>, { let Some(body) = body else { @@ -1220,13 +1224,29 @@ impl<'a> Visitor<'a> for AstValidator<'a> { .. }) => { self.check_defaultness(fi.span, *defaultness); - self.check_foreign_kind_bodyless(fi.ident, |span, body_span, extern_span| ForeignTyWithBody { span, body_span, extern_span }, ty.as_ref().map(|b| b.span)); + self.check_foreign_kind_bodyless( + fi.ident, + |span, body_span, extern_span| ForeignTyWithBody { + span, + body_span, + extern_span, + }, + ty.as_ref().map(|b| b.span), + ); self.check_type_no_bounds(bounds, |span| ForeignTyWithBound { span }); self.check_foreign_ty_genericless(generics, where_clauses.0.1); self.check_foreign_item_ascii_only(fi.ident); } ForeignItemKind::Static(_, _, body) => { - self.check_foreign_kind_bodyless(fi.ident, |span, body_span, extern_span| ForeignStaticWithBody { span, body_span, extern_span }, body.as_ref().map(|b| b.span)); + self.check_foreign_kind_bodyless( + fi.ident, + |span, body_span, extern_span| ForeignStaticWithBody { + span, + body_span, + extern_span, + }, + body.as_ref().map(|b| b.span), + ); self.check_foreign_item_ascii_only(fi.ident); } ForeignItemKind::MacCall(..) => {} From 79ad624f276388178d7bb93f0def112702b7e1bf Mon Sep 17 00:00:00 2001 From: finalchild Date: Sat, 10 Sep 2022 17:12:18 +0900 Subject: [PATCH 07/23] Migrate ForeignFnWith(Body/Qualifier) and fix prev --- .../rustc_ast_passes/src/ast_validation.rs | 43 +++++-------------- compiler/rustc_ast_passes/src/errors.rs | 27 ++++++++++++ .../locales/en-US/ast_passes.ftl | 13 ++++++ 3 files changed, 50 insertions(+), 33 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 8d227249bacaa..2050e84f269a1 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -30,9 +30,6 @@ use std::ops::{Deref, DerefMut}; use crate::errors::*; -const MORE_EXTERN: &str = - "for more information, visit https://doc.rust-lang.org/std/keyword.extern.html"; - /// Is `self` allowed semantically as the first parameter in an `FnDecl`? enum SelfSemantic { Yes, @@ -451,26 +448,11 @@ impl<'a> AstValidator<'a> { let Some(body) = body else { return; }; - self.err_handler() - .struct_span_err(ident.span, "incorrect function inside `extern` block") - .span_label(ident.span, "cannot have a body") - .span_suggestion( - body.span, - "remove the invalid body", - ";", - Applicability::MaybeIncorrect, - ) - .help( - "you might have meant to write a function accessible through FFI, \ - which can be done by writing `extern fn` outside of the `extern` block", - ) - .span_label( - self.current_extern_span(), - "`extern` blocks define existing foreign functions and functions \ - inside of them cannot have a body", - ) - .note(MORE_EXTERN) - .emit(); + self.session.emit_err(ForeignFnWithBody { + span: ident.span, + body_span: body.span, + extern_span: self.current_extern_span(), + }); } fn current_extern_span(&self) -> Span { @@ -480,16 +462,11 @@ impl<'a> AstValidator<'a> { /// An `fn` in `extern { ... }` cannot have qualifiers, e.g. `async fn`. fn check_foreign_fn_headerless(&self, ident: Ident, span: Span, header: FnHeader) { if header.has_qualifiers() { - self.err_handler() - .struct_span_err(ident.span, "functions in `extern` blocks cannot have qualifiers") - .span_label(self.current_extern_span(), "in this `extern` block") - .span_suggestion_verbose( - span.until(ident.span.shrink_to_lo()), - "remove the qualifiers", - "fn ", - Applicability::MaybeIncorrect, - ) - .emit(); + self.session.emit_err(ForeignFnWithQualifier { + span: ident.span, + extern_span: self.current_extern_span(), + replace_span: span.until(ident.span.shrink_to_lo()), + }); } } diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index 767c4eaf67209..e5a7e42f14218 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -273,6 +273,7 @@ pub struct ForeignTyWithWhereClause { #[note(ast_passes::more_extern_note)] pub struct ForeignTyWithBody { #[primary_span] + #[label] pub span: Span, #[label(ast_passes::body_label)] pub body_span: Span, @@ -285,9 +286,35 @@ pub struct ForeignTyWithBody { #[note(ast_passes::more_extern_note)] pub struct ForeignStaticWithBody { #[primary_span] + #[label] pub span: Span, #[label(ast_passes::body_label)] pub body_span: Span, #[label(ast_passes::extern_block_label)] pub extern_span: Span, } + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::foreign_fn_with_body)] +#[help] +#[note(ast_passes::more_extern_note)] +pub struct ForeignFnWithBody { + #[primary_span] + #[label] + pub span: Span, + #[suggestion(code = ";", applicability = "maybe-incorrect")] + pub body_span: Span, + #[label(ast_passes::extern_block_label)] + pub extern_span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::foreign_fn_with_qualifier)] +pub struct ForeignFnWithQualifier { + #[primary_span] + pub span: Span, + #[label(ast_passes::extern_block_label)] + pub extern_span: Span, + #[suggestion_verbose(code = "fn ", applicability = "maybe-incorrect")] + pub replace_span: Span, +} diff --git a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl index 672f6f89f7248..d15c0c363b6db 100644 --- a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl @@ -128,3 +128,16 @@ ast_passes_foreign_static_with_body = .body_label = the invalid body .extern_block_label = `extern` blocks define existing foreign statics and statics inside of them cannot have a body .more_extern_note = { -ast_passes_more_extern } + +ast_passes_foreign_fn_with_body = + incorrect function inside `extern` block + .label = cannot have a body + .suggestion = remove the invalid body + .help = you might have meant to write a function accessible through FFI, which can be done by writing `extern fn` outside of the `extern` block + .extern_block_label = `extern` blocks define existing foreign functions and functions inside of them cannot have a body + .more_extern_note = { -ast_passes_more_extern } + +ast_passes_foreign_fn_with_qualifier = + functions in `extern` blocks cannot have qualifiers + .extern_block_label = in this `extern` block + .suggestion = remove the qualifiers From 2aea631ce6fa5ee6687a9c6255e86ea57a8cf5ec Mon Sep 17 00:00:00 2001 From: finalchild Date: Sat, 10 Sep 2022 17:42:12 +0900 Subject: [PATCH 08/23] Migrate ForeignItemNonAscii, ForbiddenCVarArgs, UnnamedAssocConst, NomangleItemNonAscii --- .../rustc_ast_passes/src/ast_validation.rs | 53 +++++-------------- compiler/rustc_ast_passes/src/errors.rs | 41 ++++++++++++++ .../locales/en-US/ast_passes.ftl | 29 ++++++++-- 3 files changed, 78 insertions(+), 45 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 2050e84f269a1..eff08acc7030c 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -473,18 +473,10 @@ impl<'a> AstValidator<'a> { /// An item in `extern { ... }` cannot use non-ascii identifier. fn check_foreign_item_ascii_only(&self, ident: Ident) { if !ident.as_str().is_ascii() { - let n = 83942; - self.err_handler() - .struct_span_err( - ident.span, - "items in `extern` blocks cannot use non-ascii identifiers", - ) - .span_label(self.current_extern_span(), "in this `extern` block") - .note(&format!( - "this limitation may be lifted in the future; see issue #{} for more information", - n, n, - )) - .emit(); + self.session.emit_err(ForeignItemNonAscii { + span: ident.span, + extern_span: self.current_extern_span(), + }); } } @@ -507,24 +499,19 @@ impl<'a> AstValidator<'a> { for Param { ty, span, .. } in &fk.decl().inputs { if let TyKind::CVarArgs = ty.kind { - self.err_handler() - .struct_span_err( - *span, - "only foreign or `unsafe extern \"C\"` functions may be C-variadic", - ) - .emit(); + self.session.emit_err(ForbiddenCVarArgs { span: *span }); } } } - fn check_item_named(&self, ident: Ident, kind: &str) { + fn check_item_named(&self, ident: Ident, create_diag: impl FnOnce(Span) -> D) + where + D: SessionDiagnostic<'a>, + { if ident.name != kw::Underscore { return; } - self.err_handler() - .struct_span_err(ident.span, &format!("`{}` items in this context need a name", kind)) - .span_label(ident.span, format!("`_` is not a valid name for this `{}` item", kind)) - .emit(); + self.session.emit_err(create_diag(ident.span)); } fn check_nomangle_item_asciionly(&self, ident: Ident, item_span: Span) { @@ -532,28 +519,14 @@ impl<'a> AstValidator<'a> { return; } let head_span = self.session.source_map().guess_head_span(item_span); - struct_span_err!( - self.session, - head_span, - E0754, - "`#[no_mangle]` requires ASCII identifier" - ) - .emit(); + self.session.emit_err(NomangleItemNonAscii { span: head_span }); } fn check_mod_file_item_asciionly(&self, ident: Ident) { if ident.name.as_str().is_ascii() { return; } - struct_span_err!( - self.session, - ident.span, - E0754, - "trying to load file for module `{}` with non-ascii identifier name", - ident.name - ) - .help("consider using `#[path]` attribute to specify filesystem path") - .emit(); + self.session.emit_err(ModFileItemNonAscii { span: ident.span, name: ident.name }); } fn deny_generic_params(&self, generics: &Generics, ident_span: Span) { @@ -1537,7 +1510,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } if let AssocItemKind::Const(..) = item.kind { - self.check_item_named(item.ident, "const"); + self.check_item_named(item.ident, |span| UnnamedAssocConst { span }); } match &item.kind { diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index e5a7e42f14218..a00ab3c9bc801 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -318,3 +318,44 @@ pub struct ForeignFnWithQualifier { #[suggestion_verbose(code = "fn ", applicability = "maybe-incorrect")] pub replace_span: Span, } + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::foreign_item_non_ascii)] +#[note] +pub struct ForeignItemNonAscii { + #[primary_span] + pub span: Span, + #[label(ast_passes::extern_block_label)] + pub extern_span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::forbidden_c_var_args)] +pub struct ForbiddenCVarArgs { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::unnamed_assoc_const)] +pub struct UnnamedAssocConst { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::nomangle_item_non_ascii, code = "E0754")] +pub struct NomangleItemNonAscii { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::mod_file_item_non_ascii, code = "E0754")] +#[help] +pub struct ModFileItemNonAscii { + #[primary_span] + pub span: Span, + pub name: Symbol, +} diff --git a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl index d15c0c363b6db..a34fcafa583ab 100644 --- a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl @@ -107,27 +107,27 @@ ast_passes_foreign_ty_with_generic_param = `type`s inside `extern` blocks cannot have generic parameters .suggestion = remove the generic parameters .extern_block_label = `extern` block begins here - .more_extern_note = { -ast_passes_more_extern } + .more_extern_note = {-ast_passes_more_extern} ast_passes_foreign_ty_with_where_clause = `type`s inside `extern` blocks cannot have `where` clauses .suggestion = remove the `where` clause .extern_block_label = `extern` block begins here - .more_extern_note = { -ast_passes_more_extern } + .more_extern_note = {-ast_passes_more_extern} ast_passes_foreign_ty_with_body = incorrect `type` inside `extern` block .label = cannot have a body .body_label = the invalid body .extern_block_label = `extern` blocks define existing foreign types and types inside of them cannot have a body - .more_extern_note = { -ast_passes_more_extern } + .more_extern_note = {-ast_passes_more_extern} ast_passes_foreign_static_with_body = incorrect `static` inside `extern` block .label = cannot have a body .body_label = the invalid body .extern_block_label = `extern` blocks define existing foreign statics and statics inside of them cannot have a body - .more_extern_note = { -ast_passes_more_extern } + .more_extern_note = {-ast_passes_more_extern} ast_passes_foreign_fn_with_body = incorrect function inside `extern` block @@ -135,9 +135,28 @@ ast_passes_foreign_fn_with_body = .suggestion = remove the invalid body .help = you might have meant to write a function accessible through FFI, which can be done by writing `extern fn` outside of the `extern` block .extern_block_label = `extern` blocks define existing foreign functions and functions inside of them cannot have a body - .more_extern_note = { -ast_passes_more_extern } + .more_extern_note = {-ast_passes_more_extern} ast_passes_foreign_fn_with_qualifier = functions in `extern` blocks cannot have qualifiers .extern_block_label = in this `extern` block .suggestion = remove the qualifiers + +ast_passes_foreign_item_non_ascii = + items in `extern` blocks cannot use non-ascii identifiers + .extern_block_label = in this `extern` block + .note = this limitation may be lifted in the future; see issue #83942 for more information + +ast_passes_forbidden_c_var_args = + only foreign or `unsafe extern "C"` functions may be C-variadic + +ast_passes_unnamed_assoc_const = + `const` items in this context need a name + .label = `_` is not a valid name for this `const` item + +ast_passes_nomangle_item_non_ascii = + `#[no_mangle]` requires ASCII identifier + +ast_passes_mod_file_item_non_ascii = + trying to load file for module `{$name}` with non-ascii identifier name + .help = consider using `#[path]` attribute to specify filesystem path From 00d739bd07bbdd29c3f53e163b177e1fed197748 Mon Sep 17 00:00:00 2001 From: finalchild Date: Sat, 10 Sep 2022 18:13:50 +0900 Subject: [PATCH 09/23] Migrate AutoTraitWith(GenericParam/SuperTraitOrWhereClause/AssocItem) --- .../rustc_ast_passes/src/ast_validation.rs | 56 ++++--------------- compiler/rustc_ast_passes/src/errors.rs | 30 ++++++++++ .../locales/en-US/ast_passes.ftl | 15 +++++ 3 files changed, 55 insertions(+), 46 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index eff08acc7030c..c7edd56d9b564 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -531,50 +531,23 @@ impl<'a> AstValidator<'a> { fn deny_generic_params(&self, generics: &Generics, ident_span: Span) { if !generics.params.is_empty() { - struct_span_err!( - self.session, - generics.span, - E0567, - "auto traits cannot have generic parameters" - ) - .span_label(ident_span, "auto trait cannot have generic parameters") - .span_suggestion( - generics.span, - "remove the parameters", - "", - Applicability::MachineApplicable, - ) - .emit(); + self.session.emit_err(AutoTraitWithGenericParam { span: generics.span, ident_span }); } } - fn emit_e0568(&self, span: Span, ident_span: Span) { - struct_span_err!( - self.session, - span, - E0568, - "auto traits cannot have super traits or lifetime bounds" - ) - .span_label(ident_span, "auto trait cannot have super traits or lifetime bounds") - .span_suggestion( - span, - "remove the super traits or lifetime bounds", - "", - Applicability::MachineApplicable, - ) - .emit(); - } - fn deny_super_traits(&self, bounds: &GenericBounds, ident_span: Span) { if let [.., last] = &bounds[..] { let span = ident_span.shrink_to_hi().to(last.span()); - self.emit_e0568(span, ident_span); + self.session.emit_err(AutoTraitWithSuperTraitOrWhereClause { span, ident_span }); } } fn deny_where_clause(&self, where_clause: &WhereClause, ident_span: Span) { if !where_clause.predicates.is_empty() { - self.emit_e0568(where_clause.span, ident_span); + self.session.emit_err(AutoTraitWithSuperTraitOrWhereClause { + span: where_clause.span, + ident_span, + }); } } @@ -582,20 +555,11 @@ impl<'a> AstValidator<'a> { if !trait_items.is_empty() { let spans: Vec<_> = trait_items.iter().map(|i| i.ident.span).collect(); let total_span = trait_items.first().unwrap().span.to(trait_items.last().unwrap().span); - struct_span_err!( - self.session, + self.session.emit_err(AutoTraitWithAssocItem { spans, - E0380, - "auto traits cannot have associated items" - ) - .span_suggestion( - total_span, - "remove these associated items", - "", - Applicability::MachineApplicable, - ) - .span_label(ident_span, "auto trait cannot have associated items") - .emit(); + replace_span: total_span, + ident_span, + }); } } diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index a00ab3c9bc801..dd6c42144c4a7 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -359,3 +359,33 @@ pub struct ModFileItemNonAscii { pub span: Span, pub name: Symbol, } + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::auto_trait_with_generic_param, code = "E0567")] +pub struct AutoTraitWithGenericParam { + #[primary_span] + #[suggestion(code = "", applicability = "machine-applicable")] + pub span: Span, + #[label(ast_passes::ident_label)] + pub ident_span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::auto_trait_with_super_trait_or_where_clause, code = "E0568")] +pub struct AutoTraitWithSuperTraitOrWhereClause { + #[primary_span] + pub span: Span, + #[label(ast_passes::ident_label)] + pub ident_span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::auto_trait_with_assoc_item)] +pub struct AutoTraitWithAssocItem { + #[primary_span] + pub spans: Vec, + #[suggestion(code = "", applicability = "machine-applicable")] + pub replace_span: Span, + #[label(ast_passes::ident_label)] + pub ident_span: Span, +} diff --git a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl index a34fcafa583ab..63eeecd6e7330 100644 --- a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl @@ -160,3 +160,18 @@ ast_passes_nomangle_item_non_ascii = ast_passes_mod_file_item_non_ascii = trying to load file for module `{$name}` with non-ascii identifier name .help = consider using `#[path]` attribute to specify filesystem path + +ast_passes_auto_trait_with_generic_param = + auto traits cannot have generic parameters + .ident_label = auto trait cannot have generic parameters + .suggestion = remove the parameters + +ast_passes_auto_trait_with_super_trait_or_where_clause = + auto traits cannot have super traits or lifetime bounds + .ident_label = auto trait cannot have super traits or lifetime bounds + .suggestion = remove the super traits or lifetime bounds + +ast_passes_auto_trait_with_assoc_item = + auto traits cannot have associated items + .suggestion = remove these associated items + .ident_label = auto trait cannot have associated items From c8de3350bb2bef24a90dfa461607116aea5abad2 Mon Sep 17 00:00:00 2001 From: finalchild Date: Sat, 10 Sep 2022 19:14:13 +0900 Subject: [PATCH 10/23] Migrate GenericArgAfterConstraint and fix prev --- .../rustc_ast_passes/src/ast_validation.rs | 39 +++++++------------ compiler/rustc_ast_passes/src/errors.rs | 21 +++++++++- .../locales/en-US/ast_passes.ftl | 20 ++++++++++ 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index c7edd56d9b564..faa6711764cac 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -13,7 +13,7 @@ use rustc_ast::walk_list; use rustc_ast::*; use rustc_ast_pretty::pprust::{self, State}; use rustc_data_structures::fx::FxHashMap; -use rustc_errors::{error_code, fluent, pluralize, struct_span_err, Applicability}; +use rustc_errors::{error_code, fluent, struct_span_err, Applicability}; use rustc_macros::Subdiagnostic; use rustc_parse::validate_attr; use rustc_session::lint::builtin::{ @@ -602,32 +602,21 @@ impl<'a> AstValidator<'a> { AngleBracketedArg::Constraint(c) => Either::Left(c.span), AngleBracketedArg::Arg(a) => Either::Right(a.span()), }); + let last_arg_span = *arg_spans.last().unwrap(); + let first_constraint_span = constraint_spans[0]; let args_len = arg_spans.len(); - let constraint_len = constraint_spans.len(); + let constraints_len = constraint_spans.len(); // ...and then error: - self.err_handler() - .struct_span_err( - arg_spans.clone(), - "generic arguments must come before the first constraint", - ) - .span_label(constraint_spans[0], &format!("constraint{}", pluralize!(constraint_len))) - .span_label( - *arg_spans.iter().last().unwrap(), - &format!("generic argument{}", pluralize!(args_len)), - ) - .span_labels(constraint_spans, "") - .span_labels(arg_spans, "") - .span_suggestion_verbose( - data.span, - &format!( - "move the constraint{} after the generic argument{}", - pluralize!(constraint_len), - pluralize!(args_len) - ), - self.correct_generic_order_suggestion(&data), - Applicability::MachineApplicable, - ) - .emit(); + self.session.emit_err(GenericArgAfterConstraint { + arg_spans: arg_spans.clone(), + constraint_spans, + last_arg_span, + first_constraint_span, + replace_span: data.span, + args_len, + constraints_len, + correct_order: self.correct_generic_order_suggestion(&data), + }); } fn visit_ty_common(&mut self, ty: &'a Ty) { diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index dd6c42144c4a7..aabb62fd9d630 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -374,13 +374,14 @@ pub struct AutoTraitWithGenericParam { #[diag(ast_passes::auto_trait_with_super_trait_or_where_clause, code = "E0568")] pub struct AutoTraitWithSuperTraitOrWhereClause { #[primary_span] + #[suggestion(code = "", applicability = "machine-applicable")] pub span: Span, #[label(ast_passes::ident_label)] pub ident_span: Span, } #[derive(SessionDiagnostic)] -#[diag(ast_passes::auto_trait_with_assoc_item)] +#[diag(ast_passes::auto_trait_with_assoc_item, code = "E0380")] pub struct AutoTraitWithAssocItem { #[primary_span] pub spans: Vec, @@ -389,3 +390,21 @@ pub struct AutoTraitWithAssocItem { #[label(ast_passes::ident_label)] pub ident_span: Span, } + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::generic_arg_after_constraint)] +pub struct GenericArgAfterConstraint { + #[primary_span] + pub arg_spans: Vec, + #[label(ast_passes::constraints_label)] + pub constraint_spans: Vec, + #[label(ast_passes::last_arg_label)] + pub last_arg_span: Span, + #[label(ast_passes::first_constraint_label)] + pub first_constraint_span: Span, + #[suggestion_verbose(code = "{correct_order}", applicability = "machine-applicable")] + pub replace_span: Span, + pub args_len: usize, + pub constraints_len: usize, + pub correct_order: String, +} diff --git a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl index 63eeecd6e7330..4e9c955339995 100644 --- a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl @@ -175,3 +175,23 @@ ast_passes_auto_trait_with_assoc_item = auto traits cannot have associated items .suggestion = remove these associated items .ident_label = auto trait cannot have associated items + +ast_passes_generic_arg_after_constraint = + generic arguments must come before the first constraint + .first_constraint_label = { $constraints_len -> + [one] constraint + *[other] constraints + } + .last_arg_label = { $args_len -> + [one] generic argument + *[other] generic arguments + } + .constraints_label = {""} + .args_label = {""} + .suggestion = move the { $constraints_len -> + [one] constraint + *[other] constraints + } after the { $args_len -> + [one] generic argument + *[other] generic arguments + } From 18cd17df79c7088a1a40e4aa2e8e5a1415a2647c Mon Sep 17 00:00:00 2001 From: finalchild Date: Sat, 10 Sep 2022 20:13:55 +0900 Subject: [PATCH 11/23] Migrate FnPtrTyWithPat, MultipleExplicitLifetimeBound, ImplTraitTyInPathParam, ImplTraitTyNested, ImplTraitTyWithoutTraitBound, deprecated_extern_missing_abi --- .../rustc_ast_passes/src/ast_validation.rs | 42 ++++--------------- compiler/rustc_ast_passes/src/errors.rs | 38 +++++++++++++++++ .../locales/en-US/ast_passes.ftl | 20 +++++++++ 3 files changed, 67 insertions(+), 33 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index faa6711764cac..42a1e97c63ffd 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -624,13 +624,7 @@ impl<'a> AstValidator<'a> { TyKind::BareFn(bfty) => { self.check_fn_decl(&bfty.decl, SelfSemantic::No); Self::check_decl_no_pat(&bfty.decl, |span, _, _| { - struct_span_err!( - self.session, - span, - E0561, - "patterns aren't allowed in function pointer types" - ) - .emit(); + self.session.emit_err(FnPtrTyWithPat { span }); }); self.check_late_bound_lifetime_defs(&bfty.generic_params); if let Extern::Implicit(_) = bfty.ext { @@ -643,13 +637,9 @@ impl<'a> AstValidator<'a> { for bound in bounds { if let GenericBound::Outlives(lifetime) = bound { if any_lifetime_bounds { - struct_span_err!( - self.session, - lifetime.ident.span, - E0226, - "only a single explicit lifetime bound is permitted" - ) - .emit(); + self.session.emit_err(MultipleExplicitLifetimeBound { + span: lifetime.ident.span, + }); break; } any_lifetime_bounds = true; @@ -658,29 +648,15 @@ impl<'a> AstValidator<'a> { } TyKind::ImplTrait(_, bounds) => { if self.is_impl_trait_banned { - struct_span_err!( - self.session, - ty.span, - E0667, - "`impl Trait` is not allowed in path parameters" - ) - .emit(); + self.session.emit_err(ImplTraitTyInPathParam { span: ty.span }); } - if let Some(outer_impl_trait_sp) = self.outer_impl_trait { - struct_span_err!( - self.session, - ty.span, - E0666, - "nested `impl Trait` is not allowed" - ) - .span_label(outer_impl_trait_sp, "outer `impl Trait`") - .span_label(ty.span, "nested `impl Trait` here") - .emit(); + if let Some(outer_span) = self.outer_impl_trait { + self.session.emit_err(ImplTraitTyNested { nested_span: ty.span, outer_span }); } if !bounds.iter().any(|b| matches!(b, GenericBound::Trait(..))) { - self.err_handler().span_err(ty.span, "at least one trait must be specified"); + self.session.emit_err(ImplTraitTyWithoutTraitBound { span: ty.span }); } } _ => {} @@ -701,7 +677,7 @@ impl<'a> AstValidator<'a> { MISSING_ABI, id, span, - "extern declarations without an explicit ABI are deprecated", + fluent::ast_passes::deprecated_extern_missing_abi, BuiltinLintDiagnostics::MissingAbi(span, abi::Abi::FALLBACK), ) } diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index aabb62fd9d630..48ef5052c39c6 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -408,3 +408,41 @@ pub struct GenericArgAfterConstraint { pub constraints_len: usize, pub correct_order: String, } + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::fn_ptr_ty_with_pat, code = "E0561")] +pub struct FnPtrTyWithPat { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::multiple_explicit_lifetime_bound, code = "E0226")] +pub struct MultipleExplicitLifetimeBound { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::impl_trait_ty_in_path_param, code = "E0667")] +pub struct ImplTraitTyInPathParam { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::impl_trait_ty_nested, code = "E0666")] +pub struct ImplTraitTyNested { + #[primary_span] + #[label(ast_passes::nested_label)] + pub nested_span: Span, + #[label(ast_passes::outer_label)] + pub outer_span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::impl_trait_ty_without_trait_bound)] +pub struct ImplTraitTyWithoutTraitBound { + #[primary_span] + pub span: Span, +} diff --git a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl index 4e9c955339995..e96c411e06a7e 100644 --- a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl @@ -195,3 +195,23 @@ ast_passes_generic_arg_after_constraint = [one] generic argument *[other] generic arguments } + +ast_passes_fn_ptr_ty_with_pat = + patterns aren't allowed in function pointer types + +ast_passes_multiple_explicit_lifetime_bound = + only a single explicit lifetime bound is permitted + +ast_passes_impl_trait_ty_in_path_param = + `impl Trait` is not allowed in path parameters + +ast_passes_impl_trait_ty_nested = + nested `impl Trait` is not allowed + .outer_label = outer `impl Trait` + .nested_label = nested `impl Trait` here + +ast_passes_impl_trait_ty_without_trait_bound = + at least one trait must be specified + +ast_passes_deprecated_extern_missing_abi = + extern declarations without an explicit ABI are deprecated From baab6c175cf5cee2f0e16d6450f6f5b3acdd561c Mon Sep 17 00:00:00 2001 From: finalchild Date: Sun, 11 Sep 2022 00:25:20 +0900 Subject: [PATCH 12/23] Migrate GenericParamWrongOrder --- .../rustc_ast_passes/src/ast_validation.rs | 31 ++++------- compiler/rustc_ast_passes/src/errors.rs | 54 ++++++++++++------- .../locales/en-US/ast_passes.ftl | 12 +++++ compiler/rustc_errors/src/diagnostic.rs | 1 + compiler/rustc_errors/src/diagnostic_impls.rs | 11 ++++ 5 files changed, 68 insertions(+), 41 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 42a1e97c63ffd..6abb4c56a1149 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -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 = None; let mut out_of_order = FxHashMap::default(); let mut param_idents = Vec::with_capacity(generics.len()); @@ -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(), + }); } } } @@ -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 { diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index 48ef5052c39c6..b19748209f3b6 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -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}; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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)] @@ -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] @@ -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 { @@ -329,14 +331,14 @@ 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] @@ -344,14 +346,14 @@ pub struct UnnamedAssocConst { 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 { @@ -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] @@ -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] @@ -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] @@ -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] @@ -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] @@ -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, + pub param_kind: ParamKindOrd, + pub max_param_kind: ParamKindOrd, + #[suggestion(code = "{correct_order}", applicability = "machine-applicable")] + pub replace_span: Span, + pub correct_order: String, +} diff --git a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl index e96c411e06a7e..56309113d73a3 100644 --- a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl @@ -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 diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 51b2ff6a00381..79ed8070643c8 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -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; diff --git a/compiler/rustc_errors/src/diagnostic_impls.rs b/compiler/rustc_errors/src/diagnostic_impls.rs index dad5e98aac021..961b8eb8f652c 100644 --- a/compiler/rustc_errors/src/diagnostic_impls.rs +++ b/compiler/rustc_errors/src/diagnostic_impls.rs @@ -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))) From 560d4456ad7b814453c64ed3e5b5d6771e724a72 Mon Sep 17 00:00:00 2001 From: finalchild Date: Sun, 11 Sep 2022 16:34:59 +0900 Subject: [PATCH 13/23] Fix rebase Co-authored-by: RanolP --- compiler/rustc_error_messages/locales/en-US/ast_passes.ftl | 6 ++---- compiler/rustc_errors/src/diagnostic_impls.rs | 4 +--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl index 56309113d73a3..fd136cc4da616 100644 --- a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl @@ -219,11 +219,9 @@ ast_passes_deprecated_extern_missing_abi = ast_passes_generic_param_wrong_order = { $param_kind -> [lifetime] lifetime - [type] type - *[const] const + *[type_or_const] type and const } parameters must be declared prior to { $max_param_kind -> [lifetime] lifetime - [type] type - *[const] const + *[type_or_const] type and const } parameters .suggestion = reorder the parameters: lifetimes, then consts and types diff --git a/compiler/rustc_errors/src/diagnostic_impls.rs b/compiler/rustc_errors/src/diagnostic_impls.rs index 961b8eb8f652c..d902d261122b6 100644 --- a/compiler/rustc_errors/src/diagnostic_impls.rs +++ b/compiler/rustc_errors/src/diagnostic_impls.rs @@ -151,9 +151,7 @@ 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", + ast::ParamKindOrd::TypeOrConst => "type_or_const", })) } } From 834db8bb44015bec7eb67df2adabf23109d3c4e7 Mon Sep 17 00:00:00 2001 From: finalchild Date: Mon, 12 Sep 2022 02:38:02 +0900 Subject: [PATCH 14/23] Migrate the rest of ast_validation.rs Co-authored-by: RanolP --- .../rustc_ast_passes/src/ast_validation.rs | 345 +++++++----------- compiler/rustc_ast_passes/src/errors.rs | 212 +++++++++++ .../locales/en-US/ast_passes.ftl | 86 +++++ 3 files changed, 435 insertions(+), 208 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 6abb4c56a1149..3b6cf193109d8 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -13,7 +13,7 @@ use rustc_ast::walk_list; use rustc_ast::*; use rustc_ast_pretty::pprust::{self, State}; use rustc_data_structures::fx::FxHashMap; -use rustc_errors::{error_code, fluent, struct_span_err, Applicability}; +use rustc_errors::fluent; use rustc_macros::Subdiagnostic; use rustc_parse::validate_attr; use rustc_session::lint::builtin::{ @@ -244,10 +244,6 @@ impl<'a> AstValidator<'a> { } } - fn err_handler(&self) -> &rustc_errors::Handler { - &self.session.diagnostic() - } - fn check_lifetime(&self, ident: Ident) { let valid_names = [kw::UnderscoreLifetime, kw::StaticLifetime, kw::Empty]; if !valid_names.contains(&ident.name) && ident.without_first_quote().is_reserved() { @@ -866,25 +862,15 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.with_in_trait_impl(true, Some(*constness), |this| { this.invalid_visibility(&item.vis, None); if let TyKind::Err = self_ty.kind { - this.err_handler() - .struct_span_err( - item.span, - "`impl Trait for .. {}` is an obsolete syntax", - ) - .help("use `auto trait Trait {}` instead") - .emit(); + this.session.emit_err(ObsoleteAutoTraitSyntax { span: item.span }); } if let (&Unsafe::Yes(span), &ImplPolarity::Negative(sp)) = (unsafety, polarity) { - struct_span_err!( - this.session, - sp.to(t.path.span), - E0198, - "negative impls cannot be unsafe" - ) - .span_label(sp, "negative because of this") - .span_label(span, "unsafe because of this") - .emit(); + this.session.emit_err(UnsafeNegativeImpl { + span: sp.to(t.path.span), + negative_span: sp, + unsafe_span: span, + }); } this.visit_vis(&item.vis); @@ -912,35 +898,27 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self_ty, items: _, }) => { - let error = |annotation_span, annotation| { - let mut err = self.err_handler().struct_span_err( - self_ty.span, - &format!("inherent impls cannot be {}", annotation), - ); - err.span_label(annotation_span, &format!("{} because of this", annotation)); - err.span_label(self_ty.span, "inherent impl for this type"); - err - }; - self.invalid_visibility( &item.vis, Some(InvalidVisibilityNote::IndividualImplItems), ); if let &Unsafe::Yes(span) = unsafety { - error(span, "unsafe").code(error_code!(E0197)).emit(); + self.session + .emit_err(UnsafeInherentImpl { span: self_ty.span, unsafe_span: span }); } if let &ImplPolarity::Negative(span) = polarity { - error(span, "negative").emit(); + self.session + .emit_err(NegativeInherentImpl { span: self_ty.span, negative_span: span }); } if let &Defaultness::Default(def_span) = defaultness { - error(def_span, "`default`") - .note("only trait implementations may be annotated with `default`") - .emit(); + self.session.emit_err(DefaultInherentImpl { + span: self_ty.span, + default_span: def_span, + }); } if let &Const::Yes(span) = constness { - error(span, "`const`") - .note("only trait implementations may be annotated with `const`") - .emit(); + self.session + .emit_err(ConstInherentImpl { span: self_ty.span, const_span: span }); } } ItemKind::Fn(box Fn { defaultness, sig, generics, body }) => { @@ -982,7 +960,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { Some(InvalidVisibilityNote::IndividualForeignItems), ); if let &Unsafe::Yes(span) = unsafety { - self.err_handler().span_err(span, "extern block cannot be declared unsafe"); + self.session.emit_err(UnsafeExternBlock { span }); } if abi.is_none() { self.maybe_lint_missing_abi(item.span, item.id); @@ -1022,7 +1000,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } ItemKind::Mod(unsafety, mod_kind) => { if let &Unsafe::Yes(span) = unsafety { - self.err_handler().span_err(span, "module cannot be declared unsafe"); + self.session.emit_err(UnsafeModule { span }); } // Ensure that `path` attributes on modules are recorded as used (cf. issue #35584). if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _)) @@ -1033,7 +1011,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } ItemKind::Union(vdata, ..) => { if vdata.fields().is_empty() { - self.err_handler().span_err(item.span, "unions cannot have zero fields"); + self.session.emit_err(EmptyUnion { span: item.span }); } } ItemKind::Const(def, .., None) => { @@ -1059,14 +1037,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } self.check_type_no_bounds(bounds, |span| TyAliasWithBound { span }); if where_clauses.1.0 { - let mut err = self.err_handler().struct_span_err( - where_clauses.1.1, - "where clauses are not allowed after the type for type aliases", - ); - err.note( - "see issue #89122 for more information", - ); - err.emit(); + self.session.emit_err(TyAliasWithWhereClause { span: where_clauses.1.1 }); } } _ => {} @@ -1164,11 +1135,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => { if let Some(span) = prev_param_default { - let mut err = self.err_handler().struct_span_err( - span, - "generic parameters with a default must be trailing", - ); - err.emit(); + self.session.emit_err(GenericParamWithDefaultNotTrailing { span }); break; } } @@ -1199,13 +1166,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> { match bound { GenericBound::Trait(t, _) => { if !t.bound_generic_params.is_empty() { - struct_span_err!( - self.err_handler(), - t.span, - E0316, - "nested quantification of lifetimes" - ) - .emit(); + self.session.emit_err(LifetimeNestedQuantification { + span: t.span, + }); } } GenericBound::Outlives(_) => {} @@ -1230,32 +1193,19 @@ impl<'a> Visitor<'a> for AstValidator<'a> { if let GenericBound::Trait(poly, modify) = bound { match (ctxt, modify) { (BoundKind::SuperTraits, TraitBoundModifier::Maybe) => { - let mut err = self - .err_handler() - .struct_span_err(poly.span, "`?Trait` is not permitted in supertraits"); - let path_str = pprust::path_to_string(&poly.trait_ref.path); - err.note(&format!("traits are `?{}` by default", path_str)); - err.emit(); + self.session.emit_err(SuperTraitWithMaybe { + span: poly.span, + path_str: pprust::path_to_string(&poly.trait_ref.path), + }); } (BoundKind::TraitObject, TraitBoundModifier::Maybe) => { - let mut err = self.err_handler().struct_span_err( - poly.span, - "`?Trait` is not permitted in trait object types", - ); - err.emit(); + self.session.emit_err(TraitObjectWithMaybe { span: poly.span }); } (_, TraitBoundModifier::MaybeConst) if let Some(reason) = &self.disallow_tilde_const => { - let mut err = self.err_handler().struct_span_err(bound.span(), "`~const` is not allowed here"); - match reason { - DisallowTildeConstContext::TraitObject => err.note("trait objects cannot have `~const` trait bounds"), - DisallowTildeConstContext::Fn(FnKind::Closure(..)) => err.note("closures cannot have `~const` trait bounds"), - DisallowTildeConstContext::Fn(FnKind::Fn(_, ident, ..)) => err.span_note(ident.span, "this function is not `const`, so it cannot have `~const` trait bounds"), - }; - err.emit(); + self.session.emit_err(ForbiddenMaybeConst { span: bound.span(), reason }); } (_, TraitBoundModifier::MaybeConstMaybe) => { - self.err_handler() - .span_err(bound.span(), "`~const` and `?` are mutually exclusive"); + self.session.emit_err(MaybeConstWithMaybeTrait { span: bound.span() }); } _ => {} } @@ -1294,15 +1244,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> { .. }) = fk.header() { - self.err_handler() - .struct_span_err( - vec![*cspan, *aspan], - "functions cannot be both `const` and `async`", - ) - .span_label(*cspan, "`const` because of this") - .span_label(*aspan, "`async` because of this") - .span_label(span, "") // Point at the fn header. - .emit(); + self.session.emit_err(ConstAsyncFn { + spans: vec![*cspan, *aspan], + const_span: *cspan, + async_span: *aspan, + fn_span: span, + }); } if let FnKind::Closure(ClosureBinder::For { generic_params, .. }, ..) = fk { @@ -1324,20 +1271,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> { // Functions without bodies cannot have patterns. if let FnKind::Fn(ctxt, _, sig, _, _, None) = fk { Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| { - let (code, msg, label) = match ctxt { - FnCtxt::Foreign => ( - error_code!(E0130), - "patterns aren't allowed in foreign function declarations", - "pattern not allowed in foreign function", - ), - _ => ( - error_code!(E0642), - "patterns aren't allowed in functions without bodies", - "pattern not allowed in function without body", - ), - }; if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) { if let Some(ident) = ident { + let msg = match ctxt { + FnCtxt::Foreign => fluent::ast_passes::patterns_in_foreign_fns, + _ => fluent::ast_passes::patterns_in_fns_without_body, + }; let diag = BuiltinLintDiagnostics::PatternsInFnsWithoutBody(span, ident); self.lint_buffer.buffer_lint_with_diagnostic( PATTERNS_IN_FNS_WITHOUT_BODY, @@ -1348,11 +1287,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> { ) } } else { - self.err_handler() - .struct_span_err(span, msg) - .span_label(span, label) - .code(code) - .emit(); + match ctxt { + FnCtxt::Foreign => self.session.emit_err(PatternsInForeignFns { span }), + _ => self.session.emit_err(PatternsInFnsWithoutBody { span }), + }; } }); } @@ -1474,129 +1412,120 @@ fn deny_equality_constraints( predicate: &WhereEqPredicate, generics: &Generics, ) { - let mut err = this.err_handler().struct_span_err( - predicate.span, - "equality constraints are not yet supported in `where` clauses", - ); - err.span_label(predicate.span, "not supported"); + let mut err = + EqualityConstraint { span: predicate.span, assoc_constraint_suggestion: Vec::new() }; // Given `::Bar = RhsTy`, suggest `A: Foo`. if let TyKind::Path(Some(qself), full_path) = &predicate.lhs_ty.kind { - if let TyKind::Path(None, path) = &qself.ty.kind { - match &path.segments[..] { - [PathSegment { ident, args: None, .. }] => { - for param in &generics.params { - if param.ident == *ident { - let param = ident; - match &full_path.segments[qself.position..] { - [PathSegment { ident, args, .. }] => { - // Make a new `Path` from `foo::Bar` to `Foo`. - let mut assoc_path = full_path.clone(); - // Remove `Bar` from `Foo::Bar`. - assoc_path.segments.pop(); - let len = assoc_path.segments.len() - 1; - let gen_args = args.as_deref().cloned(); - // Build ``. - let arg = AngleBracketedArg::Constraint(AssocConstraint { - id: rustc_ast::node_id::DUMMY_NODE_ID, - ident: *ident, - gen_args, - kind: AssocConstraintKind::Equality { - term: predicate.rhs_ty.clone().into(), - }, - span: ident.span, - }); - // Add `` to `Foo`. - match &mut assoc_path.segments[len].args { - Some(args) => match args.deref_mut() { - GenericArgs::Parenthesized(_) => continue, - GenericArgs::AngleBracketed(args) => { - args.args.push(arg); - } - }, - empty_args => { - *empty_args = AngleBracketedArgs { - span: ident.span, - args: vec![arg], - } - .into(); - } + if let TyKind::Path(None, qself_ty_path) = &qself.ty.kind { + if let [PathSegment { ident: qself_ty_ident, args: None, .. }] = + &qself_ty_path.segments[..] + { + if generics.params.iter().any(|param| param.ident == *qself_ty_ident) { + if let [PathSegment { ident: assoc_ty_ident, args: assoc_ty_args, .. }] = + &full_path.segments[qself.position..] + { + // Build ``. + let new_arg = AngleBracketedArg::Constraint(AssocConstraint { + id: rustc_ast::node_id::DUMMY_NODE_ID, + ident: *assoc_ty_ident, + gen_args: assoc_ty_args.as_deref().cloned(), + kind: AssocConstraintKind::Equality { + term: predicate.rhs_ty.clone().into(), + }, + span: assoc_ty_ident.span, + }); + + let bound_opt = { + // Make a new `Path` from `foo::Bar` to `Foo`. + let mut new_bound = full_path.clone(); + // Remove `Bar` from `Foo::Bar`. + new_bound.segments.pop(); + // Add `` to `Foo` if applicable and return. + match &mut new_bound.segments.last_mut().unwrap().args { + Some(args) => match args.deref_mut() { + GenericArgs::Parenthesized(_) => None, + GenericArgs::AngleBracketed(args) => { + args.args.push(new_arg); + Some(new_bound) } - err.span_suggestion_verbose( + }, + empty_args => { + *empty_args = AngleBracketedArgs { + span: assoc_ty_ident.span, + args: vec![new_arg], + } + .into(); + Some(new_bound) + } + } + }; + + if let Some(bound) = bound_opt { + err.assoc_constraint_suggestion.push( + EqualityConstraintToAssocConstraintSuggestion { + assoc_ty: assoc_ty_ident.name.to_string(), + suggestion: vec![( predicate.span, - &format!( - "if `{}` is an associated type you're trying to set, \ - use the associated type binding syntax", - ident - ), format!( "{}: {}", - param, - pprust::path_to_string(&assoc_path) + qself_ty_ident.name, + pprust::path_to_string(&bound) ), - Applicability::MaybeIncorrect, - ); - } - _ => {} - }; + )], + }, + ) } } } - _ => {} } } } // Given `A: Foo, A::Bar = RhsTy`, suggest `A: Foo`. if let TyKind::Path(None, full_path) = &predicate.lhs_ty.kind { if let [potential_param, potential_assoc] = &full_path.segments[..] { - for param in &generics.params { - if param.ident == potential_param.ident { - for bound in ¶m.bounds { - if let ast::GenericBound::Trait(trait_ref, TraitBoundModifier::None) = bound - { - if let [trait_segment] = &trait_ref.trait_ref.path.segments[..] { - let assoc = pprust::path_to_string(&ast::Path::from_ident( - potential_assoc.ident, - )); - let ty = pprust::ty_to_string(&predicate.rhs_ty); - let (args, span) = match &trait_segment.args { - Some(args) => match args.deref() { - ast::GenericArgs::AngleBracketed(args) => { - let Some(arg) = args.args.last() else { - continue; - }; - ( - format!(", {} = {}", assoc, ty), - arg.span().shrink_to_hi(), - ) - } - _ => continue, - }, - None => ( - format!("<{} = {}>", assoc, ty), - trait_segment.span().shrink_to_hi(), - ), - }; - err.multipart_suggestion( - &format!( - "if `{}::{}` is an associated type you're trying to set, \ - use the associated type binding syntax", - trait_segment.ident, potential_assoc.ident, - ), - vec![(span, args), (predicate.span, String::new())], - Applicability::MaybeIncorrect, - ); - } + if let Some(param) = + generics.params.iter().find(|param| param.ident == potential_param.ident) + { + for bound in ¶m.bounds { + if let ast::GenericBound::Trait(trait_ref, TraitBoundModifier::None) = bound { + if let [trait_segment] = &trait_ref.trait_ref.path.segments[..] { + let assoc = potential_assoc.ident; + let rhs = pprust::ty_to_string(&predicate.rhs_ty); + let (add_loc, add_str) = match &trait_segment.args { + Some(args) => match args.deref() { + ast::GenericArgs::AngleBracketed(args) => { + let Some(arg) = args.args.last() else { + continue; + }; + ( + arg.span().shrink_to_hi(), + format!(", {} = {}", assoc, rhs), + ) + } + _ => continue, + }, + None => ( + trait_segment.span().shrink_to_hi(), + format!("<{} = {}>", assoc, rhs), + ), + }; + err.assoc_constraint_suggestion.push( + EqualityConstraintToAssocConstraintSuggestion { + assoc_ty: format!("{}::{}", trait_segment.ident, assoc), + suggestion: vec![ + (add_loc, add_str), + (predicate.span, String::new()), + ], + }, + ); } } } } } } - err.note( - "see issue #20041 for more information", - ); - err.emit(); + this.session.emit_err(err); } pub fn check_crate(session: &Session, krate: &Crate, lints: &mut LintBuffer) -> bool { diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index b19748209f3b6..8af8387bcaf75 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -460,3 +460,215 @@ pub struct GenericParamWrongOrder { pub replace_span: Span, pub correct_order: String, } + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::obsolete_auto_trait_syntax)] +#[help] +pub struct ObsoleteAutoTraitSyntax { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::unsafe_negative_impl, code = "E0198")] +pub struct UnsafeNegativeImpl { + #[primary_span] + pub span: Span, + #[label(ast_passes::negative_label)] + pub negative_span: Span, + #[label(ast_passes::unsafe_label)] + pub unsafe_span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::unsafe_inherent_impl, code = "E0197")] +pub struct UnsafeInherentImpl { + #[primary_span] + #[label(ast_passes::ty_label)] + pub span: Span, + #[label(ast_passes::unsafe_label)] + pub unsafe_span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::negative_inherent_impl)] +pub struct NegativeInherentImpl { + #[primary_span] + #[label(ast_passes::ty_label)] + pub span: Span, + #[label(ast_passes::negative_label)] + pub negative_span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::default_inherent_impl)] +#[note] +pub struct DefaultInherentImpl { + #[primary_span] + #[label(ast_passes::ty_label)] + pub span: Span, + #[label(ast_passes::default_label)] + pub default_span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::const_inherent_impl)] +#[note] +pub struct ConstInherentImpl { + #[primary_span] + #[label(ast_passes::ty_label)] + pub span: Span, + #[label(ast_passes::const_label)] + pub const_span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::unsafe_extern_block)] +pub struct UnsafeExternBlock { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::unsafe_module)] +pub struct UnsafeModule { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::empty_union)] +pub struct EmptyUnion { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::ty_alias_with_where_clause)] +#[note] +pub struct TyAliasWithWhereClause { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::generic_param_with_default_not_trailing)] +pub struct GenericParamWithDefaultNotTrailing { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::lifetime_nested_quantification, code = "E0316")] +pub struct LifetimeNestedQuantification { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::super_trait_with_maybe)] +#[note] +pub struct SuperTraitWithMaybe { + #[primary_span] + pub span: Span, + pub path_str: String, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::trait_object_with_maybe)] +pub struct TraitObjectWithMaybe { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::forbidden_maybe_const)] +#[note] +pub struct ForbiddenMaybeConst { + #[primary_span] + pub span: Span, + #[subdiagnostic] + pub reason: DisallowTildeConstContext, +} + +impl AddToDiagnostic for DisallowTildeConstContext { + fn add_to_diagnostic_with(self, diag: &mut Diagnostic, _: F) + where + F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage, + { + match self { + Self::TraitObject => { + diag.note(fluent::ast_passes::trait_object); + } + Self::Fn(FnKind::Closure(..)) => { + diag.note(fluent::ast_passes::closure); + } + Self::Fn(FnKind::Fn(_, ident, ..)) => { + diag.span_note(ident.span, fluent::ast_passes::fn_not_const); + } + } + } +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::maybe_const_with_maybe_trait)] +pub struct MaybeConstWithMaybeTrait { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::const_async_fn)] +pub struct ConstAsyncFn { + #[primary_span] + pub spans: Vec, + #[label(ast_passes::const_label)] + pub const_span: Span, + #[label(ast_passes::async_label)] + pub async_span: Span, + #[label(ast_passes::fn_label)] + pub fn_span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::patterns_in_foreign_fns, code = "E0130")] +pub struct PatternsInForeignFns { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::patterns_in_fns_without_body, code = "E0642")] +pub struct PatternsInFnsWithoutBody { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(ast_passes::equality_constraint)] +#[note] +pub struct EqualityConstraint { + #[primary_span] + #[label] + pub span: Span, + #[subdiagnostic] + pub assoc_constraint_suggestion: Vec, +} + +pub struct EqualityConstraintToAssocConstraintSuggestion { + pub assoc_ty: String, + pub suggestion: Vec<(Span, String)>, +} + +impl AddSubdiagnostic for EqualityConstraintToAssocConstraintSuggestion { + fn add_to_diagnostic(self, diag: &mut Diagnostic) { + diag.set_arg("assoc_ty", self.assoc_ty); + diag.multipart_suggestion( + fluent::ast_passes::assoc_constraint_suggestion, + self.suggestion, + Applicability::MaybeIncorrect, + ); + } +} diff --git a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl index fd136cc4da616..a5d83b0c33d26 100644 --- a/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/ast_passes.ftl @@ -225,3 +225,89 @@ ast_passes_generic_param_wrong_order = *[type_or_const] type and const } parameters .suggestion = reorder the parameters: lifetimes, then consts and types + +ast_passes_obsolete_auto_trait_syntax = + `impl Trait for .. {"{}"}` is an obsolete syntax + .help = use `auto trait Trait {"{}"}` instead + +ast_passes_unsafe_negative_impl = + negative impls cannot be unsafe + .negative_label = negative because of this + .unsafe_label = unsafe because of this + +ast_passes_unsafe_inherent_impl = + inherent impls cannot be unsafe + .unsafe_label = unsafe because of this + .ty_label = inherent impl for this type + +ast_passes_negative_inherent_impl = + inherent impls cannot be negative + .negative_label = negative because of this + .ty_label = inherent impl for this type + +ast_passes_default_inherent_impl = + inherent impls cannot be `default` + .default_label = `default` because of this + .ty_label = inherent impl for this type + .note = only trait implementations may be annotated with `default` + +ast_passes_const_inherent_impl = + inherent impls cannot be `const` + .const_label = `const` because of this + .ty_label = inherent impl for this type + .note = only trait implementations may be annotated with `const` + +ast_passes_unsafe_extern_block = + extern block cannot be declared unsafe + +ast_passes_unsafe_module = + module cannot be declared unsafe + +ast_passes_empty_union = + unions cannot have zero fields + +ast_passes_ty_alias_with_where_clause = + where clauses are not allowed after the type for type aliases + .note = see issue #89122 for more information + +ast_passes_generic_param_with_default_not_trailing = + generic parameters with a default must be trailing + +ast_passes_lifetime_nested_quantification = + nested quantification of lifetimes + +ast_passes_super_trait_with_maybe = + `?Trait` is not permitted in supertraits + .note = traits are `?{$path_str}` by default + +ast_passes_trait_object_with_maybe = + `?Trait` is not permitted in trait object types + +ast_passes_forbidden_maybe_const = + `~const` is not allowed here + .trait_object = trait objects cannot have `~const` trait bounds + .closure = closures cannot have `~const` trait bounds + .fn_not_const = this function is not `const`, so it cannot have `~const` trait bounds + +ast_passes_maybe_const_with_maybe_trait = + `~const` and `?` are mutually exclusive + +ast_passes_const_async_fn = + functions cannot be both `const` and `async` + .const_label = `const` because of this + .async_label = `async` because of this + .fn_label = {""} + +ast_passes_patterns_in_foreign_fns = + patterns aren't allowed in foreign function declarations + .label = pattern not allowed in foreign function + +ast_passes_patterns_in_fns_without_body = + patterns aren't allowed in functions without bodies + .label = pattern not allowed in function without body + +ast_passes_equality_constraint = + equality constraints are not yet supported in `where` clauses + .label = not supported + .assoc_constraint_suggestion = if `{$assoc_ty}` is an associated type you're trying to set, use the associated type binding syntax + .note = see issue #20041 for more information From af29599354c01c4309d88cc0babb6658f30b5b8e Mon Sep 17 00:00:00 2001 From: finalchild Date: Tue, 13 Sep 2022 02:00:11 +0900 Subject: [PATCH 15/23] Fix raw identifiers not handled correctly in EqualityConstraintToAssocConstraintSuggestion --- compiler/rustc_ast_passes/src/ast_validation.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 3b6cf193109d8..000ebb486367d 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1464,12 +1464,12 @@ fn deny_equality_constraints( if let Some(bound) = bound_opt { err.assoc_constraint_suggestion.push( EqualityConstraintToAssocConstraintSuggestion { - assoc_ty: assoc_ty_ident.name.to_string(), + assoc_ty: assoc_ty_ident.to_string(), suggestion: vec![( predicate.span, format!( "{}: {}", - qself_ty_ident.name, + qself_ty_ident, pprust::path_to_string(&bound) ), )], From 798064c42f71f99f4f80bea523debe0d1e14d1c4 Mon Sep 17 00:00:00 2001 From: finalchild Date: Thu, 29 Sep 2022 21:57:25 +0900 Subject: [PATCH 16/23] Fix rebase mistake --- compiler/rustc_errors/src/diagnostic.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 79ed8070643c8..a662429a59046 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -4,6 +4,8 @@ use crate::{ SubdiagnosticMessage, Substitution, SubstitutionPart, SuggestionStyle, }; +use rustc_ast as ast; +use rustc_ast_pretty::pprust; use rustc_data_structures::fx::FxHashMap; use rustc_error_messages::fluent_value_from_str_list_sep_by_and; use rustc_error_messages::FluentValue; From 4f4248ff043e246675ff6196cc9806bae62057a7 Mon Sep 17 00:00:00 2001 From: finalchild Date: Thu, 29 Sep 2022 22:17:34 +0900 Subject: [PATCH 17/23] Fix rebase mistake --- .../rustc_ast_passes/src/ast_validation.rs | 10 ++--- compiler/rustc_ast_passes/src/errors.rs | 42 +++++++++---------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 000ebb486367d..8df45b2ab679c 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -13,14 +13,14 @@ use rustc_ast::walk_list; use rustc_ast::*; use rustc_ast_pretty::pprust::{self, State}; use rustc_data_structures::fx::FxHashMap; -use rustc_errors::fluent; +use rustc_errors::{fluent, IntoDiagnostic}; use rustc_macros::Subdiagnostic; use rustc_parse::validate_attr; use rustc_session::lint::builtin::{ DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, PATTERNS_IN_FNS_WITHOUT_BODY, }; use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer}; -use rustc_session::{Session, SessionDiagnostic}; +use rustc_session::Session; use rustc_span::source_map::Spanned; use rustc_span::symbol::{kw, sym, Ident}; use rustc_span::Span; @@ -399,7 +399,7 @@ impl<'a> AstValidator<'a> { fn check_type_no_bounds(&self, bounds: &[GenericBound], create_diag: impl FnOnce(Span) -> D) where - D: SessionDiagnostic<'a>, + D: IntoDiagnostic<'a>, { let span = match bounds { [] => return, @@ -431,7 +431,7 @@ impl<'a> AstValidator<'a> { create_diag: impl FnOnce(Span, Span, Span) -> D, body: Option, ) where - D: SessionDiagnostic<'a>, + D: IntoDiagnostic<'a>, { let Some(body) = body else { return; @@ -502,7 +502,7 @@ impl<'a> AstValidator<'a> { fn check_item_named(&self, ident: Ident, create_diag: impl FnOnce(Span) -> D) where - D: SessionDiagnostic<'a>, + D: IntoDiagnostic<'a>, { if ident.name != kw::Underscore { return; diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index 8af8387bcaf75..c89ea24ca6615 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -461,7 +461,7 @@ pub struct GenericParamWrongOrder { pub correct_order: String, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::obsolete_auto_trait_syntax)] #[help] pub struct ObsoleteAutoTraitSyntax { @@ -469,7 +469,7 @@ pub struct ObsoleteAutoTraitSyntax { pub span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::unsafe_negative_impl, code = "E0198")] pub struct UnsafeNegativeImpl { #[primary_span] @@ -480,7 +480,7 @@ pub struct UnsafeNegativeImpl { pub unsafe_span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::unsafe_inherent_impl, code = "E0197")] pub struct UnsafeInherentImpl { #[primary_span] @@ -490,7 +490,7 @@ pub struct UnsafeInherentImpl { pub unsafe_span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::negative_inherent_impl)] pub struct NegativeInherentImpl { #[primary_span] @@ -500,7 +500,7 @@ pub struct NegativeInherentImpl { pub negative_span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::default_inherent_impl)] #[note] pub struct DefaultInherentImpl { @@ -511,7 +511,7 @@ pub struct DefaultInherentImpl { pub default_span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::const_inherent_impl)] #[note] pub struct ConstInherentImpl { @@ -522,28 +522,28 @@ pub struct ConstInherentImpl { pub const_span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::unsafe_extern_block)] pub struct UnsafeExternBlock { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::unsafe_module)] pub struct UnsafeModule { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::empty_union)] pub struct EmptyUnion { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::ty_alias_with_where_clause)] #[note] pub struct TyAliasWithWhereClause { @@ -551,21 +551,21 @@ pub struct TyAliasWithWhereClause { pub span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::generic_param_with_default_not_trailing)] pub struct GenericParamWithDefaultNotTrailing { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::lifetime_nested_quantification, code = "E0316")] pub struct LifetimeNestedQuantification { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::super_trait_with_maybe)] #[note] pub struct SuperTraitWithMaybe { @@ -574,14 +574,14 @@ pub struct SuperTraitWithMaybe { pub path_str: String, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::trait_object_with_maybe)] pub struct TraitObjectWithMaybe { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::forbidden_maybe_const)] #[note] pub struct ForbiddenMaybeConst { @@ -610,14 +610,14 @@ impl AddToDiagnostic for DisallowTildeConstContext { } } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::maybe_const_with_maybe_trait)] pub struct MaybeConstWithMaybeTrait { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::const_async_fn)] pub struct ConstAsyncFn { #[primary_span] @@ -630,7 +630,7 @@ pub struct ConstAsyncFn { pub fn_span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::patterns_in_foreign_fns, code = "E0130")] pub struct PatternsInForeignFns { #[primary_span] @@ -638,7 +638,7 @@ pub struct PatternsInForeignFns { pub span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::patterns_in_fns_without_body, code = "E0642")] pub struct PatternsInFnsWithoutBody { #[primary_span] @@ -646,7 +646,7 @@ pub struct PatternsInFnsWithoutBody { pub span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(ast_passes::equality_constraint)] #[note] pub struct EqualityConstraint { @@ -662,7 +662,7 @@ pub struct EqualityConstraintToAssocConstraintSuggestion { pub suggestion: Vec<(Span, String)>, } -impl AddSubdiagnostic for EqualityConstraintToAssocConstraintSuggestion { +impl AddToDiagnostic for EqualityConstraintToAssocConstraintSuggestion { fn add_to_diagnostic(self, diag: &mut Diagnostic) { diag.set_arg("assoc_ty", self.assoc_ty); diag.multipart_suggestion( From e3c627906c5da2373524ec95071afd49a4e1b76b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Park=20Jaeon=20=5B=ED=8C=8C=EC=B0=A8=5D?= Date: Thu, 13 Oct 2022 23:08:24 +0900 Subject: [PATCH 18/23] resolve rebase error --- compiler/rustc_ast_passes/src/errors.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index c89ea24ca6615..944d697a4e9d3 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -663,7 +663,10 @@ pub struct EqualityConstraintToAssocConstraintSuggestion { } impl AddToDiagnostic for EqualityConstraintToAssocConstraintSuggestion { - fn add_to_diagnostic(self, diag: &mut Diagnostic) { + fn add_to_diagnostic_with(self, diag: &mut Diagnostic, _: F) + where + F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage, + { diag.set_arg("assoc_ty", self.assoc_ty); diag.multipart_suggestion( fluent::ast_passes::assoc_constraint_suggestion, From 2a0f2cd37ac625abfd16b83bc933bddb31722db4 Mon Sep 17 00:00:00 2001 From: finalchild Date: Fri, 14 Oct 2022 16:59:07 +0900 Subject: [PATCH 19/23] Remove redundant change --- compiler/rustc_errors/src/diagnostic.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index a662429a59046..51b2ff6a00381 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -3,9 +3,6 @@ use crate::{ CodeSuggestion, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Level, MultiSpan, SubdiagnosticMessage, Substitution, SubstitutionPart, SuggestionStyle, }; - -use rustc_ast as ast; -use rustc_ast_pretty::pprust; use rustc_data_structures::fx::FxHashMap; use rustc_error_messages::fluent_value_from_str_list_sep_by_and; use rustc_error_messages::FluentValue; From 7ca0eb8ee9ea4f6f3c983f4ddb468b5ce9d0373d Mon Sep 17 00:00:00 2001 From: finalchild Date: Fri, 21 Oct 2022 23:07:20 +0900 Subject: [PATCH 20/23] fix rebase mistakes --- compiler/rustc_ast_passes/src/ast_validation.rs | 2 +- compiler/rustc_ast_passes/src/errors.rs | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 8df45b2ab679c..e48b5cc3b23da 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -37,7 +37,7 @@ enum SelfSemantic { } /// What is the context that prevents using `~const`? -enum DisallowTildeConstContext<'a> { +pub(crate) enum DisallowTildeConstContext<'a> { TraitObject, Fn(FnKind<'a>), } diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index 944d697a4e9d3..99f5edecd30e1 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -1,11 +1,11 @@ //! Errors emitted by ast_passes. -use rustc_ast::ParamKindOrd; +use rustc_ast::{visit::FnKind, ParamKindOrd}; use rustc_errors::{fluent, AddToDiagnostic, Applicability, Diagnostic, SubdiagnosticMessage}; use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_span::{Span, Symbol}; -use crate::ast_validation::ForbiddenLetReason; +use crate::ast_validation::{DisallowTildeConstContext, ForbiddenLetReason}; #[derive(Diagnostic)] #[diag(ast_passes_forbidden_let)] @@ -584,26 +584,26 @@ pub struct TraitObjectWithMaybe { #[derive(Diagnostic)] #[diag(ast_passes::forbidden_maybe_const)] #[note] -pub struct ForbiddenMaybeConst { +pub struct ForbiddenMaybeConst<'a, 'b> { #[primary_span] pub span: Span, #[subdiagnostic] - pub reason: DisallowTildeConstContext, + pub(crate) reason: &'a DisallowTildeConstContext<'b>, } -impl AddToDiagnostic for DisallowTildeConstContext { +impl<'a> AddToDiagnostic for &'a DisallowTildeConstContext<'_> { fn add_to_diagnostic_with(self, diag: &mut Diagnostic, _: F) where F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage, { match self { - Self::TraitObject => { + DisallowTildeConstContext::TraitObject => { diag.note(fluent::ast_passes::trait_object); } - Self::Fn(FnKind::Closure(..)) => { + DisallowTildeConstContext::Fn(FnKind::Closure(..)) => { diag.note(fluent::ast_passes::closure); } - Self::Fn(FnKind::Fn(_, ident, ..)) => { + DisallowTildeConstContext::Fn(FnKind::Fn(_, ident, ..)) => { diag.span_note(ident.span, fluent::ast_passes::fn_not_const); } } From facf6ac4082b5ee2bdb35516fa0d2370a25969fc Mon Sep 17 00:00:00 2001 From: finalchild Date: Fri, 21 Oct 2022 23:44:34 +0900 Subject: [PATCH 21/23] fix rebase mistake --- compiler/rustc_ast_passes/src/errors.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index 99f5edecd30e1..7222b3ac66d85 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -583,7 +583,6 @@ pub struct TraitObjectWithMaybe { #[derive(Diagnostic)] #[diag(ast_passes::forbidden_maybe_const)] -#[note] pub struct ForbiddenMaybeConst<'a, 'b> { #[primary_span] pub span: Span, From 85758739dfc56ea6cb5dacad6a94aeeae6ec35a1 Mon Sep 17 00:00:00 2001 From: finalchild Date: Wed, 26 Oct 2022 20:44:53 +0900 Subject: [PATCH 22/23] fix rebase --- .../rustc_ast_passes/src/ast_validation.rs | 6 +- compiler/rustc_ast_passes/src/errors.rs | 160 +++++++++--------- 2 files changed, 83 insertions(+), 83 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index e48b5cc3b23da..502765e03e764 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -673,7 +673,7 @@ impl<'a> AstValidator<'a> { MISSING_ABI, id, span, - fluent::ast_passes::deprecated_extern_missing_abi, + fluent::ast_passes_deprecated_extern_missing_abi, BuiltinLintDiagnostics::MissingAbi(span, abi::Abi::FALLBACK), ) } @@ -1274,8 +1274,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) { if let Some(ident) = ident { let msg = match ctxt { - FnCtxt::Foreign => fluent::ast_passes::patterns_in_foreign_fns, - _ => fluent::ast_passes::patterns_in_fns_without_body, + FnCtxt::Foreign => fluent::ast_passes_patterns_in_foreign_fns, + _ => fluent::ast_passes_patterns_in_fns_without_body, }; let diag = BuiltinLintDiagnostics::PatternsInFnsWithoutBody(span, ident); self.lint_buffer.buffer_lint_with_diagnostic( diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index 7222b3ac66d85..1ab0d32e9568c 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -249,97 +249,97 @@ pub struct ImplAssocTyWithBound { } #[derive(Diagnostic)] -#[diag(ast_passes::foreign_ty_with_generic_param)] -#[note(ast_passes::more_extern_note)] +#[diag(ast_passes_foreign_ty_with_generic_param)] +#[note(more_extern_note)] pub struct ForeignTyWithGenericParam { #[primary_span] #[suggestion(code = "", applicability = "maybe-incorrect")] pub span: Span, - #[label(ast_passes::extern_block_label)] + #[label(extern_block_label)] pub extern_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::foreign_ty_with_where_clause)] -#[note(ast_passes::more_extern_note)] +#[diag(ast_passes_foreign_ty_with_where_clause)] +#[note(more_extern_note)] pub struct ForeignTyWithWhereClause { #[primary_span] #[suggestion(code = "", applicability = "maybe-incorrect")] pub span: Span, - #[label(ast_passes::extern_block_label)] + #[label(extern_block_label)] pub extern_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::foreign_ty_with_body)] -#[note(ast_passes::more_extern_note)] +#[diag(ast_passes_foreign_ty_with_body)] +#[note(more_extern_note)] pub struct ForeignTyWithBody { #[primary_span] #[label] pub span: Span, - #[label(ast_passes::body_label)] + #[label(body_label)] pub body_span: Span, - #[label(ast_passes::extern_block_label)] + #[label(extern_block_label)] pub extern_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::foreign_static_with_body)] -#[note(ast_passes::more_extern_note)] +#[diag(ast_passes_foreign_static_with_body)] +#[note(more_extern_note)] pub struct ForeignStaticWithBody { #[primary_span] #[label] pub span: Span, - #[label(ast_passes::body_label)] + #[label(body_label)] pub body_span: Span, - #[label(ast_passes::extern_block_label)] + #[label(extern_block_label)] pub extern_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::foreign_fn_with_body)] +#[diag(ast_passes_foreign_fn_with_body)] #[help] -#[note(ast_passes::more_extern_note)] +#[note(more_extern_note)] pub struct ForeignFnWithBody { #[primary_span] #[label] pub span: Span, #[suggestion(code = ";", applicability = "maybe-incorrect")] pub body_span: Span, - #[label(ast_passes::extern_block_label)] + #[label(extern_block_label)] pub extern_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::foreign_fn_with_qualifier)] +#[diag(ast_passes_foreign_fn_with_qualifier)] pub struct ForeignFnWithQualifier { #[primary_span] pub span: Span, - #[label(ast_passes::extern_block_label)] + #[label(extern_block_label)] pub extern_span: Span, #[suggestion_verbose(code = "fn ", applicability = "maybe-incorrect")] pub replace_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::foreign_item_non_ascii)] +#[diag(ast_passes_foreign_item_non_ascii)] #[note] pub struct ForeignItemNonAscii { #[primary_span] pub span: Span, - #[label(ast_passes::extern_block_label)] + #[label(extern_block_label)] pub extern_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::forbidden_c_var_args)] +#[diag(ast_passes_forbidden_c_var_args)] pub struct ForbiddenCVarArgs { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::unnamed_assoc_const)] +#[diag(ast_passes_unnamed_assoc_const)] pub struct UnnamedAssocConst { #[primary_span] #[label] @@ -347,14 +347,14 @@ pub struct UnnamedAssocConst { } #[derive(Diagnostic)] -#[diag(ast_passes::nomangle_item_non_ascii, code = "E0754")] +#[diag(ast_passes_nomangle_item_non_ascii, code = "E0754")] pub struct NomangleItemNonAscii { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::mod_file_item_non_ascii, code = "E0754")] +#[diag(ast_passes_mod_file_item_non_ascii, code = "E0754")] #[help] pub struct ModFileItemNonAscii { #[primary_span] @@ -363,46 +363,46 @@ pub struct ModFileItemNonAscii { } #[derive(Diagnostic)] -#[diag(ast_passes::auto_trait_with_generic_param, code = "E0567")] +#[diag(ast_passes_auto_trait_with_generic_param, code = "E0567")] pub struct AutoTraitWithGenericParam { #[primary_span] #[suggestion(code = "", applicability = "machine-applicable")] pub span: Span, - #[label(ast_passes::ident_label)] + #[label(ident_label)] pub ident_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::auto_trait_with_super_trait_or_where_clause, code = "E0568")] +#[diag(ast_passes_auto_trait_with_super_trait_or_where_clause, code = "E0568")] pub struct AutoTraitWithSuperTraitOrWhereClause { #[primary_span] #[suggestion(code = "", applicability = "machine-applicable")] pub span: Span, - #[label(ast_passes::ident_label)] + #[label(ident_label)] pub ident_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::auto_trait_with_assoc_item, code = "E0380")] +#[diag(ast_passes_auto_trait_with_assoc_item, code = "E0380")] pub struct AutoTraitWithAssocItem { #[primary_span] pub spans: Vec, #[suggestion(code = "", applicability = "machine-applicable")] pub replace_span: Span, - #[label(ast_passes::ident_label)] + #[label(ident_label)] pub ident_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::generic_arg_after_constraint)] +#[diag(ast_passes_generic_arg_after_constraint)] pub struct GenericArgAfterConstraint { #[primary_span] pub arg_spans: Vec, - #[label(ast_passes::constraints_label)] + #[label(constraints_label)] pub constraint_spans: Vec, - #[label(ast_passes::last_arg_label)] + #[label(last_arg_label)] pub last_arg_span: Span, - #[label(ast_passes::first_constraint_label)] + #[label(first_constraint_label)] pub first_constraint_span: Span, #[suggestion_verbose(code = "{correct_order}", applicability = "machine-applicable")] pub replace_span: Span, @@ -412,45 +412,45 @@ pub struct GenericArgAfterConstraint { } #[derive(Diagnostic)] -#[diag(ast_passes::fn_ptr_ty_with_pat, code = "E0561")] +#[diag(ast_passes_fn_ptr_ty_with_pat, code = "E0561")] pub struct FnPtrTyWithPat { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::multiple_explicit_lifetime_bound, code = "E0226")] +#[diag(ast_passes_multiple_explicit_lifetime_bound, code = "E0226")] pub struct MultipleExplicitLifetimeBound { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::impl_trait_ty_in_path_param, code = "E0667")] +#[diag(ast_passes_impl_trait_ty_in_path_param, code = "E0667")] pub struct ImplTraitTyInPathParam { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::impl_trait_ty_nested, code = "E0666")] +#[diag(ast_passes_impl_trait_ty_nested, code = "E0666")] pub struct ImplTraitTyNested { #[primary_span] - #[label(ast_passes::nested_label)] + #[label(nested_label)] pub nested_span: Span, - #[label(ast_passes::outer_label)] + #[label(outer_label)] pub outer_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::impl_trait_ty_without_trait_bound)] +#[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)] +#[diag(ast_passes_generic_param_wrong_order)] pub struct GenericParamWrongOrder { #[primary_span] pub spans: Vec, @@ -462,7 +462,7 @@ pub struct GenericParamWrongOrder { } #[derive(Diagnostic)] -#[diag(ast_passes::obsolete_auto_trait_syntax)] +#[diag(ast_passes_obsolete_auto_trait_syntax)] #[help] pub struct ObsoleteAutoTraitSyntax { #[primary_span] @@ -470,81 +470,81 @@ pub struct ObsoleteAutoTraitSyntax { } #[derive(Diagnostic)] -#[diag(ast_passes::unsafe_negative_impl, code = "E0198")] +#[diag(ast_passes_unsafe_negative_impl, code = "E0198")] pub struct UnsafeNegativeImpl { #[primary_span] pub span: Span, - #[label(ast_passes::negative_label)] + #[label(negative_label)] pub negative_span: Span, - #[label(ast_passes::unsafe_label)] + #[label(unsafe_label)] pub unsafe_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::unsafe_inherent_impl, code = "E0197")] +#[diag(ast_passes_unsafe_inherent_impl, code = "E0197")] pub struct UnsafeInherentImpl { #[primary_span] - #[label(ast_passes::ty_label)] + #[label(ty_label)] pub span: Span, - #[label(ast_passes::unsafe_label)] + #[label(unsafe_label)] pub unsafe_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::negative_inherent_impl)] +#[diag(ast_passes_negative_inherent_impl)] pub struct NegativeInherentImpl { #[primary_span] - #[label(ast_passes::ty_label)] + #[label(ty_label)] pub span: Span, - #[label(ast_passes::negative_label)] + #[label(negative_label)] pub negative_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::default_inherent_impl)] +#[diag(ast_passes_default_inherent_impl)] #[note] pub struct DefaultInherentImpl { #[primary_span] - #[label(ast_passes::ty_label)] + #[label(ty_label)] pub span: Span, - #[label(ast_passes::default_label)] + #[label(default_label)] pub default_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::const_inherent_impl)] +#[diag(ast_passes_const_inherent_impl)] #[note] pub struct ConstInherentImpl { #[primary_span] - #[label(ast_passes::ty_label)] + #[label(ty_label)] pub span: Span, - #[label(ast_passes::const_label)] + #[label(const_label)] pub const_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::unsafe_extern_block)] +#[diag(ast_passes_unsafe_extern_block)] pub struct UnsafeExternBlock { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::unsafe_module)] +#[diag(ast_passes_unsafe_module)] pub struct UnsafeModule { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::empty_union)] +#[diag(ast_passes_empty_union)] pub struct EmptyUnion { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::ty_alias_with_where_clause)] +#[diag(ast_passes_ty_alias_with_where_clause)] #[note] pub struct TyAliasWithWhereClause { #[primary_span] @@ -552,21 +552,21 @@ pub struct TyAliasWithWhereClause { } #[derive(Diagnostic)] -#[diag(ast_passes::generic_param_with_default_not_trailing)] +#[diag(ast_passes_generic_param_with_default_not_trailing)] pub struct GenericParamWithDefaultNotTrailing { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::lifetime_nested_quantification, code = "E0316")] +#[diag(ast_passes_lifetime_nested_quantification, code = "E0316")] pub struct LifetimeNestedQuantification { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::super_trait_with_maybe)] +#[diag(ast_passes_super_trait_with_maybe)] #[note] pub struct SuperTraitWithMaybe { #[primary_span] @@ -575,14 +575,14 @@ pub struct SuperTraitWithMaybe { } #[derive(Diagnostic)] -#[diag(ast_passes::trait_object_with_maybe)] +#[diag(ast_passes_trait_object_with_maybe)] pub struct TraitObjectWithMaybe { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::forbidden_maybe_const)] +#[diag(ast_passes_forbidden_maybe_const)] pub struct ForbiddenMaybeConst<'a, 'b> { #[primary_span] pub span: Span, @@ -597,40 +597,40 @@ impl<'a> AddToDiagnostic for &'a DisallowTildeConstContext<'_> { { match self { DisallowTildeConstContext::TraitObject => { - diag.note(fluent::ast_passes::trait_object); + diag.note(fluent::trait_object); } DisallowTildeConstContext::Fn(FnKind::Closure(..)) => { - diag.note(fluent::ast_passes::closure); + diag.note(fluent::closure); } DisallowTildeConstContext::Fn(FnKind::Fn(_, ident, ..)) => { - diag.span_note(ident.span, fluent::ast_passes::fn_not_const); + diag.span_note(ident.span, fluent::fn_not_const); } } } } #[derive(Diagnostic)] -#[diag(ast_passes::maybe_const_with_maybe_trait)] +#[diag(ast_passes_maybe_const_with_maybe_trait)] pub struct MaybeConstWithMaybeTrait { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::const_async_fn)] +#[diag(ast_passes_const_async_fn)] pub struct ConstAsyncFn { #[primary_span] pub spans: Vec, - #[label(ast_passes::const_label)] + #[label(const_label)] pub const_span: Span, - #[label(ast_passes::async_label)] + #[label(async_label)] pub async_span: Span, - #[label(ast_passes::fn_label)] + #[label(fn_label)] pub fn_span: Span, } #[derive(Diagnostic)] -#[diag(ast_passes::patterns_in_foreign_fns, code = "E0130")] +#[diag(ast_passes_patterns_in_foreign_fns, code = "E0130")] pub struct PatternsInForeignFns { #[primary_span] #[label] @@ -638,7 +638,7 @@ pub struct PatternsInForeignFns { } #[derive(Diagnostic)] -#[diag(ast_passes::patterns_in_fns_without_body, code = "E0642")] +#[diag(ast_passes_patterns_in_fns_without_body, code = "E0642")] pub struct PatternsInFnsWithoutBody { #[primary_span] #[label] @@ -646,7 +646,7 @@ pub struct PatternsInFnsWithoutBody { } #[derive(Diagnostic)] -#[diag(ast_passes::equality_constraint)] +#[diag(ast_passes_equality_constraint)] #[note] pub struct EqualityConstraint { #[primary_span] @@ -668,7 +668,7 @@ impl AddToDiagnostic for EqualityConstraintToAssocConstraintSuggestion { { diag.set_arg("assoc_ty", self.assoc_ty); diag.multipart_suggestion( - fluent::ast_passes::assoc_constraint_suggestion, + fluent::assoc_constraint_suggestion, self.suggestion, Applicability::MaybeIncorrect, ); From 56d74265b30f750f2bbda48b4ce2e3cd615de035 Mon Sep 17 00:00:00 2001 From: finalchild Date: Thu, 26 Jan 2023 04:06:37 +0900 Subject: [PATCH 23/23] fix rebase --- compiler/rustc_ast_passes/src/errors.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index 1ab0d32e9568c..3e7ac966c30d7 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -317,7 +317,7 @@ pub struct ForeignFnWithQualifier { pub span: Span, #[label(extern_block_label)] pub extern_span: Span, - #[suggestion_verbose(code = "fn ", applicability = "maybe-incorrect")] + #[suggestion(style = "verbose", code = "fn ", applicability = "maybe-incorrect")] pub replace_span: Span, } @@ -404,7 +404,11 @@ pub struct GenericArgAfterConstraint { pub last_arg_span: Span, #[label(first_constraint_label)] pub first_constraint_span: Span, - #[suggestion_verbose(code = "{correct_order}", applicability = "machine-applicable")] + #[suggestion( + style = "verbose", + code = "{correct_order}", + applicability = "machine-applicable" + )] pub replace_span: Span, pub args_len: usize, pub constraints_len: usize,