Skip to content

Commit b3517cd

Browse files
committed
review comments
1 parent 6f8f706 commit b3517cd

File tree

3 files changed

+60
-48
lines changed

3 files changed

+60
-48
lines changed

src/librustc/infer/error_reporting/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1197,10 +1197,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
11971197
};
11981198

11991199
if let Some((expected, found)) = expected_found {
1200-
let expected_label = exp_found.map(|ef| ef.expected.prefix_string())
1201-
.unwrap_or("type".into());
1202-
let found_label = exp_found.map(|ef| ef.found.prefix_string())
1203-
.unwrap_or("type".into());
1200+
let expected_label = exp_found.map_or("type".into(), |ef| ef.expected.prefix_string());
1201+
let found_label = exp_found.map_or("type".into(), |ef| ef.found.prefix_string());
12041202
match (&terr, expected == found) {
12051203
(TypeError::Sorts(values), extra) => {
12061204
let sort_string = |ty: Ty<'tcx>| match (extra, &ty.kind) {

src/librustc/ty/diagnostics.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 refered 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 refered 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+
}

src/librustc/ty/mod.rs

+2-44
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub use self::sty::BoundRegion::*;
7171
pub use self::sty::InferTy::*;
7272
pub use self::sty::RegionKind::*;
7373
pub use self::sty::TyKind::*;
74+
pub use crate::ty::diagnostics::*;
7475

7576
pub use self::binding::BindingMode;
7677
pub use self::binding::BindingMode::*;
@@ -122,6 +123,7 @@ mod instance;
122123
mod structural_impls;
123124
mod structural_match;
124125
mod sty;
126+
mod diagnostics;
125127

126128
// Data types
127129

@@ -552,50 +554,6 @@ impl<'tcx> Hash for TyS<'tcx> {
552554
}
553555
}
554556

555-
impl<'tcx> TyS<'tcx> {
556-
pub fn is_primitive_ty(&self) -> bool {
557-
match self.kind {
558-
Bool | Char | Str | Int(_) | Uint(_) | Float(_) |
559-
Infer(InferTy::IntVar(_)) | Infer(InferTy::FloatVar(_)) |
560-
Infer(InferTy::FreshIntTy(_)) | Infer(InferTy::FreshFloatTy(_)) => true,
561-
_ => false,
562-
}
563-
}
564-
565-
pub fn is_simple_ty(&self) -> bool {
566-
match self.kind {
567-
Bool | Char | Str | Int(_) | Uint(_) | Float(_) |
568-
Infer(InferTy::IntVar(_)) | Infer(InferTy::FloatVar(_)) |
569-
Infer(InferTy::FreshIntTy(_)) | Infer(InferTy::FreshFloatTy(_)) => true,
570-
Ref(_, x, _) | Array(x, _) | Slice(x) => x.peel_refs().is_simple_ty(),
571-
Tuple(tys) if tys.is_empty() => true,
572-
_ => false,
573-
}
574-
}
575-
576-
pub fn is_simple_text(&self) -> bool {
577-
match self.kind {
578-
Adt(_, substs) => substs.types().next().is_none(),
579-
Ref(_, ty, _) => ty.is_simple_text(),
580-
_ if self.is_simple_ty() => true,
581-
_ => false,
582-
}
583-
}
584-
585-
pub fn is_suggestable(&self) -> bool {
586-
match self.kind {
587-
Opaque(..) |
588-
FnDef(..) |
589-
FnPtr(..) |
590-
Dynamic(..) |
591-
Closure(..) |
592-
Infer(..) |
593-
Projection(..) => false,
594-
_ => true,
595-
}
596-
}
597-
}
598-
599557
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ty::TyS<'tcx> {
600558
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
601559
let ty::TyS {

0 commit comments

Comments
 (0)