]> git.lizzy.rs Git - rust.git/commitdiff
review comments
authorEsteban Küber <esteban@kuber.com.ar>
Mon, 18 Nov 2019 18:33:05 +0000 (10:33 -0800)
committerEsteban Küber <esteban@kuber.com.ar>
Mon, 18 Nov 2019 19:03:04 +0000 (11:03 -0800)
src/librustc/infer/error_reporting/mod.rs
src/librustc/ty/diagnostics.rs [new file with mode: 0644]
src/librustc/ty/mod.rs

index cccdcc68a579328b0528c7a5b150ac7b69b75db2..f2607b23527a1d2d743eadde441badf0ee8720ff 100644 (file)
@@ -1197,10 +1197,8 @@ pub fn note_type_err(
         };
 
         if let Some((expected, found)) = expected_found {
-            let expected_label = exp_found.map(|ef| ef.expected.prefix_string())
-                .unwrap_or("type".into());
-            let found_label = exp_found.map(|ef| ef.found.prefix_string())
-                .unwrap_or("type".into());
+            let expected_label = exp_found.map_or("type".into(), |ef| ef.expected.prefix_string());
+            let found_label = exp_found.map_or("type".into(), |ef| ef.found.prefix_string());
             match (&terr, expected == found) {
                 (TypeError::Sorts(values), extra) => {
                     let sort_string = |ty: Ty<'tcx>| match (extra, &ty.kind) {
diff --git a/src/librustc/ty/diagnostics.rs b/src/librustc/ty/diagnostics.rs
new file mode 100644 (file)
index 0000000..95bdce2
--- /dev/null
@@ -0,0 +1,56 @@
+//! Diagnostics related methods for `TyS`.
+
+use crate::ty::TyS;
+use crate::ty::TyKind::*;
+use crate::ty::sty::InferTy;
+
+impl<'tcx> TyS<'tcx> {
+    /// Similar to `TyS::is_primitive`, but also considers inferred numeric values to be primitive.
+    pub fn is_primitive_ty(&self) -> bool {
+        match self.kind {
+            Bool | Char | Str | Int(_) | Uint(_) | Float(_) |
+            Infer(InferTy::IntVar(_)) | Infer(InferTy::FloatVar(_)) |
+            Infer(InferTy::FreshIntTy(_)) | Infer(InferTy::FreshFloatTy(_)) => true,
+            _ => false,
+        }
+    }
+
+    /// Whether the type is succinctly representable as a type instead of just refered to with a
+    /// description in error messages. This is used in the main error message.
+    pub fn is_simple_ty(&self) -> bool {
+        match self.kind {
+            Bool | Char | Str | Int(_) | Uint(_) | Float(_) |
+            Infer(InferTy::IntVar(_)) | Infer(InferTy::FloatVar(_)) |
+            Infer(InferTy::FreshIntTy(_)) | Infer(InferTy::FreshFloatTy(_)) => true,
+            Ref(_, x, _) | Array(x, _) | Slice(x) => x.peel_refs().is_simple_ty(),
+            Tuple(tys) if tys.is_empty() => true,
+            _ => false,
+        }
+    }
+
+    /// Whether the type is succinctly representable as a type instead of just refered to with a
+    /// description in error messages. This is used in the primary span label. Beyond what
+    /// `is_simple_ty` includes, it also accepts ADTs with no type arguments and references to
+    /// ADTs with no type arguments.
+    pub fn is_simple_text(&self) -> bool {
+        match self.kind {
+            Adt(_, substs) => substs.types().next().is_none(),
+            Ref(_, ty, _) => ty.is_simple_text(),
+            _ => self.is_simple_ty(),
+        }
+    }
+
+    /// Whether the type can be safely suggested during error recovery.
+    pub fn is_suggestable(&self) -> bool {
+        match self.kind {
+            Opaque(..) |
+            FnDef(..) |
+            FnPtr(..) |
+            Dynamic(..) |
+            Closure(..) |
+            Infer(..) |
+            Projection(..) => false,
+            _ => true,
+        }
+    }
+}
index 5fc2812fd97db10d8fa1b4abaa93bc55d8b9690b..280a5a157e9345c1bfa697a030b57312f0a9454a 100644 (file)
@@ -71,6 +71,7 @@
 pub use self::sty::InferTy::*;
 pub use self::sty::RegionKind::*;
 pub use self::sty::TyKind::*;
+pub use crate::ty::diagnostics::*;
 
 pub use self::binding::BindingMode;
 pub use self::binding::BindingMode::*;
 mod structural_impls;
 mod structural_match;
 mod sty;
+mod diagnostics;
 
 // Data types
 
@@ -552,50 +554,6 @@ fn hash<H: Hasher>(&self, s: &mut H) {
     }
 }
 
-impl<'tcx> TyS<'tcx> {
-    pub fn is_primitive_ty(&self) -> bool {
-        match self.kind {
-            Bool | Char | Str | Int(_) | Uint(_) | Float(_) |
-            Infer(InferTy::IntVar(_)) | Infer(InferTy::FloatVar(_)) |
-            Infer(InferTy::FreshIntTy(_)) | Infer(InferTy::FreshFloatTy(_)) => true,
-            _ => false,
-        }
-    }
-
-    pub fn is_simple_ty(&self) -> bool {
-        match self.kind {
-            Bool | Char | Str | Int(_) | Uint(_) | Float(_) |
-            Infer(InferTy::IntVar(_)) | Infer(InferTy::FloatVar(_)) |
-            Infer(InferTy::FreshIntTy(_)) | Infer(InferTy::FreshFloatTy(_)) => true,
-            Ref(_, x, _) | Array(x, _) | Slice(x) => x.peel_refs().is_simple_ty(),
-            Tuple(tys) if tys.is_empty() => true,
-            _ => false,
-        }
-    }
-
-    pub fn is_simple_text(&self) -> bool {
-        match self.kind {
-            Adt(_, substs) => substs.types().next().is_none(),
-            Ref(_, ty, _) => ty.is_simple_text(),
-            _ if self.is_simple_ty() => true,
-            _ => false,
-        }
-    }
-
-    pub fn is_suggestable(&self) -> bool {
-        match self.kind {
-            Opaque(..) |
-            FnDef(..) |
-            FnPtr(..) |
-            Dynamic(..) |
-            Closure(..) |
-            Infer(..) |
-            Projection(..) => false,
-            _ => true,
-        }
-    }
-}
-
 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ty::TyS<'tcx> {
     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
         let ty::TyS {