Use targetted diagnostic for borrow across yield error

This commit is contained in:
Oli Scherer 2023-10-24 10:12:49 +00:00
parent 6223744078
commit bb90c4bf35
3 changed files with 4 additions and 3 deletions

View file

@ -373,11 +373,12 @@ pub(crate) fn cannot_borrow_across_coroutine_yield(
span: Span, span: Span,
yield_span: Span, yield_span: Span,
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> { ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
let coroutine_kind = self.body.coroutine.as_ref().unwrap().coroutine_kind;
let mut err = struct_span_err!( let mut err = struct_span_err!(
self, self,
span, span,
E0626, E0626,
"borrow may still be in use when coroutine yields", "borrow may still be in use when {coroutine_kind:#} yields",
); );
err.span_label(yield_span, "possible yield occurs here"); err.span_label(yield_span, "possible yield occurs here");
err err

View file

@ -6,7 +6,7 @@ fn main() {
let mut x = { let mut x = {
let mut x = gen { let mut x = gen {
let y = 42; let y = 42;
let z = &y; //~ ERROR: borrow may still be in use when coroutine yields let z = &y; //~ ERROR: borrow may still be in use when `gen` block yields
yield 43; yield 43;
panic!("{z}"); panic!("{z}");
}; };

View file

@ -1,4 +1,4 @@
error[E0626]: borrow may still be in use when coroutine yields error[E0626]: borrow may still be in use when `gen` block yields
--> $DIR/self_referential_gen_block.rs:9:21 --> $DIR/self_referential_gen_block.rs:9:21
| |
LL | let z = &y; LL | let z = &y;