Skip to content

Commit 6138215

Browse files
authored
Rollup merge of rust-lang#103559 - AndyJado:var_span_label, r=davidtwco
first move on a nested span_label trying not to be smart this time.
2 parents 3779222 + e49d10d commit 6138215

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -724,13 +724,19 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
724724
borrow_span,
725725
&self.describe_any_place(borrow.borrowed_place.as_ref()),
726726
);
727-
728-
borrow_spans.var_span_label(
727+
borrow_spans.var_subdiag(
729728
&mut err,
730-
{
729+
|var_span| {
730+
use crate::session_diagnostics::CaptureVarCause::*;
731731
let place = &borrow.borrowed_place;
732732
let desc_place = self.describe_any_place(place.as_ref());
733-
format!("borrow occurs due to use of {}{}", desc_place, borrow_spans.describe())
733+
match borrow_spans {
734+
UseSpans::ClosureUse { generator_kind, .. } => match generator_kind {
735+
Some(_) => BorrowUsePlaceGenerator { place: desc_place, var_span },
736+
None => BorrowUsePlaceClosure { place: desc_place, var_span },
737+
},
738+
_ => BorrowUsePlace { place: desc_place, var_span },
739+
}
734740
},
735741
"mutable",
736742
);

compiler/rustc_borrowck/src/diagnostics/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,26 @@ impl UseSpans<'_> {
623623
}
624624
}
625625

626+
/// Add a subdiagnostic to the use of the captured variable, if it exists.
627+
pub(super) fn var_subdiag(
628+
self,
629+
err: &mut Diagnostic,
630+
f: impl Fn(Span) -> crate::session_diagnostics::CaptureVarCause,
631+
kind_desc: impl Into<String>,
632+
) {
633+
if let UseSpans::ClosureUse { capture_kind_span, path_span, .. } = self {
634+
if capture_kind_span == path_span {
635+
err.subdiagnostic(f(capture_kind_span));
636+
} else {
637+
err.subdiagnostic(crate::session_diagnostics::CaptureVarKind {
638+
kind_desc: kind_desc.into(),
639+
kind_span: capture_kind_span,
640+
});
641+
err.subdiagnostic(f(path_span));
642+
}
643+
}
644+
}
645+
626646
/// Returns `false` if this place is not used in a closure.
627647
pub(super) fn for_closure(&self) -> bool {
628648
match *self {

compiler/rustc_borrowck/src/session_diagnostics.rs

+30
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,33 @@ pub(crate) enum RequireStaticErr {
148148
multi_span: MultiSpan,
149149
},
150150
}
151+
152+
#[derive(Subdiagnostic)]
153+
#[label(borrowck_capture_kind_label)]
154+
pub(crate) struct CaptureVarKind {
155+
pub kind_desc: String,
156+
#[primary_span]
157+
pub kind_span: Span,
158+
}
159+
160+
#[derive(Subdiagnostic)]
161+
pub(crate) enum CaptureVarCause {
162+
#[label(borrowck_var_borrow_by_use_place)]
163+
BorrowUsePlace {
164+
place: String,
165+
#[primary_span]
166+
var_span: Span,
167+
},
168+
#[label(borrowck_var_borrow_by_use_place_in_generator)]
169+
BorrowUsePlaceGenerator {
170+
place: String,
171+
#[primary_span]
172+
var_span: Span,
173+
},
174+
#[label(borrowck_var_borrow_by_use_place_in_closure)]
175+
BorrowUsePlaceClosure {
176+
place: String,
177+
#[primary_span]
178+
var_span: Span,
179+
},
180+
}

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

+12
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,15 @@ borrowck_returned_lifetime_short =
5858
5959
borrowck_used_impl_require_static =
6060
the used `impl` has a `'static` requirement
61+
62+
borrowck_capture_kind_label =
63+
capture is {$kind_desc} because of use here
64+
65+
borrowck_var_borrow_by_use_place_in_generator =
66+
borrow occurs due to use of {$place} in closure in generator
67+
68+
borrowck_var_borrow_by_use_place_in_closure =
69+
borrow occurs due to use of {$place} in closure
70+
71+
borrowck_var_borrow_by_use_place =
72+
borrow occurs due to use of {$place}

0 commit comments

Comments
 (0)