Skip to content

Commit e457cbd

Browse files
committed
Use suggestions instead of notes ref mismatches
On type mismatch errors, use a suggestion when encountering minimal differences in type differences due to refs, instead of a note.
1 parent ea51b19 commit e457cbd

File tree

6 files changed

+53
-33
lines changed

6 files changed

+53
-33
lines changed

src/librustc_typeck/check/demand.rs

+19-15
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
134134
}
135135
}
136136

137-
if let Some(suggestion) = self.check_ref(expr,
138-
checked_ty,
139-
expected) {
140-
err.help(&suggestion);
137+
if let Some((msg, suggestion)) = self.check_ref(expr, checked_ty, expected) {
138+
err.span_suggestion(expr.span, msg, suggestion);
141139
} else {
142140
let mode = probe::Mode::MethodCall;
143141
let suggestions = self.probe_for_return_type(syntax_pos::DUMMY_SP,
@@ -214,15 +212,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
214212
expr: &hir::Expr,
215213
checked_ty: Ty<'tcx>,
216214
expected: Ty<'tcx>)
217-
-> Option<String> {
215+
-> Option<(&'static str, String)> {
218216
match (&expected.sty, &checked_ty.sty) {
219217
(&ty::TyRef(_, exp), &ty::TyRef(_, check)) => match (&exp.ty.sty, &check.ty.sty) {
220218
(&ty::TyStr, &ty::TyArray(arr, _)) |
221219
(&ty::TyStr, &ty::TySlice(arr)) if arr == self.tcx.types.u8 => {
222220
if let hir::ExprLit(_) = expr.node {
223221
let sp = self.sess().codemap().call_span_if_macro(expr.span);
224222
if let Ok(src) = self.tcx.sess.codemap().span_to_snippet(sp) {
225-
return Some(format!("try `{}`", &src[1..]));
223+
return Some(("consider removing the leading `b`",
224+
src[1..].to_string()));
226225
}
227226
}
228227
None
@@ -232,7 +231,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
232231
if let hir::ExprLit(_) = expr.node {
233232
let sp = self.sess().codemap().call_span_if_macro(expr.span);
234233
if let Ok(src) = self.tcx.sess.codemap().span_to_snippet(sp) {
235-
return Some(format!("try `b{}`", src));
234+
return Some(("consider adding a leading `b`",
235+
format!("b{}", src)));
236236
}
237237
}
238238
None
@@ -260,12 +260,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
260260
// Use the callsite's span if this is a macro call. #41858
261261
let sp = self.sess().codemap().call_span_if_macro(expr.span);
262262
if let Ok(src) = self.tcx.sess.codemap().span_to_snippet(sp) {
263-
return Some(format!("try with `{}{}`",
264-
match mutability.mutbl {
265-
hir::Mutability::MutMutable => "&mut ",
266-
hir::Mutability::MutImmutable => "&",
267-
},
268-
&src));
263+
return Some(match mutability.mutbl {
264+
hir::Mutability::MutMutable => {
265+
("consider using a mutable borrow", format!("&mut {}", src))
266+
}
267+
hir::Mutability::MutImmutable => {
268+
("consider using a borrow", format!("&{}", src))
269+
}
270+
});
269271
}
270272
}
271273
None
@@ -284,7 +286,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
284286
// Maybe remove `&`?
285287
hir::ExprAddrOf(_, ref expr) => {
286288
if let Ok(code) = self.tcx.sess.codemap().span_to_snippet(expr.span) {
287-
return Some(format!("try with `{}`", code));
289+
return Some(("consider removing the borrow",
290+
code));
288291
}
289292
}
290293

@@ -295,7 +298,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
295298
expr.span) {
296299
let sp = self.sess().codemap().call_span_if_macro(expr.span);
297300
if let Ok(code) = self.tcx.sess.codemap().span_to_snippet(sp) {
298-
return Some(format!("try with `*{}`", code));
301+
return Some(("consider dereferencing the borrow",
302+
format!("*{}", code)));
299303
}
300304
}
301305
},

src/test/compile-fail/issue-13058.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ fn check<'r, I: Iterator<Item=usize>, T: Itble<'r, usize, I>>(cont: &T) -> bool
3535
fn main() {
3636
check((3, 5));
3737
//~^ ERROR mismatched types
38-
//~| HELP try with `&(3, 5)`
38+
//~| HELP consider using a borrow
3939
}

src/test/ui/deref-suggestion.stderr

+12-6
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,37 @@ error[E0308]: mismatched types
1717
--> $DIR/deref-suggestion.rs:23:10
1818
|
1919
23 | foo3(u); //~ ERROR mismatched types
20-
| ^ expected u32, found &u32
20+
| ^
21+
| |
22+
| expected u32, found &u32
23+
| help: consider dereferencing the borrow: `*u`
2124
|
2225
= note: expected type `u32`
2326
found type `&u32`
24-
= help: try with `*u`
2527

2628
error[E0308]: mismatched types
2729
--> $DIR/deref-suggestion.rs:30:9
2830
|
2931
30 | foo(&"aaa".to_owned()); //~ ERROR mismatched types
30-
| ^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found reference
32+
| ^^^^^^^^^^^^^^^^^
33+
| |
34+
| expected struct `std::string::String`, found reference
35+
| help: consider removing the borrow: `"aaa".to_owned()`
3136
|
3237
= note: expected type `std::string::String`
3338
found type `&std::string::String`
34-
= help: try with `"aaa".to_owned()`
3539

3640
error[E0308]: mismatched types
3741
--> $DIR/deref-suggestion.rs:31:9
3842
|
3943
31 | foo(&mut "aaa".to_owned()); //~ ERROR mismatched types
40-
| ^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found mutable reference
44+
| ^^^^^^^^^^^^^^^^^^^^^
45+
| |
46+
| expected struct `std::string::String`, found mutable reference
47+
| help: consider removing the borrow: `"aaa".to_owned()`
4148
|
4249
= note: expected type `std::string::String`
4350
found type `&mut std::string::String`
44-
= help: try with `"aaa".to_owned()`
4551

4652
error[E0308]: mismatched types
4753
--> $DIR/deref-suggestion.rs:12:20

src/test/ui/span/coerce-suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn main() {
2323
//~^ ERROR E0308
2424
//~| NOTE expected &str, found struct `std::string::String`
2525
//~| NOTE expected type `&str`
26-
//~| HELP try with `&String::new()`
26+
//~| HELP consider using a borrow
2727
let y = String::new();
2828
test(&y);
2929
//~^ ERROR E0308

src/test/ui/span/coerce-suggestions.stderr

+8-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ error[E0308]: mismatched types
1414
--> $DIR/coerce-suggestions.rs:22:19
1515
|
1616
22 | let x: &str = String::new();
17-
| ^^^^^^^^^^^^^ expected &str, found struct `std::string::String`
17+
| ^^^^^^^^^^^^^
18+
| |
19+
| expected &str, found struct `std::string::String`
20+
| help: consider using a borrow: `&String::new()`
1821
|
1922
= note: expected type `&str`
2023
found type `std::string::String`
21-
= help: try with `&String::new()`
2224

2325
error[E0308]: mismatched types
2426
--> $DIR/coerce-suggestions.rs:28:10
@@ -48,11 +50,13 @@ error[E0308]: mismatched types
4850
--> $DIR/coerce-suggestions.rs:42:9
4951
|
5052
42 | s = format!("foo");
51-
| ^^^^^^^^^^^^^^ expected mutable reference, found struct `std::string::String`
53+
| ^^^^^^^^^^^^^^
54+
| |
55+
| expected mutable reference, found struct `std::string::String`
56+
| help: consider using a mutable borrow: `&mut format!("foo")`
5257
|
5358
= note: expected type `&mut std::string::String`
5459
found type `std::string::String`
55-
= help: try with `&mut format!("foo")`
5660
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
5761

5862
error: aborting due to 6 previous errors

src/test/ui/str-lit-type-mismatch.stderr

+12-6
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,37 @@ error[E0308]: mismatched types
22
--> $DIR/str-lit-type-mismatch.rs:13:20
33
|
44
13 | let x: &[u8] = "foo"; //~ ERROR mismatched types
5-
| ^^^^^ expected slice, found str
5+
| ^^^^^
6+
| |
7+
| expected slice, found str
8+
| help: consider adding a leading `b`: `b"foo"`
69
|
710
= note: expected type `&[u8]`
811
found type `&'static str`
9-
= help: try `b"foo"`
1012

1113
error[E0308]: mismatched types
1214
--> $DIR/str-lit-type-mismatch.rs:14:23
1315
|
1416
14 | let y: &[u8; 4] = "baaa"; //~ ERROR mismatched types
15-
| ^^^^^^ expected array of 4 elements, found str
17+
| ^^^^^^
18+
| |
19+
| expected array of 4 elements, found str
20+
| help: consider adding a leading `b`: `b"baaa"`
1621
|
1722
= note: expected type `&[u8; 4]`
1823
found type `&'static str`
19-
= help: try `b"baaa"`
2024

2125
error[E0308]: mismatched types
2226
--> $DIR/str-lit-type-mismatch.rs:15:19
2327
|
2428
15 | let z: &str = b"foo"; //~ ERROR mismatched types
25-
| ^^^^^^ expected str, found array of 3 elements
29+
| ^^^^^^
30+
| |
31+
| expected str, found array of 3 elements
32+
| help: consider removing the leading `b`: `"foo"`
2633
|
2734
= note: expected type `&str`
2835
found type `&'static [u8; 3]`
29-
= help: try `"foo"`
3036

3137
error: aborting due to 3 previous errors
3238

0 commit comments

Comments
 (0)