]> git.lizzy.rs Git - rust.git/commitdiff
Made ty_or_sig and trait_path use their actual types instead of String
authorNikita Tomashevich <quant3234@gmail.com>
Thu, 3 Nov 2022 18:50:52 +0000 (21:50 +0300)
committerNikita Tomashevich <quant3234@gmail.com>
Wed, 28 Dec 2022 11:53:48 +0000 (14:53 +0300)
compiler/rustc_infer/src/errors/mod.rs
compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mod.rs
compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs

index 9e3def93a6d3df8b5642eb2a01b0d3fd0661b5e5..388ddd09905ac1d4e270ab5b3b1fe76d983f9f74 100644 (file)
@@ -1,16 +1,18 @@
 use hir::GenericParamKind;
 use rustc_errors::{
     fluent, AddToDiagnostic, Applicability, Diagnostic, DiagnosticMessage, DiagnosticStyledString,
-    MultiSpan, SubdiagnosticMessage,
+    IntoDiagnosticArg, MultiSpan, SubdiagnosticMessage,
 };
 use rustc_hir as hir;
 use rustc_hir::FnRetTy;
 use rustc_macros::{Diagnostic, Subdiagnostic};
-use rustc_middle::ty::{Region, Ty, TyCtxt};
+use rustc_middle::ty::print::TraitRefPrintOnlyTraitPath;
+use rustc_middle::ty::{Binder, FnSig, Region, Ty, TyCtxt};
 use rustc_span::symbol::kw;
 use rustc_span::Symbol;
 use rustc_span::{symbol::Ident, BytePos, Span};
 
+use crate::infer::error_reporting::nice_region_error::placeholder_error::Highlighted;
 use crate::infer::error_reporting::{
     need_type_info::{GeneratorKindAsDiagArg, UnderspecifiedArgKind},
     ObligationCauseAsDiagArg,
@@ -557,91 +559,126 @@ pub enum ExplicitLifetimeRequired<'a> {
     },
 }
 
+pub enum TyOrSig<'tcx> {
+    Ty(Highlighted<'tcx, Ty<'tcx>>),
+    ClosureSig(Highlighted<'tcx, Binder<'tcx, FnSig<'tcx>>>),
+}
+
+impl IntoDiagnosticArg for TyOrSig<'_> {
+    fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
+        match self {
+            TyOrSig::Ty(ty) => ty.into_diagnostic_arg(),
+            TyOrSig::ClosureSig(sig) => sig.into_diagnostic_arg(),
+        }
+    }
+}
+
 #[derive(Subdiagnostic)]
-pub enum ActualImplExplNotes {
+pub enum ActualImplExplNotes<'tcx> {
     #[note(infer_actual_impl_expl_expected_signature_two)]
     ExpectedSignatureTwo {
         leading_ellipsis: bool,
-        ty_or_sig: String,
-        trait_path: String,
+        ty_or_sig: TyOrSig<'tcx>,
+        trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
         lifetime_1: usize,
         lifetime_2: usize,
     },
     #[note(infer_actual_impl_expl_expected_signature_any)]
     ExpectedSignatureAny {
         leading_ellipsis: bool,
-        ty_or_sig: String,
-        trait_path: String,
+        ty_or_sig: TyOrSig<'tcx>,
+        trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
         lifetime_1: usize,
     },
     #[note(infer_actual_impl_expl_expected_signature_some)]
     ExpectedSignatureSome {
         leading_ellipsis: bool,
-        ty_or_sig: String,
-        trait_path: String,
+        ty_or_sig: TyOrSig<'tcx>,
+        trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
         lifetime_1: usize,
     },
     #[note(infer_actual_impl_expl_expected_signature_nothing)]
-    ExpectedSignatureNothing { leading_ellipsis: bool, ty_or_sig: String, trait_path: String },
+    ExpectedSignatureNothing {
+        leading_ellipsis: bool,
+        ty_or_sig: TyOrSig<'tcx>,
+        trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
+    },
     #[note(infer_actual_impl_expl_expected_passive_two)]
     ExpectedPassiveTwo {
         leading_ellipsis: bool,
-        ty_or_sig: String,
-        trait_path: String,
+        ty_or_sig: TyOrSig<'tcx>,
+        trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
         lifetime_1: usize,
         lifetime_2: usize,
     },
     #[note(infer_actual_impl_expl_expected_passive_any)]
     ExpectedPassiveAny {
         leading_ellipsis: bool,
-        ty_or_sig: String,
-        trait_path: String,
+        ty_or_sig: TyOrSig<'tcx>,
+        trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
         lifetime_1: usize,
     },
     #[note(infer_actual_impl_expl_expected_passive_some)]
     ExpectedPassiveSome {
         leading_ellipsis: bool,
-        ty_or_sig: String,
-        trait_path: String,
+        ty_or_sig: TyOrSig<'tcx>,
+        trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
         lifetime_1: usize,
     },
     #[note(infer_actual_impl_expl_expected_passive_nothing)]
-    ExpectedPassiveNothing { leading_ellipsis: bool, ty_or_sig: String, trait_path: String },
+    ExpectedPassiveNothing {
+        leading_ellipsis: bool,
+        ty_or_sig: TyOrSig<'tcx>,
+        trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
+    },
     #[note(infer_actual_impl_expl_expected_other_two)]
     ExpectedOtherTwo {
         leading_ellipsis: bool,
-        ty_or_sig: String,
-        trait_path: String,
+        ty_or_sig: TyOrSig<'tcx>,
+        trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
         lifetime_1: usize,
         lifetime_2: usize,
     },
     #[note(infer_actual_impl_expl_expected_other_any)]
     ExpectedOtherAny {
         leading_ellipsis: bool,
-        ty_or_sig: String,
-        trait_path: String,
+        ty_or_sig: TyOrSig<'tcx>,
+        trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
         lifetime_1: usize,
     },
     #[note(infer_actual_impl_expl_expected_other_some)]
     ExpectedOtherSome {
         leading_ellipsis: bool,
-        ty_or_sig: String,
-        trait_path: String,
+        ty_or_sig: TyOrSig<'tcx>,
+        trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
         lifetime_1: usize,
     },
     #[note(infer_actual_impl_expl_expected_other_nothing)]
-    ExpectedOtherNothing { leading_ellipsis: bool, ty_or_sig: String, trait_path: String },
+    ExpectedOtherNothing {
+        leading_ellipsis: bool,
+        ty_or_sig: TyOrSig<'tcx>,
+        trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
+    },
     #[note(infer_actual_impl_expl_but_actually_implements_trait)]
-    ButActuallyImplementsTrait { trait_path: String, has_lifetime: bool, lifetime: usize },
+    ButActuallyImplementsTrait {
+        trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
+        has_lifetime: bool,
+        lifetime: usize,
+    },
     #[note(infer_actual_impl_expl_but_actually_implemented_for_ty)]
     ButActuallyImplementedForTy {
-        trait_path: String,
+        trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
         has_lifetime: bool,
         lifetime: usize,
         ty: String,
     },
     #[note(infer_actual_impl_expl_but_actually_ty_implements)]
-    ButActuallyTyImplements { trait_path: String, has_lifetime: bool, lifetime: usize, ty: String },
+    ButActuallyTyImplements {
+        trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
+        has_lifetime: bool,
+        lifetime: usize,
+        ty: String,
+    },
 }
 
 pub enum ActualImplExpectedKind {
@@ -657,13 +694,13 @@ pub enum ActualImplExpectedLifetimeKind {
     Nothing,
 }
 
-impl ActualImplExplNotes {
+impl<'tcx> ActualImplExplNotes<'tcx> {
     pub fn new_expected(
         kind: ActualImplExpectedKind,
         lt_kind: ActualImplExpectedLifetimeKind,
         leading_ellipsis: bool,
-        ty_or_sig: String,
-        trait_path: String,
+        ty_or_sig: TyOrSig<'tcx>,
+        trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
         lifetime_1: usize,
         lifetime_2: usize,
     ) -> Self {
@@ -728,7 +765,7 @@ pub fn new_expected(
 
 #[derive(Diagnostic)]
 #[diag(infer_trait_placeholder_mismatch)]
-pub struct TraitPlaceholderMismatch {
+pub struct TraitPlaceholderMismatch<'tcx> {
     #[primary_span]
     pub span: Span,
     #[label(label_satisfy)]
@@ -741,7 +778,7 @@ pub struct TraitPlaceholderMismatch {
     pub trait_def_id: String,
 
     #[subdiagnostic(eager)]
-    pub actual_impl_expl_notes: Vec<ActualImplExplNotes>,
+    pub actual_impl_expl_notes: Vec<ActualImplExplNotes<'tcx>>,
 }
 
 pub struct ConsiderBorrowingParamHelp {
index 8a0e332f9c704b4f5f958e6f59bd546c65fa629a..59fb74eb54378f0c1e836c33214251659871d701 100644 (file)
@@ -9,7 +9,7 @@
 pub mod find_anon_type;
 mod mismatched_static_lifetime;
 mod named_anon_conflict;
-mod placeholder_error;
+pub(crate) mod placeholder_error;
 mod placeholder_relation;
 mod static_impl_trait;
 mod trait_impl_difference;
index 904723c4da9d7e3a0b07790f6d588f35508d5032..2ff2d1926a260bf66cf6c6163c00f57ccdd9b96d 100644 (file)
@@ -1,6 +1,6 @@
 use crate::errors::{
     ActualImplExpectedKind, ActualImplExpectedLifetimeKind, ActualImplExplNotes,
-    TraitPlaceholderMismatch,
+    TraitPlaceholderMismatch, TyOrSig,
 };
 use crate::infer::error_reporting::nice_region_error::NiceRegionError;
 use crate::infer::lexical_region_resolve::RegionResolutionError;
@@ -8,7 +8,7 @@
 use crate::infer::{SubregionOrigin, TypeTrace};
 use crate::traits::{ObligationCause, ObligationCauseCode};
 use rustc_data_structures::intern::Interned;
-use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
+use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, IntoDiagnosticArg};
 use rustc_hir::def::Namespace;
 use rustc_hir::def_id::DefId;
 use rustc_middle::ty::error::ExpectedFound;
 
 use std::fmt;
 
+// HACK(eddyb) maybe move this in a more central location.
+#[derive(Copy, Clone)]
+pub struct Highlighted<'tcx, T> {
+    tcx: TyCtxt<'tcx>,
+    highlight: RegionHighlightMode<'tcx>,
+    value: T,
+}
+
+impl<'tcx, T> IntoDiagnosticArg for Highlighted<'tcx, T>
+where
+    T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>, Error = fmt::Error, Output = FmtPrinter<'a, 'tcx>>,
+{
+    fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
+        rustc_errors::DiagnosticArgValue::Str(self.to_string().into())
+    }
+}
+
+impl<'tcx, T> Highlighted<'tcx, T> {
+    fn map<U>(self, f: impl FnOnce(T) -> U) -> Highlighted<'tcx, U> {
+        Highlighted { tcx: self.tcx, highlight: self.highlight, value: f(self.value) }
+    }
+}
+
+impl<'tcx, T> fmt::Display for Highlighted<'tcx, T>
+where
+    T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>, Error = fmt::Error, Output = FmtPrinter<'a, 'tcx>>,
+{
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS);
+        printer.region_highlight_mode = self.highlight;
+
+        let s = self.value.print(printer)?.into_buffer();
+        f.write_str(&s)
+    }
+}
+
 impl<'tcx> NiceRegionError<'_, 'tcx> {
     /// When given a `ConcreteFailure` for a function with arguments containing a named region and
     /// an anonymous region, emit a descriptive diagnostic error.
@@ -328,39 +364,7 @@ fn explain_actual_impl_that_was_found(
         actual_has_vid: Option<usize>,
         any_self_ty_has_vid: bool,
         leading_ellipsis: bool,
-    ) -> Vec<ActualImplExplNotes> {
-        // HACK(eddyb) maybe move this in a more central location.
-        #[derive(Copy, Clone)]
-        struct Highlighted<'tcx, T> {
-            tcx: TyCtxt<'tcx>,
-            highlight: RegionHighlightMode<'tcx>,
-            value: T,
-        }
-
-        impl<'tcx, T> Highlighted<'tcx, T> {
-            fn map<U>(self, f: impl FnOnce(T) -> U) -> Highlighted<'tcx, U> {
-                Highlighted { tcx: self.tcx, highlight: self.highlight, value: f(self.value) }
-            }
-        }
-
-        impl<'tcx, T> fmt::Display for Highlighted<'tcx, T>
-        where
-            T: for<'a> Print<
-                'tcx,
-                FmtPrinter<'a, 'tcx>,
-                Error = fmt::Error,
-                Output = FmtPrinter<'a, 'tcx>,
-            >,
-        {
-            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-                let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS);
-                printer.region_highlight_mode = self.highlight;
-
-                let s = self.value.print(printer)?.into_buffer();
-                f.write_str(&s)
-            }
-        }
-
+    ) -> Vec<ActualImplExplNotes<'tcx>> {
         // The weird thing here with the `maybe_highlighting_region` calls and the
         // the match inside is meant to be like this:
         //
@@ -418,27 +422,27 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                 });
                 (
                     ActualImplExpectedKind::Signature,
-                    closure_sig.to_string(),
-                    expected_trait_ref.map(|tr| tr.print_only_trait_path()).to_string(),
+                    TyOrSig::ClosureSig(closure_sig),
+                    expected_trait_ref.map(|tr| tr.print_only_trait_path()),
                 )
             } else {
                 (
                     ActualImplExpectedKind::Other,
-                    self_ty.to_string(),
-                    expected_trait_ref.map(|tr| tr.print_only_trait_path()).to_string(),
+                    TyOrSig::Ty(self_ty),
+                    expected_trait_ref.map(|tr| tr.print_only_trait_path()),
                 )
             }
         } else if passive_voice {
             (
                 ActualImplExpectedKind::Passive,
-                expected_trait_ref.map(|tr| tr.self_ty()).to_string(),
-                expected_trait_ref.map(|tr| tr.print_only_trait_path()).to_string(),
+                TyOrSig::Ty(expected_trait_ref.map(|tr| tr.self_ty())),
+                expected_trait_ref.map(|tr| tr.print_only_trait_path()),
             )
         } else {
             (
                 ActualImplExpectedKind::Other,
-                expected_trait_ref.map(|tr| tr.self_ty()).to_string(),
-                expected_trait_ref.map(|tr| tr.print_only_trait_path()).to_string(),
+                TyOrSig::Ty(expected_trait_ref.map(|tr| tr.self_ty())),
+                expected_trait_ref.map(|tr| tr.print_only_trait_path()),
             )
         };
 
@@ -474,7 +478,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
             None => true,
         };
 
-        let trait_path = actual_trait_ref.map(|tr| tr.print_only_trait_path()).to_string();
+        let trait_path = actual_trait_ref.map(|tr| tr.print_only_trait_path());
         let ty = actual_trait_ref.map(|tr| tr.self_ty()).to_string();
         let has_lifetime = actual_has_vid.is_some();
         let lifetime = actual_has_vid.unwrap_or_default();