Skip to content

Commit

Permalink
Rollup merge of #106581 - estebank:bad-suggestion, r=compiler-errors
Browse files Browse the repository at this point in the history
Do not emit wrong E0308 suggestion for closure mismatch

Found in #76353.
  • Loading branch information
Yuki Okushi authored Jan 8, 2023
2 parents 6459a51 + ebbc5da commit 789ebdc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/type/closure-with-wrong-borrows.rs
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions src/test/ui/type/closure-with-wrong-borrows.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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)) {
| ^ -------------------

error: aborting due to previous error

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

0 comments on commit 789ebdc

Please sign in to comment.