Skip to content

Commit 83ffda5

Browse files
committed
Specific labels when referring to "expected" and "found" types
1 parent a0d40f8 commit 83ffda5

File tree

406 files changed

+1602
-1522
lines changed

Some content is hidden

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

406 files changed

+1602
-1522
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,18 +1197,25 @@ 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());
12001204
match (terr, is_simple_error, expected == found) {
1201-
(&TypeError::Sorts(ref values), false, true) => {
1202-
let sort_string = | a_type: Ty<'tcx> |
1203-
if let ty::Opaque(def_id, _) = a_type.kind {
1204-
format!(" (opaque type at {})", self.tcx.sess.source_map()
1205-
.mk_substr_filename(self.tcx.def_span(def_id)))
1206-
} else {
1207-
format!(" ({})", a_type.sort_string(self.tcx))
1208-
};
1205+
(&TypeError::Sorts(ref values), false, extra) => {
1206+
let sort_string = |ty: Ty<'tcx>| match (extra, &ty.kind) {
1207+
(true, ty::Opaque(def_id, _)) => format!(
1208+
" (opaque type at {})",
1209+
self.tcx.sess.source_map()
1210+
.mk_substr_filename(self.tcx.def_span(*def_id)),
1211+
),
1212+
(true, _) => format!(" ({})", ty.sort_string(self.tcx)),
1213+
(false, _) => "".to_string(),
1214+
};
12091215
diag.note_expected_found_extra(
1210-
&"type",
1216+
&expected_label,
12111217
expected,
1218+
&found_label,
12121219
found,
12131220
&sort_string(values.expected),
12141221
&sort_string(values.found),
@@ -1222,15 +1229,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
12221229
"note_type_err: exp_found={:?}, expected={:?} found={:?}",
12231230
exp_found, expected, found
12241231
);
1225-
if let Some(exp_found) = exp_found {
1226-
self.suggest_as_ref_where_appropriate(span, &exp_found, diag);
1227-
}
1228-
1229-
diag.note_expected_found(&"type", expected, found);
1232+
diag.note_expected_found(&expected_label, expected, &found_label, found);
12301233
}
12311234
_ => (),
12321235
}
12331236
}
1237+
if let Some(exp_found) = exp_found {
1238+
self.suggest_as_ref_where_appropriate(span, &exp_found, diag);
1239+
}
12341240

12351241
// In some (most?) cases cause.body_id points to actual body, but in some cases
12361242
// it's a actual definition. According to the comments (e.g. in

src/librustc/ty/error.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,37 @@ impl<'tcx> ty::TyS<'tcx> {
246246
ty::Error => "type error".into(),
247247
}
248248
}
249+
250+
pub fn prefix_string(&self) -> Cow<'static, str> {
251+
debug!("prefix_string {:?} {} {:?}", self, self, self.kind);
252+
match self.kind {
253+
ty::Infer(_) | ty::Error | ty::Bool | ty::Char | ty::Int(_) |
254+
ty::Uint(_) | ty::Float(_) | ty::Str | ty::Never => "type".into(),
255+
ty::Tuple(ref tys) if tys.is_empty() => "type".into(),
256+
ty::Adt(def, _) => def.descr().into(),
257+
ty::Foreign(_) => "extern type".into(),
258+
ty::Array(..) => "array".into(),
259+
ty::Slice(_) => "slice".into(),
260+
ty::RawPtr(_) => "raw pointer".into(),
261+
ty::Ref(.., mutbl) => match mutbl {
262+
hir::Mutability::Mutable => "mutable reference",
263+
_ => "reference"
264+
}.into(),
265+
ty::FnDef(..) => "fn item".into(),
266+
ty::FnPtr(_) => "fn pointer".into(),
267+
ty::Dynamic(..) => "trait".into(),
268+
ty::Closure(..) => "closure".into(),
269+
ty::Generator(..) => "generator".into(),
270+
ty::GeneratorWitness(..) => "generator witness".into(),
271+
ty::Tuple(..) => "tuple".into(),
272+
ty::Placeholder(..) => "placeholder type".into(),
273+
ty::Bound(..) => "bound type".into(),
274+
ty::Projection(_) => "associated type".into(),
275+
ty::UnnormalizedProjection(_) => "associated type".into(),
276+
ty::Param(_) => "type parameter".into(),
277+
ty::Opaque(..) => "opaque type".into(),
278+
}
279+
}
249280
}
250281

251282
impl<'tcx> TyCtxt<'tcx> {

src/librustc_errors/diagnostic.rs

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,21 @@ impl Diagnostic {
143143
self
144144
}
145145

146-
pub fn note_expected_found(&mut self,
147-
label: &dyn fmt::Display,
148-
expected: DiagnosticStyledString,
149-
found: DiagnosticStyledString)
150-
-> &mut Self
151-
{
152-
self.note_expected_found_extra(label, expected, found, &"", &"")
153-
}
154-
155-
pub fn note_unsuccessfull_coercion(&mut self,
156-
expected: DiagnosticStyledString,
157-
found: DiagnosticStyledString)
158-
-> &mut Self
159-
{
146+
pub fn note_expected_found(
147+
&mut self,
148+
expected_label: &dyn fmt::Display,
149+
expected: DiagnosticStyledString,
150+
found_label: &dyn fmt::Display,
151+
found: DiagnosticStyledString,
152+
) -> &mut Self {
153+
self.note_expected_found_extra(expected_label, expected, found_label, found, &"", &"")
154+
}
155+
156+
pub fn note_unsuccessfull_coercion(
157+
&mut self,
158+
expected: DiagnosticStyledString,
159+
found: DiagnosticStyledString,
160+
) -> &mut Self {
160161
let mut msg: Vec<_> =
161162
vec![(format!("required when trying to coerce from type `"),
162163
Style::NoStyle)];
@@ -178,27 +179,38 @@ impl Diagnostic {
178179
self
179180
}
180181

181-
pub fn note_expected_found_extra(&mut self,
182-
label: &dyn fmt::Display,
183-
expected: DiagnosticStyledString,
184-
found: DiagnosticStyledString,
185-
expected_extra: &dyn fmt::Display,
186-
found_extra: &dyn fmt::Display)
187-
-> &mut Self
188-
{
189-
let mut msg: Vec<_> = vec![(format!("expected {} `", label), Style::NoStyle)];
182+
pub fn note_expected_found_extra(
183+
&mut self,
184+
expected_label: &dyn fmt::Display,
185+
expected: DiagnosticStyledString,
186+
found_label: &dyn fmt::Display,
187+
found: DiagnosticStyledString,
188+
expected_extra: &dyn fmt::Display,
189+
found_extra: &dyn fmt::Display,
190+
) -> &mut Self {
191+
let expected_label = format!("expected {}", expected_label);
192+
let found_label = format!("found {}", found_label);
193+
let (found_padding, expected_padding) = if expected_label.len() > found_label.len() {
194+
(expected_label.len() - found_label.len(), 0)
195+
} else {
196+
(0, found_label.len() - expected_label.len())
197+
};
198+
let mut msg: Vec<_> = vec![(
199+
format!("{}{} `", " ".repeat(expected_padding), expected_label),
200+
Style::NoStyle,
201+
)];
190202
msg.extend(expected.0.iter()
191-
.map(|x| match *x {
192-
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
193-
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
194-
}));
203+
.map(|x| match *x {
204+
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
205+
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
206+
}));
195207
msg.push((format!("`{}\n", expected_extra), Style::NoStyle));
196-
msg.push((format!(" found {} `", label), Style::NoStyle));
208+
msg.push((format!("{}{} `", " ".repeat(found_padding), found_label), Style::NoStyle));
197209
msg.extend(found.0.iter()
198-
.map(|x| match *x {
199-
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
200-
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
201-
}));
210+
.map(|x| match *x {
211+
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
212+
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
213+
}));
202214
msg.push((format!("`{}", found_extra), Style::NoStyle));
203215

204216
// For now, just attach these as notes

src/librustc_errors/diagnostic_builder.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -195,37 +195,44 @@ impl<'a> DiagnosticBuilder<'a> {
195195
self
196196
}
197197

198-
forward!(pub fn note_expected_found(&mut self,
199-
label: &dyn fmt::Display,
200-
expected: DiagnosticStyledString,
201-
found: DiagnosticStyledString,
202-
) -> &mut Self);
203-
204-
forward!(pub fn note_expected_found_extra(&mut self,
205-
label: &dyn fmt::Display,
206-
expected: DiagnosticStyledString,
207-
found: DiagnosticStyledString,
208-
expected_extra: &dyn fmt::Display,
209-
found_extra: &dyn fmt::Display,
210-
) -> &mut Self);
211-
212-
forward!(pub fn note_unsuccessfull_coercion(&mut self,
213-
expected: DiagnosticStyledString,
214-
found: DiagnosticStyledString,
215-
) -> &mut Self);
198+
forward!(pub fn note_expected_found(
199+
&mut self,
200+
expected_label: &dyn fmt::Display,
201+
expected: DiagnosticStyledString,
202+
found_label: &dyn fmt::Display,
203+
found: DiagnosticStyledString,
204+
) -> &mut Self);
205+
206+
forward!(pub fn note_expected_found_extra(
207+
&mut self,
208+
expected_label: &dyn fmt::Display,
209+
expected: DiagnosticStyledString,
210+
found_label: &dyn fmt::Display,
211+
found: DiagnosticStyledString,
212+
expected_extra: &dyn fmt::Display,
213+
found_extra: &dyn fmt::Display,
214+
) -> &mut Self);
215+
216+
forward!(pub fn note_unsuccessfull_coercion(
217+
&mut self,
218+
expected: DiagnosticStyledString,
219+
found: DiagnosticStyledString,
220+
) -> &mut Self);
216221

217222
forward!(pub fn note(&mut self, msg: &str) -> &mut Self);
218-
forward!(pub fn span_note<S: Into<MultiSpan>>(&mut self,
219-
sp: S,
220-
msg: &str,
221-
) -> &mut Self);
223+
forward!(pub fn span_note<S: Into<MultiSpan>>(
224+
&mut self,
225+
sp: S,
226+
msg: &str,
227+
) -> &mut Self);
222228
forward!(pub fn warn(&mut self, msg: &str) -> &mut Self);
223229
forward!(pub fn span_warn<S: Into<MultiSpan>>(&mut self, sp: S, msg: &str) -> &mut Self);
224230
forward!(pub fn help(&mut self, msg: &str) -> &mut Self);
225-
forward!(pub fn span_help<S: Into<MultiSpan>>(&mut self,
226-
sp: S,
227-
msg: &str,
228-
) -> &mut Self);
231+
forward!(pub fn span_help<S: Into<MultiSpan>>(
232+
&mut self,
233+
sp: S,
234+
msg: &str,
235+
) -> &mut Self);
229236

230237
pub fn multipart_suggestion(
231238
&mut self,

src/test/ui/array-break-length.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LL | |_: [_; break]| {}
1717
| ^^^^^^^^^^^^^^^^^^ expected (), found closure
1818
|
1919
= note: expected type `()`
20-
found type `[closure@$DIR/array-break-length.rs:3:9: 3:27]`
20+
found closure `[closure@$DIR/array-break-length.rs:3:9: 3:27]`
2121

2222
error[E0308]: mismatched types
2323
--> $DIR/array-break-length.rs:8:9
@@ -26,7 +26,7 @@ LL | |_: [_; continue]| {}
2626
| ^^^^^^^^^^^^^^^^^^^^^ expected (), found closure
2727
|
2828
= note: expected type `()`
29-
found type `[closure@$DIR/array-break-length.rs:8:9: 8:30]`
29+
found closure `[closure@$DIR/array-break-length.rs:8:9: 8:30]`
3030

3131
error: aborting due to 4 previous errors
3232

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

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

88
let x: &[i32] = &[1, 2, 3];
99
let _y: &i32 = x;
1010
//~^ ERROR mismatched types
11-
//~| expected type `&i32`
12-
//~| found type `&[i32]`
11+
//~| expected reference `&i32`
12+
//~| found reference `&[i32]`
1313
//~| expected i32, found slice
1414
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ LL | let _x: i32 = [1, 2, 3];
55
| ^^^^^^^^^ expected i32, found array of 3 elements
66
|
77
= note: expected type `i32`
8-
found type `[{integer}; 3]`
8+
found array `[{integer}; 3]`
99

1010
error[E0308]: mismatched types
1111
--> $DIR/array-not-vector.rs:9:20
1212
|
1313
LL | let _y: &i32 = x;
1414
| ^ expected i32, found slice
1515
|
16-
= note: expected type `&i32`
17-
found type `&[i32]`
16+
= note: expected reference `&i32`
17+
found reference `&[i32]`
1818

1919
error: aborting due to 2 previous errors
2020

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ LL | const FROM: Self::Out;
77
LL | const FROM: &'static str = "foo";
88
| ^^^^^^^^^^^^ expected associated type, found reference
99
|
10-
= note: expected type `<T as Foo>::Out`
11-
found type `&'static str`
10+
= note: expected associated type `<T as Foo>::Out`
11+
found reference `&'static str`
1212
= note: consider constraining the associated type `<T as Foo>::Out` to `&'static str` or calling a method that returns `<T as Foo>::Out`
1313
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
1414

src/test/ui/associated-const/associated-const-impl-wrong-lifetime.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0308]: mismatched types
44
LL | const NAME: &'a str = "unit";
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
66
|
7-
= note: expected type `&'static str`
8-
found type `&'a str`
7+
= note: expected reference `&'static str`
8+
found reference `&'a str`
99
note: the lifetime `'a` as defined on the impl at 6:6...
1010
--> $DIR/associated-const-impl-wrong-lifetime.rs:6:6
1111
|

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,35 @@ error[E0308]: mismatched types
44
LL | fn b() { dent(ModelT, Blue); }
55
| ^^^^ expected struct `Black`, found struct `Blue`
66
|
7-
= note: expected type `Black`
8-
found type `Blue`
7+
= note: expected struct `Black`
8+
found struct `Blue`
99

1010
error[E0308]: mismatched types
1111
--> $DIR/associated-type-projection-from-supertrait.rs:28:23
1212
|
1313
LL | fn c() { dent(ModelU, Black); }
1414
| ^^^^^ expected struct `Blue`, found struct `Black`
1515
|
16-
= note: expected type `Blue`
17-
found type `Black`
16+
= note: expected struct `Blue`
17+
found struct `Black`
1818

1919
error[E0308]: mismatched types
2020
--> $DIR/associated-type-projection-from-supertrait.rs:32:28
2121
|
2222
LL | fn f() { ModelT.chip_paint(Blue); }
2323
| ^^^^ expected struct `Black`, found struct `Blue`
2424
|
25-
= note: expected type `Black`
26-
found type `Blue`
25+
= note: expected struct `Black`
26+
found struct `Blue`
2727

2828
error[E0308]: mismatched types
2929
--> $DIR/associated-type-projection-from-supertrait.rs:33:28
3030
|
3131
LL | fn g() { ModelU.chip_paint(Black); }
3232
| ^^^^^ expected struct `Blue`, found struct `Black`
3333
|
34-
= note: expected type `Blue`
35-
found type `Black`
34+
= note: expected struct `Blue`
35+
found struct `Black`
3636

3737
error: aborting due to 4 previous errors
3838

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ LL | fn blue_car<C:Car<Color=Blue>>(c: C) {
77
LL | fn b() { blue_car(ModelT); }
88
| ^^^^^^^^ expected struct `Blue`, found struct `Black`
99
|
10-
= note: expected type `Blue`
11-
found type `Black`
10+
= note: expected struct `Blue`
11+
found struct `Black`
1212

1313
error[E0271]: type mismatch resolving `<ModelU as Vehicle>::Color == Black`
1414
--> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:32:10
@@ -19,8 +19,8 @@ LL | fn black_car<C:Car<Color=Black>>(c: C) {
1919
LL | fn c() { black_car(ModelU); }
2020
| ^^^^^^^^^ expected struct `Black`, found struct `Blue`
2121
|
22-
= note: expected type `Black`
23-
found type `Blue`
22+
= note: expected struct `Black`
23+
found struct `Blue`
2424

2525
error: aborting due to 2 previous errors
2626

0 commit comments

Comments
 (0)