]> git.lizzy.rs Git - rust.git/commitdiff
Migrate ast_lowering::lib and ast_lowering::item to SessionDiagnostic
authorJean CASPAR <55629512+JeanCASPAR@users.noreply.github.com>
Wed, 17 Aug 2022 14:58:57 +0000 (16:58 +0200)
committerJean CASPAR <55629512+JeanCASPAR@users.noreply.github.com>
Mon, 22 Aug 2022 17:19:59 +0000 (19:19 +0200)
compiler/rustc_ast_lowering/src/errors.rs
compiler/rustc_ast_lowering/src/item.rs
compiler/rustc_ast_lowering/src/lib.rs
compiler/rustc_error_messages/locales/en-US/ast_lowering.ftl

index 8701491c9eb3d08e6d7562bd244cbc8c0929ce3b..c6337ce161d6f51a0bba26c31fcf9938c17e942e 100644 (file)
@@ -1,6 +1,6 @@
 use rustc_errors::{fluent, AddSubdiagnostic, Applicability, Diagnostic};
 use rustc_macros::SessionDiagnostic;
-use rustc_span::Span;
+use rustc_span::{Span, Symbol};
 
 #[derive(SessionDiagnostic, Clone, Copy)]
 #[error(ast_lowering::generic_type_with_parentheses, code = "E0214")]
@@ -27,3 +27,54 @@ fn add_to_diagnostic(self, diag: &mut Diagnostic) {
         );
     }
 }
+
+#[derive(SessionDiagnostic)]
+#[help]
+#[error(ast_lowering::invalid_abi, code = "E0703")]
+pub struct InvalidAbi {
+    #[primary_span]
+    #[label]
+    pub span: Span,
+    pub abi: Symbol,
+    pub valid_abis: String,
+}
+
+#[derive(SessionDiagnostic, Clone, Copy)]
+#[error(ast_lowering::assoc_ty_parentheses)]
+pub struct AssocTyParentheses {
+    #[primary_span]
+    pub span: Span,
+    #[subdiagnostic]
+    pub sub: AssocTyParenthesesSub,
+}
+
+#[derive(Clone, Copy)]
+pub enum AssocTyParenthesesSub {
+    Empty { parentheses_span: Span },
+    NotEmpty { open_param: Span, close_param: Span },
+}
+
+impl AddSubdiagnostic for AssocTyParenthesesSub {
+    fn add_to_diagnostic(self, diag: &mut Diagnostic) {
+        match self {
+            Self::Empty { parentheses_span } => diag.multipart_suggestion(
+                fluent::ast_lowering::remove_parentheses,
+                vec![(parentheses_span, String::new())],
+                Applicability::MaybeIncorrect,
+            ),
+            Self::NotEmpty { open_param, close_param } => diag.multipart_suggestion(
+                fluent::ast_lowering::use_angle_brackets,
+                vec![(open_param, String::from("<")), (close_param, String::from(">"))],
+                Applicability::MaybeIncorrect,
+            ),
+        };
+    }
+}
+
+#[derive(SessionDiagnostic)]
+#[error(ast_lowering::misplaced_impl_trait, code = "E0562")]
+pub struct MisplacedImplTrait {
+    #[primary_span]
+    pub span: Span,
+    pub position: String,
+}
index 0f1bab24f96ca8036673d4c203790376df0ea8f3..a789268dfdaff5a901558d5da14425b78c25e346 100644 (file)
@@ -1,3 +1,4 @@
+use super::errors::InvalidAbi;
 use super::ResolverAstLoweringExt;
 use super::{AstOwner, ImplTraitContext, ImplTraitPosition};
 use super::{FnDeclKind, LoweringContext, ParamMode};
@@ -7,7 +8,6 @@
 use rustc_ast::*;
 use rustc_data_structures::fx::FxHashMap;
 use rustc_data_structures::sorted_map::SortedMap;
-use rustc_errors::struct_span_err;
 use rustc_hir as hir;
 use rustc_hir::def::{DefKind, Res};
 use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
@@ -1260,10 +1260,11 @@ pub(super) fn lower_extern(&mut self, ext: Extern) -> abi::Abi {
     }
 
     fn error_on_invalid_abi(&self, abi: StrLit) {
-        struct_span_err!(self.tcx.sess, abi.span, E0703, "invalid ABI: found `{}`", abi.symbol)
-            .span_label(abi.span, "invalid ABI")
-            .help(&format!("valid ABIs: {}", abi::all_names().join(", ")))
-            .emit();
+        self.tcx.sess.emit_err(InvalidAbi {
+            span: abi.span,
+            abi: abi.symbol,
+            valid_abis: abi::all_names().join(", "),
+        });
     }
 
     fn lower_asyncness(&mut self, a: Async) -> hir::IsAsync {
index c1d9a7e690e690c590e125554f711b317998320c..a44c3c0d23b9e077481e128e9c4a15ddb68c70d1 100644 (file)
@@ -39,6 +39,8 @@
 #[macro_use]
 extern crate tracing;
 
+use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait};
+
 use rustc_ast::ptr::P;
 use rustc_ast::visit;
 use rustc_ast::{self as ast, *};
@@ -49,7 +51,7 @@
 use rustc_data_structures::sorted_map::SortedMap;
 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
 use rustc_data_structures::sync::Lrc;
-use rustc_errors::{struct_span_err, Applicability, Handler, StashKey};
+use rustc_errors::{Handler, StashKey};
 use rustc_hir as hir;
 use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
 use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
@@ -1071,19 +1073,11 @@ fn lower_assoc_ty_constraint(
     }
 
     fn emit_bad_parenthesized_trait_in_assoc_ty(&self, data: &ParenthesizedArgs) {
-        let mut err = self.tcx.sess.struct_span_err(
-            data.span,
-            "parenthesized generic arguments cannot be used in associated type constraints",
-        );
         // Suggest removing empty parentheses: "Trait()" -> "Trait"
-        if data.inputs.is_empty() {
+        let sub = if data.inputs.is_empty() {
             let parentheses_span =
                 data.inputs_span.shrink_to_lo().to(data.inputs_span.shrink_to_hi());
-            err.multipart_suggestion(
-                "remove these parentheses",
-                vec![(parentheses_span, String::new())],
-                Applicability::MaybeIncorrect,
-            );
+            AssocTyParenthesesSub::Empty { parentheses_span }
         }
         // Suggest replacing parentheses with angle brackets `Trait(params...)` to `Trait<params...>`
         else {
@@ -1097,13 +1091,9 @@ fn emit_bad_parenthesized_trait_in_assoc_ty(&self, data: &ParenthesizedArgs) {
             // End of last argument to end of parameters
             let close_param =
                 data.inputs.last().unwrap().span.shrink_to_hi().to(data.inputs_span.shrink_to_hi());
-            err.multipart_suggestion(
-                &format!("use angle brackets instead",),
-                vec![(open_param, String::from("<")), (close_param, String::from(">"))],
-                Applicability::MaybeIncorrect,
-            );
-        }
-        err.emit();
+            AssocTyParenthesesSub::NotEmpty { open_param, close_param }
+        };
+        self.tcx.sess.emit_err(AssocTyParentheses { span: data.span, sub });
     }
 
     #[instrument(level = "debug", skip(self))]
@@ -1342,14 +1332,10 @@ fn lower_ty_direct(&mut self, t: &Ty, itctx: ImplTraitContext) -> hir::Ty<'hir>
                         path
                     }
                     ImplTraitContext::Disallowed(position) => {
-                        let mut err = struct_span_err!(
-                            self.tcx.sess,
-                            t.span,
-                            E0562,
-                            "`impl Trait` only allowed in function and inherent method return types, not in {}",
-                            position
-                        );
-                        err.emit();
+                        self.tcx.sess.emit_err(MisplacedImplTrait {
+                            span: t.span,
+                            position: position.to_string(),
+                        });
                         hir::TyKind::Err
                     }
                 }
index ac7fc37e37dd57fd91a4309975bb7b56a1fd164e..3856dd1050bec7e2f1a306e8b35a4c29065356df 100644 (file)
@@ -3,3 +3,16 @@ ast_lowering_generic_type_with_parentheses =
     .label = only `Fn` traits may use parentheses
 
 ast_lowering_use_angle_brackets = use angle brackets instead
+
+ast_lowering_invalid_abi =
+    invalid ABI: found `{$abi}`
+    .label = invalid ABI
+    .help = valid ABIs: {$valid_abis}
+
+ast_lowering_assoc_ty_parentheses =
+    parenthesized generic arguments cannot be used in associated type constraints
+
+ast_lowering_remove_parentheses = remove these parentheses
+
+ast_lowering_misplaced_impl_trait =
+    `impl Trait` only allowed in function and inherent method return types, not in {$position}