Skip to content

Commit abf259c

Browse files
committed
var_subdiag refinement
trim old
1 parent a0cee0a commit abf259c

File tree

5 files changed

+80
-57
lines changed

5 files changed

+80
-57
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -715,22 +715,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
715715
borrow_span,
716716
&self.describe_any_place(borrow.borrowed_place.as_ref()),
717717
);
718-
borrow_spans.var_subdiag(
719-
&mut err,
720-
|var_span| {
721-
use crate::session_diagnostics::CaptureVarCause::*;
722-
let place = &borrow.borrowed_place;
723-
let desc_place = self.describe_any_place(place.as_ref());
724-
match borrow_spans {
725-
UseSpans::ClosureUse { generator_kind, .. } => match generator_kind {
726-
Some(_) => BorrowUsePlaceGenerator { place: desc_place, var_span },
727-
None => BorrowUsePlaceClosure { place: desc_place, var_span },
728-
},
729-
_ => BorrowUsePlace { place: desc_place, var_span },
730-
}
731-
},
732-
"mutable",
733-
);
718+
borrow_spans.var_subdiag(&mut err, Some(borrow.kind), |kind, var_span| {
719+
use crate::session_diagnostics::CaptureVarCause::*;
720+
let place = &borrow.borrowed_place;
721+
let desc_place = self.describe_any_place(place.as_ref());
722+
match kind {
723+
Some(_) => BorrowUsePlaceGenerator { place: desc_place, var_span },
724+
None => BorrowUsePlaceClosure { place: desc_place, var_span },
725+
}
726+
});
734727

735728
self.explain_why_borrow_contains_point(location, borrow, None)
736729
.add_explanation_to_diagnostic(

compiler/rustc_borrowck/src/diagnostics/mod.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -650,19 +650,28 @@ impl UseSpans<'_> {
650650
pub(super) fn var_subdiag(
651651
self,
652652
err: &mut Diagnostic,
653-
f: impl Fn(Span) -> crate::session_diagnostics::CaptureVarCause,
654-
kind_desc: impl Into<String>,
653+
kind: Option<rustc_middle::mir::BorrowKind>,
654+
f: impl Fn(Option<GeneratorKind>, Span) -> crate::session_diagnostics::CaptureVarCause,
655655
) {
656-
if let UseSpans::ClosureUse { capture_kind_span, path_span, .. } = self {
657-
if capture_kind_span == path_span {
658-
err.subdiagnostic(f(capture_kind_span));
659-
} else {
660-
err.subdiagnostic(crate::session_diagnostics::CaptureVarKind {
661-
kind_desc: kind_desc.into(),
662-
kind_span: capture_kind_span,
656+
use crate::session_diagnostics::CaptureVarKind::*;
657+
if let UseSpans::ClosureUse { generator_kind, capture_kind_span, path_span, .. } = self {
658+
if capture_kind_span != path_span {
659+
err.subdiagnostic(match kind {
660+
Some(kd) => match kd {
661+
rustc_middle::mir::BorrowKind::Shared
662+
| rustc_middle::mir::BorrowKind::Shallow
663+
| rustc_middle::mir::BorrowKind::Unique => {
664+
Immute { kind_span: capture_kind_span }
665+
}
666+
667+
rustc_middle::mir::BorrowKind::Mut { .. } => {
668+
Mut { kind_span: capture_kind_span }
669+
}
670+
},
671+
None => Move { kind_span: capture_kind_span },
663672
});
664-
err.subdiagnostic(f(path_span));
665-
}
673+
};
674+
err.subdiagnostic(f(generator_kind, path_span));
666675
}
667676
}
668677

compiler/rustc_borrowck/src/session_diagnostics.rs

+35-30
Original file line numberDiff line numberDiff line change
@@ -149,36 +149,6 @@ pub(crate) enum RequireStaticErr {
149149
},
150150
}
151151

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-
}
181-
182152
#[derive(Subdiagnostic)]
183153
pub(crate) enum CaptureVarPathUseCause {
184154
#[label(borrowck_borrow_due_to_use_generator)]
@@ -222,3 +192,38 @@ pub(crate) enum CaptureVarPathUseCause {
222192
path_span: Span,
223193
},
224194
}
195+
196+
#[derive(Subdiagnostic)]
197+
pub(crate) enum CaptureVarKind {
198+
#[label(borrowck_capture_immute)]
199+
Immute {
200+
#[primary_span]
201+
kind_span: Span,
202+
},
203+
#[label(borrowck_capture_mut)]
204+
Mut {
205+
#[primary_span]
206+
kind_span: Span,
207+
},
208+
#[label(borrowck_capture_move)]
209+
Move {
210+
#[primary_span]
211+
kind_span: Span,
212+
},
213+
}
214+
215+
#[derive(Subdiagnostic)]
216+
pub(crate) enum CaptureVarCause {
217+
#[label(borrowck_var_borrow_by_use_place_in_generator)]
218+
BorrowUsePlaceGenerator {
219+
place: String,
220+
#[primary_span]
221+
var_span: Span,
222+
},
223+
#[label(borrowck_var_borrow_by_use_place_in_closure)]
224+
BorrowUsePlaceClosure {
225+
place: String,
226+
#[primary_span]
227+
var_span: Span,
228+
},
229+
}

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

+15
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,18 @@ borrowck_assign_due_to_use_closure =
9494
9595
borrowck_assign_part_due_to_use_closure =
9696
assign to part occurs due to use in closure
97+
98+
borrowck_capture_immute =
99+
capture is immutable because of use here
100+
101+
borrowck_capture_mut =
102+
capture is mutable because of use here
103+
104+
borrowck_capture_move =
105+
capture is moved because of use here
106+
107+
borrowck_var_move_by_use_place_in_generator =
108+
move occurs due to use of {$place} in generator
109+
110+
borrowck_var_move_by_use_place_in_closure =
111+
move occurs due to use of {$place} in closure

compiler/rustc_middle/src/mir/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,7 @@ impl BorrowKind {
18981898
}
18991899
}
19001900

1901+
// FIXME: won't be used after diagnostic migration
19011902
pub fn describe_mutability(&self) -> &str {
19021903
match *self {
19031904
BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => "immutable",

0 commit comments

Comments
 (0)