Skip to content

Commit 2a9b5f9

Browse files
committed
ClosureCannotAgain
1 parent 50ee33d commit 2a9b5f9

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use rustc_span::{symbol::sym, Span, Symbol, DUMMY_SP};
2222
use rustc_target::abi::VariantIdx;
2323
use rustc_trait_selection::traits::type_known_to_meet_bound_modulo_regions;
2424

25+
use crate::session_diagnostics::ClosureCannotAgain;
26+
2527
use super::borrow_set::BorrowData;
2628
use super::MirBorrowckCtxt;
2729

@@ -119,14 +121,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
119121
if let Some((span, hir_place)) =
120122
self.infcx.tcx.typeck(did).closure_kind_origins().get(hir_id)
121123
{
122-
diag.span_note(
123-
*span,
124-
&format!(
125-
"closure cannot be invoked more than once because it moves the \
126-
variable `{}` out of its environment",
127-
ty::place_to_string_for_capture(self.infcx.tcx, hir_place)
128-
),
129-
);
124+
diag.subdiagnostic(ClosureCannotAgain::Invoke {
125+
place: ty::place_to_string_for_capture(self.infcx.tcx, hir_place),
126+
span: *span,
127+
});
130128
return;
131129
}
132130
}
@@ -143,14 +141,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
143141
if let Some((span, hir_place)) =
144142
self.infcx.tcx.typeck(did).closure_kind_origins().get(hir_id)
145143
{
146-
diag.span_note(
147-
*span,
148-
&format!(
149-
"closure cannot be moved more than once as it is not `Copy` due to \
150-
moving the variable `{}` out of its environment",
151-
ty::place_to_string_for_capture(self.infcx.tcx, hir_place)
152-
),
153-
);
144+
diag.subdiagnostic(ClosureCannotAgain::Move {
145+
place: ty::place_to_string_for_capture(self.infcx.tcx, hir_place),
146+
span: *span,
147+
});
154148
}
155149
}
156150
}

compiler/rustc_borrowck/src/session_diagnostics.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,19 @@ pub(crate) enum OnLifetimeBound<'a> {
343343
#[help(borrowck::consider_add_lifetime_bound)]
344344
Add { fr_name: &'a RegionName, outlived_fr_name: &'a RegionName },
345345
}
346+
347+
#[derive(SessionSubdiagnostic)]
348+
pub(crate) enum ClosureCannotAgain {
349+
#[note(borrowck::closure_cannot_invoke_again)]
350+
Invoke {
351+
place: String,
352+
#[primary_span]
353+
span: Span,
354+
},
355+
#[note(borrowck::closure_cannot_move_again)]
356+
Move {
357+
place: String,
358+
#[primary_span]
359+
span: Span,
360+
},
361+
}

compiler/rustc_error_messages/locales/en-US/borrowck.ftl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,9 @@ borrowck_outlive_constraint_need_borrow_for =
138138
139139
borrowck_consider_add_lifetime_bound =
140140
consider adding the following bound: `{$fr_name}: {$outlived_fr_name}`
141+
142+
borrowck_closure_cannot_invoke_again =
143+
closure cannot be invoked more than once because it moves the variable `{$place}` out of its environment
144+
145+
borrowck_closure_cannot_move_again =
146+
closure cannot be moved more than once as it is not `Copy` due to moving the variable `{$place}` out of its environment

0 commit comments

Comments
 (0)