Skip to content

Commit 01cccf9

Browse files
Rollup merge of rust-lang#156301 - TaKO8Ki:fix-156299-closure-receiver-suggestion-ice, r=wesleywiser
Avoid ICE when suggesting as_ref for ill-typed closure receivers Fixes rust-lang#156299 When building mismatch suggestions, `can_use_as_ref` may inspect the receiver of a method call that is itself an ill-typed closure expression. In that recovery path, the receiver may not have a recorded type in `TypeckResults`. Use `expr_ty_opt` instead of `expr_ty` so the optional `as_ref()` suggestion is skipped when the receiver type is unavailable.
2 parents fbc52c9 + d467037 commit 01cccf9

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2787,7 +2787,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
27872787
return None;
27882788
};
27892789

2790-
let self_ty = self.typeck_results.borrow().expr_ty(receiver);
2790+
let self_ty = self.typeck_results.borrow().expr_ty_opt(receiver)?;
27912791
let name = method_path.ident.name;
27922792
let is_as_ref_able = match self_ty.peel_refs().kind() {
27932793
ty::Adt(def, _) => {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Regression test for https://github.com/rust-lang/rust/issues/156299.
2+
3+
struct A;
4+
5+
fn foo(_: &A) {}
6+
7+
fn test_foo() {
8+
(|a: A| foo(a)).bar();
9+
//~^ ERROR mismatched types
10+
//~| ERROR no method named `bar` found
11+
}
12+
13+
fn main() {}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/closure-arg-type-mismatch-method-receiver.rs:8:17
3+
|
4+
LL | (|a: A| foo(a)).bar();
5+
| --- ^ expected `&A`, found `A`
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
note: function defined here
10+
--> $DIR/closure-arg-type-mismatch-method-receiver.rs:5:4
11+
|
12+
LL | fn foo(_: &A) {}
13+
| ^^^ -----
14+
help: consider borrowing here
15+
|
16+
LL | (|a: A| foo(&a)).bar();
17+
| +
18+
19+
error[E0599]: no method named `bar` found for closure `{closure@$DIR/closure-arg-type-mismatch-method-receiver.rs:8:6: 8:12}` in the current scope
20+
--> $DIR/closure-arg-type-mismatch-method-receiver.rs:8:21
21+
|
22+
LL | (|a: A| foo(a)).bar();
23+
| ^^^ method not found in `{closure@$DIR/closure-arg-type-mismatch-method-receiver.rs:8:6: 8:12}`
24+
25+
error: aborting due to 2 previous errors
26+
27+
Some errors have detailed explanations: E0308, E0599.
28+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)