diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 7e2f3e62589f..01479180dd3e 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -2787,7 +2787,7 @@ fn can_use_as_ref(&self, expr: &hir::Expr<'_>) -> Option<(Vec<(Span, String)>, & return None; }; - let self_ty = self.typeck_results.borrow().expr_ty(receiver); + let self_ty = self.typeck_results.borrow().expr_ty_opt(receiver)?; let name = method_path.ident.name; let is_as_ref_able = match self_ty.peel_refs().kind() { ty::Adt(def, _) => { diff --git a/tests/ui/closures/closure-arg-type-mismatch-method-receiver.rs b/tests/ui/closures/closure-arg-type-mismatch-method-receiver.rs new file mode 100644 index 000000000000..0d903d03261c --- /dev/null +++ b/tests/ui/closures/closure-arg-type-mismatch-method-receiver.rs @@ -0,0 +1,13 @@ +// Regression test for https://github.com/rust-lang/rust/issues/156299. + +struct A; + +fn foo(_: &A) {} + +fn test_foo() { + (|a: A| foo(a)).bar(); + //~^ ERROR mismatched types + //~| ERROR no method named `bar` found +} + +fn main() {} diff --git a/tests/ui/closures/closure-arg-type-mismatch-method-receiver.stderr b/tests/ui/closures/closure-arg-type-mismatch-method-receiver.stderr new file mode 100644 index 000000000000..4cd1bd8c1d9c --- /dev/null +++ b/tests/ui/closures/closure-arg-type-mismatch-method-receiver.stderr @@ -0,0 +1,28 @@ +error[E0308]: mismatched types + --> $DIR/closure-arg-type-mismatch-method-receiver.rs:8:17 + | +LL | (|a: A| foo(a)).bar(); + | --- ^ expected `&A`, found `A` + | | + | arguments to this function are incorrect + | +note: function defined here + --> $DIR/closure-arg-type-mismatch-method-receiver.rs:5:4 + | +LL | fn foo(_: &A) {} + | ^^^ ----- +help: consider borrowing here + | +LL | (|a: A| foo(&a)).bar(); + | + + +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 + --> $DIR/closure-arg-type-mismatch-method-receiver.rs:8:21 + | +LL | (|a: A| foo(a)).bar(); + | ^^^ method not found in `{closure@$DIR/closure-arg-type-mismatch-method-receiver.rs:8:6: 8:12}` + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0308, E0599. +For more information about an error, try `rustc --explain E0308`.