Skip to content

Commit 2334143

Browse files
Fix unwrap suggestion for async fn
1 parent 848a387 commit 2334143

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -1747,19 +1747,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17471747
expected: Ty<'tcx>,
17481748
found: Ty<'tcx>,
17491749
) -> bool {
1750-
let ty::Adt(adt, args) = found.kind() else { return false };
1750+
let ty::Adt(adt, args) = found.kind() else {
1751+
return false;
1752+
};
17511753
let ret_ty_matches = |diagnostic_item| {
1752-
if let Some(ret_ty) = self
1753-
.ret_coercion
1754-
.as_ref()
1755-
.map(|c| self.resolve_vars_if_possible(c.borrow().expected_ty()))
1756-
&& let ty::Adt(kind, _) = ret_ty.kind()
1757-
&& self.tcx.get_diagnostic_item(diagnostic_item) == Some(kind.did())
1758-
{
1759-
true
1760-
} else {
1761-
false
1762-
}
1754+
let Some(sig) = self.body_fn_sig() else {
1755+
return false;
1756+
};
1757+
let ty::Adt(kind, _) = sig.output().kind() else {
1758+
return false;
1759+
};
1760+
self.tcx.is_diagnostic_item(diagnostic_item, kind.did())
17631761
};
17641762

17651763
// don't suggest anything like `Ok(ok_val).unwrap()` , `Some(some_val).unwrap()`,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// edition: 2021
2+
3+
async fn dont_suggest() -> i32 {
4+
if false {
5+
return Ok(6);
6+
//~^ ERROR mismatched types
7+
}
8+
9+
5
10+
}
11+
12+
async fn do_suggest() -> i32 {
13+
if false {
14+
let s = Ok(6);
15+
return s;
16+
//~^ ERROR mismatched types
17+
}
18+
19+
5
20+
}
21+
22+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/async-unwrap-suggestion.rs:5:16
3+
|
4+
LL | return Ok(6);
5+
| ^^^^^ expected `i32`, found `Result<{integer}, _>`
6+
|
7+
= note: expected type `i32`
8+
found enum `Result<{integer}, _>`
9+
10+
error[E0308]: mismatched types
11+
--> $DIR/async-unwrap-suggestion.rs:15:16
12+
|
13+
LL | return s;
14+
| ^ expected `i32`, found `Result<{integer}, _>`
15+
|
16+
= note: expected type `i32`
17+
found enum `Result<{integer}, _>`
18+
help: consider using `Result::expect` to unwrap the `Result<{integer}, _>` value, panicking if the value is a `Result::Err`
19+
|
20+
LL | return s.expect("REASON");
21+
| +++++++++++++++++
22+
23+
error: aborting due to 2 previous errors
24+
25+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)