about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2020-01-08 09:26:47 -0800
committerEsteban Küber <esteban@kuber.com.ar>2020-01-08 09:30:27 -0800
commit705e0874de4787dd65eb8484fd67ad635e136c37 (patch)
tree8f40d87b05c9e11668153bf1dce65f55b492a5d2
parentffcdbad263fc2522696b3376627aca503fe0fe8f (diff)
reduce code duplication
-rw-r--r--src/librustc/infer/error_reporting/mod.rs70
-rw-r--r--src/librustc/traits/error_reporting.rs13
2 files changed, 38 insertions, 45 deletions
diff --git a/src/librustc/infer/error_reporting/mod.rs b/src/librustc/infer/error_reporting/mod.rs
index b6d6ca0dcef..27523b1e689 100644
--- a/src/librustc/infer/error_reporting/mod.rs
+++ b/src/librustc/infer/error_reporting/mod.rs
@@ -1303,39 +1303,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
             _ => {}
         }
 
-        /// This is a bare signal of what kind of type we're dealing with. `ty::TyKind` tracks
-        /// extra information about each type, but we only care about the category.
-        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
-        enum TyCategory {
-            Closure,
-            Opaque,
-            Generator,
-            Foreign,
-        }
-
-        impl TyCategory {
-            fn descr(&self) -> &'static str {
-                match self {
-                    Self::Closure => "closure",
-                    Self::Opaque => "opaque type",
-                    Self::Generator => "generator",
-                    Self::Foreign => "foreign type",
-                }
-            }
-
-            fn from_ty(ty: Ty<'_>) -> Option<(Self, DefId)> {
-                match ty.kind {
-                    ty::Closure(def_id, _) => Some((Self::Closure, def_id)),
-                    ty::Opaque(def_id, _) => Some((Self::Opaque, def_id)),
-                    ty::Generator(def_id, ..) => Some((Self::Generator, def_id)),
-                    ty::Foreign(def_id) => Some((Self::Foreign, def_id)),
-                    _ => None,
-                }
-            }
-        }
-
         struct OpaqueTypesVisitor<'tcx> {
-            types: FxHashMap<TyKind, FxHashSet<Span>>,
+            types: FxHashMap<TyCategory, FxHashSet<Span>>,
             expected: FxHashMap<TyCategory, FxHashSet<Span>>,
             found: FxHashMap<TyCategory, FxHashSet<Span>>,
             ignore_span: Span,
@@ -1375,7 +1344,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
                 &self,
                 err: &mut DiagnosticBuilder<'_>,
                 target: &str,
-                types: &FxHashMap<TyKind, FxHashSet<Span>>,
+                types: &FxHashMap<TyCategory, FxHashSet<Span>>,
             ) {
                 for (key, values) in types.iter() {
                     let count = values.len();
@@ -1394,7 +1363,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
                                 },
                                 if count > 1 { "one of the " } else { "" },
                                 target,
-                                key,
+                                kind,
                                 pluralize!(count),
                             ),
                         );
@@ -1405,7 +1374,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
 
         impl<'tcx> ty::fold::TypeVisitor<'tcx> for OpaqueTypesVisitor<'tcx> {
             fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
-                if let Some((kind, def_id)) = TyKind::from_ty(t) {
+                if let Some((kind, def_id)) = TyCategory::from_ty(t) {
                     let span = self.tcx.def_span(def_id);
                     // Avoid cluttering the output when the "found" and error span overlap:
                     //
@@ -2067,3 +2036,34 @@ impl<'tcx> ObligationCause<'tcx> {
         }
     }
 }
+
+/// This is a bare signal of what kind of type we're dealing with. `ty::TyKind` tracks
+/// extra information about each type, but we only care about the category.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+crate enum TyCategory {
+    Closure,
+    Opaque,
+    Generator,
+    Foreign,
+}
+
+impl TyCategory {
+    fn descr(&self) -> &'static str {
+        match self {
+            Self::Closure => "closure",
+            Self::Opaque => "opaque type",
+            Self::Generator => "generator",
+            Self::Foreign => "foreign type",
+        }
+    }
+
+    pub fn from_ty(ty: Ty<'_>) -> Option<(Self, DefId)> {
+        match ty.kind {
+            ty::Closure(def_id, _) => Some((Self::Closure, def_id)),
+            ty::Opaque(def_id, _) => Some((Self::Opaque, def_id)),
+            ty::Generator(def_id, ..) => Some((Self::Generator, def_id)),
+            ty::Foreign(def_id) => Some((Self::Foreign, def_id)),
+            _ => None,
+        }
+    }
+}
diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs
index 8c2cc412a48..a42ddbbbcc6 100644
--- a/src/librustc/traits/error_reporting.rs
+++ b/src/librustc/traits/error_reporting.rs
@@ -6,7 +6,7 @@ use super::{
     TraitNotObjectSafe,
 };
 
-use crate::infer::error_reporting::TypeAnnotationNeeded as ErrorCode;
+use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCode};
 use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
 use crate::infer::{self, InferCtxt};
 use crate::mir::interpret::ErrorHandled;
@@ -676,15 +676,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
                     Some(t) => Some(t),
                     None => {
                         let ty = parent_trait_ref.skip_binder().self_ty();
-                        let span = if let ty::Closure(def_id, _)
-                        | ty::Opaque(def_id, _)
-                        | ty::Generator(def_id, ..)
-                        | ty::Foreign(def_id) = ty.kind
-                        {
-                            Some(self.tcx.def_span(def_id))
-                        } else {
-                            None
-                        };
+                        let span =
+                            TyCategory::from_ty(ty).map(|(_, def_id)| self.tcx.def_span(def_id));
                         Some((ty.to_string(), span))
                     }
                 }