]> git.proxmox.com Git - rustc.git/blob - src/librustc/ty/diagnostics.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / librustc / ty / diagnostics.rs
1 //! Diagnostics related methods for `TyS`.
2
3 use crate::ty::TyS;
4 use crate::ty::TyKind::*;
5 use crate::ty::sty::InferTy;
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 {
11 Bool | Char | Str | Int(_) | Uint(_) | Float(_) |
12 Infer(InferTy::IntVar(_)) | Infer(InferTy::FloatVar(_)) |
13 Infer(InferTy::FreshIntTy(_)) | Infer(InferTy::FreshFloatTy(_)) => true,
14 _ => false,
15 }
16 }
17
18 /// Whether the type is succinctly representable as a type instead of just referred to with a
19 /// description in error messages. This is used in the main error message.
20 pub fn is_simple_ty(&self) -> bool {
21 match self.kind {
22 Bool | Char | Str | Int(_) | Uint(_) | Float(_) |
23 Infer(InferTy::IntVar(_)) | Infer(InferTy::FloatVar(_)) |
24 Infer(InferTy::FreshIntTy(_)) | Infer(InferTy::FreshFloatTy(_)) => true,
25 Ref(_, x, _) | Array(x, _) | Slice(x) => x.peel_refs().is_simple_ty(),
26 Tuple(tys) if tys.is_empty() => true,
27 _ => false,
28 }
29 }
30
31 /// Whether the type is succinctly representable as a type instead of just referred to with a
32 /// description in error messages. This is used in the primary span label. Beyond what
33 /// `is_simple_ty` includes, it also accepts ADTs with no type arguments and references to
34 /// ADTs with no type arguments.
35 pub fn is_simple_text(&self) -> bool {
36 match self.kind {
37 Adt(_, substs) => substs.types().next().is_none(),
38 Ref(_, ty, _) => ty.is_simple_text(),
39 _ => self.is_simple_ty(),
40 }
41 }
42
43 /// Whether the type can be safely suggested during error recovery.
44 pub fn is_suggestable(&self) -> bool {
45 match self.kind {
46 Opaque(..) |
47 FnDef(..) |
48 FnPtr(..) |
49 Dynamic(..) |
50 Closure(..) |
51 Infer(..) |
52 Projection(..) => false,
53 _ => true,
54 }
55 }
56 }