From d85d38b7b8ad9faa12231bbcc9a0214410c0719c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 8 Jan 2023 01:53:08 +0000 Subject: [PATCH 1/2] Add test --- .../ui/type/closure-with-wrong-borrows.rs | 10 ++++++++ .../ui/type/closure-with-wrong-borrows.stderr | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/test/ui/type/closure-with-wrong-borrows.rs create mode 100644 src/test/ui/type/closure-with-wrong-borrows.stderr diff --git a/src/test/ui/type/closure-with-wrong-borrows.rs b/src/test/ui/type/closure-with-wrong-borrows.rs new file mode 100644 index 0000000000000..5f6a78351a247 --- /dev/null +++ b/src/test/ui/type/closure-with-wrong-borrows.rs @@ -0,0 +1,10 @@ +struct S<'a>(&'a str); + +fn f(inner: fn(&str, &S)) { +} + +#[allow(unreachable_code)] +fn main() { + let inner: fn(_, _) = unimplemented!(); + f(inner); //~ ERROR mismatched types +} diff --git a/src/test/ui/type/closure-with-wrong-borrows.stderr b/src/test/ui/type/closure-with-wrong-borrows.stderr new file mode 100644 index 0000000000000..e13db6c325add --- /dev/null +++ b/src/test/ui/type/closure-with-wrong-borrows.stderr @@ -0,0 +1,23 @@ +error[E0308]: mismatched types + --> $DIR/closure-with-wrong-borrows.rs:9:7 + | +LL | f(inner); + | - ^^^^^ one type is more general than the other + | | + | arguments to this function are incorrect + | + = note: expected fn pointer `for<'a, 'b, 'c> fn(&'a str, &'b S<'c>)` + found fn pointer `fn(_, _)` +note: function defined here + --> $DIR/closure-with-wrong-borrows.rs:3:4 + | +LL | fn f(inner: fn(&str, &S)) { + | ^ ------------------- +help: consider removing the `` + | +LL | f(inner); + | + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. From ebbc5dafd351551bdeab3e4edbdf9a03898a9017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 8 Jan 2023 01:53:39 +0000 Subject: [PATCH 2/2] Do not emit wrong E0308 suggestion for closure mismatch --- compiler/rustc_hir_typeck/src/demand.rs | 5 ++++- src/test/ui/type/closure-with-wrong-borrows.stderr | 4 ---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index 0f191c21a0a63..f15cf5e3ef948 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -1379,7 +1379,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } // If we've reached our target type with just removing `&`, then just print now. - if steps == 0 { + if steps == 0 && !remove.trim().is_empty() { return Some(( prefix_span, format!("consider removing the `{}`", remove.trim()), @@ -1438,6 +1438,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } else { (prefix_span, format!("{}{}", prefix, "*".repeat(steps))) }; + if suggestion.trim().is_empty() { + return None; + } return Some(( span, diff --git a/src/test/ui/type/closure-with-wrong-borrows.stderr b/src/test/ui/type/closure-with-wrong-borrows.stderr index e13db6c325add..7370bc7646765 100644 --- a/src/test/ui/type/closure-with-wrong-borrows.stderr +++ b/src/test/ui/type/closure-with-wrong-borrows.stderr @@ -13,10 +13,6 @@ note: function defined here | LL | fn f(inner: fn(&str, &S)) { | ^ ------------------- -help: consider removing the `` - | -LL | f(inner); - | error: aborting due to previous error