]>
Commit | Line | Data |
---|---|---|
60c5eb7d XL |
1 | //! Diagnostics related methods for `TyS`. |
2 | ||
60c5eb7d | 3 | use crate::ty::sty::InferTy; |
dfeec247 XL |
4 | use crate::ty::TyKind::*; |
5 | use crate::ty::TyS; | |
60c5eb7d XL |
6 | |
7 | impl<'tcx> TyS<'tcx> { | |
8 | /// Similar to `TyS::is_primitive`, but also considers inferred numeric values to be primitive. | |
9 | pub fn is_primitive_ty(&self) -> bool { | |
10 | match self.kind { | |
dfeec247 XL |
11 | Bool |
12 | | Char | |
13 | | Str | |
14 | | Int(_) | |
15 | | Uint(_) | |
16 | | Float(_) | |
17 | | Infer(InferTy::IntVar(_)) | |
18 | | Infer(InferTy::FloatVar(_)) | |
19 | | Infer(InferTy::FreshIntTy(_)) | |
20 | | Infer(InferTy::FreshFloatTy(_)) => true, | |
60c5eb7d XL |
21 | _ => false, |
22 | } | |
23 | } | |
24 | ||
25 | /// Whether the type is succinctly representable as a type instead of just referred to with a | |
26 | /// description in error messages. This is used in the main error message. | |
27 | pub fn is_simple_ty(&self) -> bool { | |
28 | match self.kind { | |
dfeec247 XL |
29 | Bool |
30 | | Char | |
31 | | Str | |
32 | | Int(_) | |
33 | | Uint(_) | |
34 | | Float(_) | |
35 | | Infer(InferTy::IntVar(_)) | |
36 | | Infer(InferTy::FloatVar(_)) | |
37 | | Infer(InferTy::FreshIntTy(_)) | |
38 | | Infer(InferTy::FreshFloatTy(_)) => true, | |
60c5eb7d XL |
39 | Ref(_, x, _) | Array(x, _) | Slice(x) => x.peel_refs().is_simple_ty(), |
40 | Tuple(tys) if tys.is_empty() => true, | |
41 | _ => false, | |
42 | } | |
43 | } | |
44 | ||
45 | /// Whether the type is succinctly representable as a type instead of just referred to with a | |
46 | /// description in error messages. This is used in the primary span label. Beyond what | |
47 | /// `is_simple_ty` includes, it also accepts ADTs with no type arguments and references to | |
48 | /// ADTs with no type arguments. | |
49 | pub fn is_simple_text(&self) -> bool { | |
50 | match self.kind { | |
51 | Adt(_, substs) => substs.types().next().is_none(), | |
52 | Ref(_, ty, _) => ty.is_simple_text(), | |
53 | _ => self.is_simple_ty(), | |
54 | } | |
55 | } | |
56 | ||
57 | /// Whether the type can be safely suggested during error recovery. | |
58 | pub fn is_suggestable(&self) -> bool { | |
59 | match self.kind { | |
dfeec247 XL |
60 | Opaque(..) | FnDef(..) | FnPtr(..) | Dynamic(..) | Closure(..) | Infer(..) |
61 | | Projection(..) => false, | |
60c5eb7d XL |
62 | _ => true, |
63 | } | |
64 | } | |
65 | } |