diff --git a/compiler/rustc_borrowck/src/diagnostics/mod.rs b/compiler/rustc_borrowck/src/diagnostics/mod.rs index 56e0c74c85e97..5a03c56a1724d 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mod.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mod.rs @@ -526,7 +526,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { ty::Array(ty, _) | ty::Slice(ty) => { self.describe_field_from_ty(ty, field, variant_index, including_tuple_field) } - ty::Closure(def_id, _) | ty::Coroutine(def_id, _) => { + ty::Closure(def_id, _) | ty::CoroutineClosure(def_id, _) | ty::Coroutine(def_id, _) => { // We won't be borrowck'ing here if the closure came from another crate, // so it's safe to call `expect_local`. // diff --git a/tests/ui/async-await/async-closures/borrowck-describe-field.rs b/tests/ui/async-await/async-closures/borrowck-describe-field.rs new file mode 100644 index 0000000000000..fcbb15f20c57d --- /dev/null +++ b/tests/ui/async-await/async-closures/borrowck-describe-field.rs @@ -0,0 +1,19 @@ +// Regression test for #478: describe_field_from_ty missing CoroutineClosure. +// Before the fix, borrow errors involving async closure captures would show +// "field 0" instead of the captured variable name, because the CoroutineClosure +// type was not recognized and fell through to the default arm. + +//@ edition:2021 + +fn main() { + let mut x = String::from("hello"); + + let c = async || { + x.push_str(" world"); //~ ERROR + }; + + // Immutable borrow of x while async closure holds mutable borrow + println!("{}", x); //~ ERROR + + let _ = c; +}