From 26f38c0f344f35e8d252ed756bf6794cbe974329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 26 Aug 2018 16:54:06 -0700 Subject: [PATCH 1/2] Do not suggest dereferencing in macro --- src/librustc_typeck/check/demand.rs | 13 ++++++++----- src/test/ui/deref-suggestion.rs | 16 +++++++++++----- src/test/ui/deref-suggestion.stderr | 26 ++++++++++++++++++-------- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index b0b2799c793b2..f0ca0df1b48bf 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -351,11 +351,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { if !self.infcx.type_moves_by_default(self.param_env, checked, sp) { - let sp = cm.call_span_if_macro(sp); - if let Ok(code) = cm.span_to_snippet(sp) { - return Some((sp, - "consider dereferencing the borrow", - format!("*{}", code))); + // do not suggest if the span comes from a macro (#52783) + if let (Ok(code), + true) = (cm.span_to_snippet(sp), sp == expr.span) { + return Some(( + sp, + "consider dereferencing the borrow", + format!("*{}", code), + )); } } } diff --git a/src/test/ui/deref-suggestion.rs b/src/test/ui/deref-suggestion.rs index 04ba4ab905eb1..49df7108f29a1 100644 --- a/src/test/ui/deref-suggestion.rs +++ b/src/test/ui/deref-suggestion.rs @@ -15,20 +15,26 @@ macro_rules! borrow { fn foo(_: String) {} fn foo2(s: &String) { - foo(s); //~ ERROR mismatched types + foo(s); + //~^ ERROR mismatched types } fn foo3(_: u32) {} fn foo4(u: &u32) { - foo3(u); //~ ERROR mismatched types + foo3(u); + //~^ ERROR mismatched types } fn main() { let s = String::new(); let r_s = &s; foo2(r_s); - foo(&"aaa".to_owned()); //~ ERROR mismatched types - foo(&mut "aaa".to_owned()); //~ ERROR mismatched types + foo(&"aaa".to_owned()); + //~^ ERROR mismatched types + foo(&mut "aaa".to_owned()); + //~^ ERROR mismatched types foo3(borrow!(0)); foo4(&0); -} + assert_eq!(3i32, &3i32); + //~^ ERROR mismatched types +} \ No newline at end of file diff --git a/src/test/ui/deref-suggestion.stderr b/src/test/ui/deref-suggestion.stderr index a5f87928924ca..9811c5969dad6 100644 --- a/src/test/ui/deref-suggestion.stderr +++ b/src/test/ui/deref-suggestion.stderr @@ -1,7 +1,7 @@ error[E0308]: mismatched types --> $DIR/deref-suggestion.rs:18:9 | -LL | foo(s); //~ ERROR mismatched types +LL | foo(s); | ^ | | | expected struct `std::string::String`, found reference @@ -11,9 +11,9 @@ LL | foo(s); //~ ERROR mismatched types found type `&std::string::String` error[E0308]: mismatched types - --> $DIR/deref-suggestion.rs:23:10 + --> $DIR/deref-suggestion.rs:24:10 | -LL | foo3(u); //~ ERROR mismatched types +LL | foo3(u); | ^ | | | expected u32, found &u32 @@ -23,9 +23,9 @@ LL | foo3(u); //~ ERROR mismatched types found type `&u32` error[E0308]: mismatched types - --> $DIR/deref-suggestion.rs:30:9 + --> $DIR/deref-suggestion.rs:32:9 | -LL | foo(&"aaa".to_owned()); //~ ERROR mismatched types +LL | foo(&"aaa".to_owned()); | ^^^^^^^^^^^^^^^^^ | | | expected struct `std::string::String`, found reference @@ -35,9 +35,9 @@ LL | foo(&"aaa".to_owned()); //~ ERROR mismatched types found type `&std::string::String` error[E0308]: mismatched types - --> $DIR/deref-suggestion.rs:31:9 + --> $DIR/deref-suggestion.rs:34:9 | -LL | foo(&mut "aaa".to_owned()); //~ ERROR mismatched types +LL | foo(&mut "aaa".to_owned()); | ^^^^^^^^^^^^^^^^^^^^^ | | | expected struct `std::string::String`, found mutable reference @@ -58,6 +58,16 @@ LL | foo3(borrow!(0)); = note: expected type `u32` found type `&{integer}` -error: aborting due to 5 previous errors +error[E0308]: mismatched types + --> $DIR/deref-suggestion.rs:38:5 + | +LL | assert_eq!(3i32, &3i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^ expected i32, found &i32 + | + = note: expected type `i32` + found type `&i32` + = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) + +error: aborting due to 6 previous errors For more information about this error, try `rustc --explain E0308`. From a2722f326138813d1d9cf0946523dd38d1f06b35 Mon Sep 17 00:00:00 2001 From: Esteban Kuber Date: Sun, 26 Aug 2018 18:03:57 -0700 Subject: [PATCH 2/2] readd final newline --- src/test/ui/deref-suggestion.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/deref-suggestion.rs b/src/test/ui/deref-suggestion.rs index 49df7108f29a1..1776a71a6bb47 100644 --- a/src/test/ui/deref-suggestion.rs +++ b/src/test/ui/deref-suggestion.rs @@ -37,4 +37,4 @@ fn main() { foo4(&0); assert_eq!(3i32, &3i32); //~^ ERROR mismatched types -} \ No newline at end of file +}