Skip to content

Commit 94c6425

Browse files
committed
Remove E0308 note when primary label has all info
1 parent b2e6aef commit 94c6425

File tree

237 files changed

+225
-1278
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

237 files changed

+225
-1278
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,8 +1163,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
11631163
Some(values) => {
11641164
let (is_simple_error, exp_found) = match values {
11651165
ValuePairs::Types(exp_found) => {
1166-
let is_simple_err =
1167-
exp_found.expected.is_primitive() && exp_found.found.is_primitive();
1166+
let is_simple_err = exp_found.expected.is_simple_text()
1167+
&& exp_found.found.is_simple_text();
11681168

11691169
(is_simple_err, Some(exp_found))
11701170
}
@@ -1201,8 +1201,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
12011201
.unwrap_or("type".into());
12021202
let found_label = exp_found.map(|ef| ef.found.prefix_string())
12031203
.unwrap_or("type".into());
1204-
match (terr, is_simple_error, expected == found) {
1205-
(&TypeError::Sorts(ref values), false, extra) => {
1204+
match (&terr, expected == found) {
1205+
(TypeError::Sorts(values), extra) => {
12061206
let sort_string = |ty: Ty<'tcx>| match (extra, &ty.kind) {
12071207
(true, ty::Opaque(def_id, _)) => format!(
12081208
" (opaque type at {})",
@@ -1212,26 +1212,43 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
12121212
(true, _) => format!(" ({})", ty.sort_string(self.tcx)),
12131213
(false, _) => "".to_string(),
12141214
};
1215-
diag.note_expected_found_extra(
1216-
&expected_label,
1217-
expected,
1218-
&found_label,
1219-
found,
1220-
&sort_string(values.expected),
1221-
&sort_string(values.found),
1222-
);
1215+
if !(values.expected.is_simple_text() && values.found.is_simple_text()) || (
1216+
exp_found.map_or(false, |ef| {
1217+
// This happens when the type error is a subset of the expectation,
1218+
// like when you have two references but one is `usize` and the other
1219+
// is `f32`. In those cases we still want to show the `note`. If the
1220+
// value from `ef` is `Infer(_)`, then we ignore it.
1221+
if !ef.expected.is_ty_infer() {
1222+
ef.expected != values.expected
1223+
} else if !ef.found.is_ty_infer() {
1224+
ef.found != values.found
1225+
} else {
1226+
false
1227+
}
1228+
})
1229+
) {
1230+
diag.note_expected_found_extra(
1231+
&expected_label,
1232+
expected,
1233+
&found_label,
1234+
found,
1235+
&sort_string(values.expected),
1236+
&sort_string(values.found),
1237+
);
1238+
}
12231239
}
1224-
(TypeError::ObjectUnsafeCoercion(_), ..) => {
1240+
(TypeError::ObjectUnsafeCoercion(_), _) => {
12251241
diag.note_unsuccessfull_coercion(found, expected);
12261242
}
1227-
(_, false, _) => {
1243+
(_, _) => {
12281244
debug!(
12291245
"note_type_err: exp_found={:?}, expected={:?} found={:?}",
12301246
exp_found, expected, found
12311247
);
1232-
diag.note_expected_found(&expected_label, expected, &found_label, found);
1248+
if !is_simple_error || terr.must_include_note() {
1249+
diag.note_expected_found(&expected_label, expected, &found_label, found);
1250+
}
12331251
}
1234-
_ => (),
12351252
}
12361253
}
12371254
if let Some(exp_found) = exp_found {

src/librustc/ty/error.rs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ pub enum UnconstrainedNumeric {
6464
impl<'tcx> fmt::Display for TypeError<'tcx> {
6565
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6666
use self::TypeError::*;
67-
fn report_maybe_different(f: &mut fmt::Formatter<'_>,
68-
expected: &str, found: &str) -> fmt::Result {
67+
fn report_maybe_different(
68+
f: &mut fmt::Formatter<'_>,
69+
expected: &str,
70+
found: &str,
71+
) -> fmt::Result {
6972
// A naive approach to making sure that we're not reporting silly errors such as:
7073
// (expected closure, found closure).
7174
if expected == found {
@@ -183,39 +186,70 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
183186
}
184187
}
185188

189+
impl<'tcx> TypeError<'tcx> {
190+
pub fn must_include_note(&self) -> bool {
191+
use self::TypeError::*;
192+
match self {
193+
CyclicTy(_) |
194+
UnsafetyMismatch(_) |
195+
Mismatch |
196+
AbiMismatch(_) |
197+
FixedArraySize(_) |
198+
Sorts(_) |
199+
IntMismatch(_) |
200+
FloatMismatch(_) |
201+
VariadicMismatch(_) => false,
202+
203+
Mutability |
204+
TupleSize(_) |
205+
ArgCount |
206+
RegionsDoesNotOutlive(..) |
207+
RegionsInsufficientlyPolymorphic(..) |
208+
RegionsOverlyPolymorphic(..) |
209+
RegionsPlaceholderMismatch |
210+
Traits(_) |
211+
ProjectionMismatched(_) |
212+
ProjectionBoundsLength(_) |
213+
ExistentialMismatch(_) |
214+
ConstMismatch(_) |
215+
IntrinsicCast |
216+
ObjectUnsafeCoercion(_) => true,
217+
}
218+
}
219+
}
220+
186221
impl<'tcx> ty::TyS<'tcx> {
187222
pub fn sort_string(&self, tcx: TyCtxt<'_>) -> Cow<'static, str> {
188223
match self.kind {
189224
ty::Bool | ty::Char | ty::Int(_) |
190-
ty::Uint(_) | ty::Float(_) | ty::Str | ty::Never => self.to_string().into(),
191-
ty::Tuple(ref tys) if tys.is_empty() => self.to_string().into(),
225+
ty::Uint(_) | ty::Float(_) | ty::Str | ty::Never => format!("{}", self).into(),
226+
ty::Tuple(ref tys) if tys.is_empty() => format!("{}", self).into(),
192227

193228
ty::Adt(def, _) => format!("{} `{}`", def.descr(), tcx.def_path_str(def.did)).into(),
194229
ty::Foreign(def_id) => format!("extern type `{}`", tcx.def_path_str(def_id)).into(),
195-
ty::Array(_, n) => {
230+
ty::Array(t, n) => {
196231
let n = tcx.lift(&n).unwrap();
197232
match n.try_eval_usize(tcx, ty::ParamEnv::empty()) {
198-
Some(n) => {
199-
format!("array of {} element{}", n, pluralize!(n)).into()
200-
}
233+
_ if t.is_simple_ty() => format!("array `{}`", self).into(),
234+
Some(n) => format!("array of {} element{} ", n, pluralize!(n)).into(),
201235
None => "array".into(),
202236
}
203237
}
238+
ty::Slice(ty) if ty.is_simple_ty() => format!("slice `{}`", self).into(),
204239
ty::Slice(_) => "slice".into(),
205240
ty::RawPtr(_) => "*-ptr".into(),
206-
ty::Ref(region, ty, mutbl) => {
241+
ty::Ref(_, ty, mutbl) => {
207242
let tymut = ty::TypeAndMut { ty, mutbl };
208243
let tymut_string = tymut.to_string();
209-
if tymut_string == "_" || //unknown type name,
210-
tymut_string.len() > 10 || //name longer than saying "reference",
211-
region.to_string() != "'_" //... or a complex type
212-
{
213-
format!("{}reference", match mutbl {
214-
hir::Mutability::Mutable => "mutable ",
215-
_ => ""
216-
}).into()
217-
} else {
244+
if tymut_string != "_" && (
245+
ty.is_simple_text() || tymut_string.len() < "mutable reference".len()
246+
) {
218247
format!("&{}", tymut_string).into()
248+
} else { // Unknown type name, it's long or has type arguments
249+
match mutbl {
250+
hir::Mutability::Mutable => "mutable reference",
251+
_ => "reference",
252+
}.into()
219253
}
220254
}
221255
ty::FnDef(..) => "fn item".into(),
@@ -248,7 +282,6 @@ impl<'tcx> ty::TyS<'tcx> {
248282
}
249283

250284
pub fn prefix_string(&self) -> Cow<'static, str> {
251-
debug!("prefix_string {:?} {} {:?}", self, self, self.kind);
252285
match self.kind {
253286
ty::Infer(_) | ty::Error | ty::Bool | ty::Char | ty::Int(_) |
254287
ty::Uint(_) | ty::Float(_) | ty::Str | ty::Never => "type".into(),

src/librustc/ty/mod.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -555,16 +555,29 @@ impl<'tcx> Hash for TyS<'tcx> {
555555
impl<'tcx> TyS<'tcx> {
556556
pub fn is_primitive_ty(&self) -> bool {
557557
match self.kind {
558-
Bool |
559-
Char |
560-
Int(_) |
561-
Uint(_) |
562-
Float(_) |
563-
Infer(InferTy::IntVar(_)) |
564-
Infer(InferTy::FloatVar(_)) |
565-
Infer(InferTy::FreshIntTy(_)) |
566-
Infer(InferTy::FreshFloatTy(_)) => true,
567-
Ref(_, x, _) => x.is_primitive_ty(),
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,
568581
_ => false,
569582
}
570583
}

src/test/rustdoc-ui/failed-doctest-missing-codes.stdout

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ error[E0308]: mismatched types
1010
|
1111
LL | let x: () = 5i32;
1212
| ^^^^ expected (), found i32
13-
|
14-
= note: expected type `()`
15-
found type `i32`
1613

1714
error: aborting due to previous error
1815

src/test/ui/arg-type-mismatch.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ error[E0308]: mismatched types
33
|
44
LL | fn main() { let i: (); i = f(()); }
55
| ^^ expected isize, found ()
6-
|
7-
= note: expected type `isize`
8-
found unit type `()`
96

107
error: aborting due to previous error
118

src/test/ui/array-not-vector.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
fn main() {
22
let _x: i32 = [1, 2, 3];
33
//~^ ERROR mismatched types
4-
//~| expected type `i32`
5-
//~| found array `[{integer}; 3]`
6-
//~| expected i32, found array of 3 elements
4+
//~| expected i32, found array
75

86
let x: &[i32] = &[1, 2, 3];
97
let _y: &i32 = x;

src/test/ui/array-not-vector.stderr

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ error[E0308]: mismatched types
22
--> $DIR/array-not-vector.rs:2:19
33
|
44
LL | let _x: i32 = [1, 2, 3];
5-
| ^^^^^^^^^ expected i32, found array of 3 elements
6-
|
7-
= note: expected type `i32`
8-
found array `[{integer}; 3]`
5+
| ^^^^^^^^^ expected i32, found array `[{integer}; 3]`
96

107
error[E0308]: mismatched types
11-
--> $DIR/array-not-vector.rs:9:20
8+
--> $DIR/array-not-vector.rs:7:20
129
|
1310
LL | let _y: &i32 = x;
14-
| ^ expected i32, found slice
11+
| ^ expected i32, found slice `[i32]`
1512
|
1613
= note: expected reference `&i32`
1714
found reference `&[i32]`

src/test/ui/associated-const/associated-const-generic-obligations.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | const FROM: Self::Out;
55
| --------- type in trait
66
...
77
LL | const FROM: &'static str = "foo";
8-
| ^^^^^^^^^^^^ expected associated type, found reference
8+
| ^^^^^^^^^^^^ expected associated type, found &str
99
|
1010
= note: expected associated type `<T as Foo>::Out`
1111
found reference `&'static str`

src/test/ui/associated-type/associated-type-projection-from-supertrait.stderr

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,24 @@ error[E0308]: mismatched types
33
|
44
LL | fn b() { dent(ModelT, Blue); }
55
| ^^^^ expected struct `Black`, found struct `Blue`
6-
|
7-
= note: expected struct `Black`
8-
found struct `Blue`
96

107
error[E0308]: mismatched types
118
--> $DIR/associated-type-projection-from-supertrait.rs:28:23
129
|
1310
LL | fn c() { dent(ModelU, Black); }
1411
| ^^^^^ expected struct `Blue`, found struct `Black`
15-
|
16-
= note: expected struct `Blue`
17-
found struct `Black`
1812

1913
error[E0308]: mismatched types
2014
--> $DIR/associated-type-projection-from-supertrait.rs:32:28
2115
|
2216
LL | fn f() { ModelT.chip_paint(Blue); }
2317
| ^^^^ expected struct `Black`, found struct `Blue`
24-
|
25-
= note: expected struct `Black`
26-
found struct `Blue`
2718

2819
error[E0308]: mismatched types
2920
--> $DIR/associated-type-projection-from-supertrait.rs:33:28
3021
|
3122
LL | fn g() { ModelU.chip_paint(Black); }
3223
| ^^^^^ expected struct `Blue`, found struct `Black`
33-
|
34-
= note: expected struct `Blue`
35-
found struct `Black`
3624

3725
error: aborting due to 4 previous errors
3826

src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ LL | fn blue_car<C:Car<Color=Blue>>(c: C) {
66
...
77
LL | fn b() { blue_car(ModelT); }
88
| ^^^^^^^^ expected struct `Blue`, found struct `Black`
9-
|
10-
= note: expected struct `Blue`
11-
found struct `Black`
129

1310
error[E0271]: type mismatch resolving `<ModelU as Vehicle>::Color == Black`
1411
--> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:32:10
@@ -18,9 +15,6 @@ LL | fn black_car<C:Car<Color=Black>>(c: C) {
1815
...
1916
LL | fn c() { black_car(ModelU); }
2017
| ^^^^^^^^^ expected struct `Black`, found struct `Blue`
21-
|
22-
= note: expected struct `Black`
23-
found struct `Blue`
2418

2519
error: aborting due to 2 previous errors
2620

src/test/ui/associated-types/associated-types-eq-3.stderr

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,13 @@ LL | fn foo1<I: Foo<A=Bar>>(x: I) {
1717
...
1818
LL | foo1(a);
1919
| ^^^^ expected struct `Bar`, found usize
20-
|
21-
= note: expected struct `Bar`
22-
found type `usize`
2320

2421
error[E0271]: type mismatch resolving `<isize as Foo>::A == Bar`
2522
--> $DIR/associated-types-eq-3.rs:41:9
2623
|
2724
LL | baz(&a);
2825
| ^^ expected struct `Bar`, found usize
2926
|
30-
= note: expected struct `Bar`
31-
found type `usize`
3227
= note: required for the cast to the object type `dyn Foo<A = Bar>`
3328

3429
error: aborting due to 3 previous errors

src/test/ui/associated-types/associated-types-overridden-binding-2.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ error[E0271]: type mismatch resolving `<std::vec::IntoIter<u32> as std::iter::It
44
LL | let _: &dyn I32Iterator<Item = u32> = &vec![42].into_iter();
55
| ^^^^^^^^^^^^^^^^^^^^^ expected i32, found u32
66
|
7-
= note: expected type `i32`
8-
found type `u32`
97
= note: required for the cast to the object type `dyn std::iter::Iterator<Item = u32, Item = i32>`
108

119
error: aborting due to previous error

src/test/ui/associated-types/issue-44153.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ LL | fn visit() {}
77
LL | <() as Visit>::visit();
88
| ^^^^^^^^^^^^^^^^^^^^ expected (), found &()
99
|
10-
= note: expected unit type `()`
11-
found reference `&()`
1210
= note: required because of the requirements on the impl of `Visit` for `()`
1311

1412
error: aborting due to previous error

src/test/ui/associated-types/point-at-type-on-obligation-failure.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ LL | impl Bar for Foo {
88
| ---------------- in this `impl` item
99
LL | type Ok = ();
1010
| ^^^^^^^^^^^^^ expected u32, found ()
11-
|
12-
= note: expected type `u32`
13-
found unit type `()`
1411

1512
error: aborting due to previous error
1613

0 commit comments

Comments
 (0)