Skip to content

Commit 94e4a29

Browse files
Rollup merge of rust-lang#53727 - estebank:incorrect-deref-suggestion, r=nikomatsakis
Do not suggest dereferencing in macro Fix rust-lang#52783.
2 parents f922ac0 + a2722f3 commit 94e4a29

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

src/librustc_typeck/check/demand.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
351351
if !self.infcx.type_moves_by_default(self.param_env,
352352
checked,
353353
sp) {
354-
let sp = cm.call_span_if_macro(sp);
355-
if let Ok(code) = cm.span_to_snippet(sp) {
356-
return Some((sp,
357-
"consider dereferencing the borrow",
358-
format!("*{}", code)));
354+
// do not suggest if the span comes from a macro (#52783)
355+
if let (Ok(code),
356+
true) = (cm.span_to_snippet(sp), sp == expr.span) {
357+
return Some((
358+
sp,
359+
"consider dereferencing the borrow",
360+
format!("*{}", code),
361+
));
359362
}
360363
}
361364
}

src/test/ui/deref-suggestion.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,26 @@ macro_rules! borrow {
1515
fn foo(_: String) {}
1616

1717
fn foo2(s: &String) {
18-
foo(s); //~ ERROR mismatched types
18+
foo(s);
19+
//~^ ERROR mismatched types
1920
}
2021

2122
fn foo3(_: u32) {}
2223
fn foo4(u: &u32) {
23-
foo3(u); //~ ERROR mismatched types
24+
foo3(u);
25+
//~^ ERROR mismatched types
2426
}
2527

2628
fn main() {
2729
let s = String::new();
2830
let r_s = &s;
2931
foo2(r_s);
30-
foo(&"aaa".to_owned()); //~ ERROR mismatched types
31-
foo(&mut "aaa".to_owned()); //~ ERROR mismatched types
32+
foo(&"aaa".to_owned());
33+
//~^ ERROR mismatched types
34+
foo(&mut "aaa".to_owned());
35+
//~^ ERROR mismatched types
3236
foo3(borrow!(0));
3337
foo4(&0);
38+
assert_eq!(3i32, &3i32);
39+
//~^ ERROR mismatched types
3440
}

src/test/ui/deref-suggestion.stderr

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error[E0308]: mismatched types
22
--> $DIR/deref-suggestion.rs:18:9
33
|
4-
LL | foo(s); //~ ERROR mismatched types
4+
LL | foo(s);
55
| ^
66
| |
77
| expected struct `std::string::String`, found reference
@@ -11,9 +11,9 @@ LL | foo(s); //~ ERROR mismatched types
1111
found type `&std::string::String`
1212

1313
error[E0308]: mismatched types
14-
--> $DIR/deref-suggestion.rs:23:10
14+
--> $DIR/deref-suggestion.rs:24:10
1515
|
16-
LL | foo3(u); //~ ERROR mismatched types
16+
LL | foo3(u);
1717
| ^
1818
| |
1919
| expected u32, found &u32
@@ -23,9 +23,9 @@ LL | foo3(u); //~ ERROR mismatched types
2323
found type `&u32`
2424

2525
error[E0308]: mismatched types
26-
--> $DIR/deref-suggestion.rs:30:9
26+
--> $DIR/deref-suggestion.rs:32:9
2727
|
28-
LL | foo(&"aaa".to_owned()); //~ ERROR mismatched types
28+
LL | foo(&"aaa".to_owned());
2929
| ^^^^^^^^^^^^^^^^^
3030
| |
3131
| expected struct `std::string::String`, found reference
@@ -35,9 +35,9 @@ LL | foo(&"aaa".to_owned()); //~ ERROR mismatched types
3535
found type `&std::string::String`
3636

3737
error[E0308]: mismatched types
38-
--> $DIR/deref-suggestion.rs:31:9
38+
--> $DIR/deref-suggestion.rs:34:9
3939
|
40-
LL | foo(&mut "aaa".to_owned()); //~ ERROR mismatched types
40+
LL | foo(&mut "aaa".to_owned());
4141
| ^^^^^^^^^^^^^^^^^^^^^
4242
| |
4343
| expected struct `std::string::String`, found mutable reference
@@ -58,6 +58,16 @@ LL | foo3(borrow!(0));
5858
= note: expected type `u32`
5959
found type `&{integer}`
6060

61-
error: aborting due to 5 previous errors
61+
error[E0308]: mismatched types
62+
--> $DIR/deref-suggestion.rs:38:5
63+
|
64+
LL | assert_eq!(3i32, &3i32);
65+
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected i32, found &i32
66+
|
67+
= note: expected type `i32`
68+
found type `&i32`
69+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
70+
71+
error: aborting due to 6 previous errors
6272

6373
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)