Skip to content

Commit

Permalink
Suppress malformed closure return type suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Apr 18, 2024
1 parent 354d48e commit 79b89af
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
13 changes: 12 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,13 +799,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
can_suggest: bool,
fn_id: LocalDefId,
) -> bool {
// Can't suggest `->` on a block-like coroutine
// Can't suggest `->` on a block-like coroutine.
if let Some(hir::CoroutineKind::Desugared(_, hir::CoroutineSource::Block)) =
self.tcx.coroutine_kind(fn_id)
{
return false;
}

// FIXME: Add braces to a closure when suggesting a return type.
// A case where this may happen is `|| if { return x; }`, though we
// should first think hard about whether these suggestions are useful.
if let hir::Node::Expr(hir::Expr {
kind: ExprKind::Closure(hir::Closure { body, .. }), ..
}) = self.tcx.hir_node_by_def_id(fn_id)
&& !matches!(self.tcx.hir().body(*body).value.kind, hir::ExprKind::Block(..))
{
return false;
}

let found =
self.resolve_numeric_literals_with_default(self.resolve_vars_if_possible(found));
// Only suggest changing the return type for methods that
Expand Down
26 changes: 15 additions & 11 deletions tests/ui/typeck/issue-81943.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,36 @@ error[E0308]: mismatched types
--> $DIR/issue-81943.rs:7:9
|
LL | f(|x| lib::d!(x));
| -^^^^^^^^^^ expected `()`, found `i32`
| |
| help: try adding a return type: `-> i32`
| ^^^^^^^^^^ expected `()`, found `i32`
|
= note: this error originates in the macro `lib::d` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
--> $DIR/issue-81943.rs:8:28
|
LL | f(|x| match x { tmp => { g(tmp) } });
| ^^^^^^ expected `()`, found `i32`
| -------------------^^^^^^----
| | |
| | expected `()`, found `i32`
| expected this to be `()`
|
help: consider using a semicolon here
|
LL | f(|x| match x { tmp => { g(tmp); } });
| +
help: try adding a return type
help: consider using a semicolon here
|
LL | f(|x| -> i32 match x { tmp => { g(tmp) } });
| ++++++
LL | f(|x| match x { tmp => { g(tmp) } };);
| +

error[E0308]: mismatched types
--> $DIR/issue-81943.rs:10:38
|
LL | ($e:expr) => { match $e { x => { g(x) } } }
| ^^^^ expected `()`, found `i32`
| ------------------^^^^----
| | |
| | expected `()`, found `i32`
| expected this to be `()`
LL | }
LL | f(|x| d!(x));
| ----- in this macro invocation
Expand All @@ -37,10 +41,10 @@ help: consider using a semicolon here
|
LL | ($e:expr) => { match $e { x => { g(x); } } }
| +
help: try adding a return type
help: consider using a semicolon here
|
LL | f(|x| -> i32 d!(x));
| ++++++
LL | ($e:expr) => { match $e { x => { g(x) } }; }
| +

error: aborting due to 3 previous errors

Expand Down

0 comments on commit 79b89af

Please sign in to comment.