Skip to content

Commit ef55919

Browse files
committed
Restrict diagnostic context lifetime of mir borrowck to InferCtxt instead of TyCtxt
1 parent 81695a1 commit ef55919

File tree

8 files changed

+75
-75
lines changed

8 files changed

+75
-75
lines changed

compiler/rustc_borrowck/src/borrowck_errors.rs

+25-25
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use rustc_middle::span_bug;
66
use rustc_middle::ty::{self, Ty, TyCtxt};
77
use rustc_span::Span;
88

9-
impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
10-
pub fn dcx(&self) -> DiagCtxtHandle<'tcx> {
9+
impl<'cx, 'tcx> crate::MirBorrowckCtxt<'_, '_, 'cx, 'tcx> {
10+
pub fn dcx(&self) -> DiagCtxtHandle<'cx> {
1111
self.infcx.dcx()
1212
}
1313

@@ -18,7 +18,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
1818
place: &str,
1919
borrow_place: &str,
2020
value_place: &str,
21-
) -> Diag<'tcx> {
21+
) -> Diag<'cx> {
2222
self.dcx().create_err(crate::session_diagnostics::MoveBorrow {
2323
place,
2424
span,
@@ -34,7 +34,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
3434
desc: &str,
3535
borrow_span: Span,
3636
borrow_desc: &str,
37-
) -> Diag<'tcx> {
37+
) -> Diag<'cx> {
3838
struct_span_code_err!(
3939
self.dcx(),
4040
span,
@@ -54,7 +54,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
5454
old_loan_span: Span,
5555
old_opt_via: &str,
5656
old_load_end_span: Option<Span>,
57-
) -> Diag<'tcx> {
57+
) -> Diag<'cx> {
5858
let via = |msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {msg})") };
5959
let mut err = struct_span_code_err!(
6060
self.dcx(),
@@ -101,7 +101,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
101101
desc: &str,
102102
old_loan_span: Span,
103103
old_load_end_span: Option<Span>,
104-
) -> Diag<'tcx> {
104+
) -> Diag<'cx> {
105105
let mut err = struct_span_code_err!(
106106
self.dcx(),
107107
new_loan_span,
@@ -134,7 +134,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
134134
noun_old: &str,
135135
old_opt_via: &str,
136136
previous_end_span: Option<Span>,
137-
) -> Diag<'tcx> {
137+
) -> Diag<'cx> {
138138
let mut err = struct_span_code_err!(
139139
self.dcx(),
140140
new_loan_span,
@@ -166,7 +166,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
166166
old_opt_via: &str,
167167
previous_end_span: Option<Span>,
168168
second_borrow_desc: &str,
169-
) -> Diag<'tcx> {
169+
) -> Diag<'cx> {
170170
let mut err = struct_span_code_err!(
171171
self.dcx(),
172172
new_loan_span,
@@ -198,7 +198,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
198198
kind_old: &str,
199199
msg_old: &str,
200200
old_load_end_span: Option<Span>,
201-
) -> Diag<'tcx> {
201+
) -> Diag<'cx> {
202202
let via = |msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {msg})") };
203203
let mut err = struct_span_code_err!(
204204
self.dcx(),
@@ -239,7 +239,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
239239
span: Span,
240240
borrow_span: Span,
241241
desc: &str,
242-
) -> Diag<'tcx> {
242+
) -> Diag<'cx> {
243243
struct_span_code_err!(
244244
self.dcx(),
245245
span,
@@ -256,20 +256,20 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
256256
span: Span,
257257
desc: &str,
258258
is_arg: bool,
259-
) -> Diag<'tcx> {
259+
) -> Diag<'cx> {
260260
let msg = if is_arg { "to immutable argument" } else { "twice to immutable variable" };
261261
struct_span_code_err!(self.dcx(), span, E0384, "cannot assign {} {}", msg, desc)
262262
}
263263

264-
pub(crate) fn cannot_assign(&self, span: Span, desc: &str) -> Diag<'tcx> {
264+
pub(crate) fn cannot_assign(&self, span: Span, desc: &str) -> Diag<'cx> {
265265
struct_span_code_err!(self.dcx(), span, E0594, "cannot assign to {}", desc)
266266
}
267267

268268
pub(crate) fn cannot_move_out_of(
269269
&self,
270270
move_from_span: Span,
271271
move_from_desc: &str,
272-
) -> Diag<'tcx> {
272+
) -> Diag<'cx> {
273273
struct_span_code_err!(
274274
self.dcx(),
275275
move_from_span,
@@ -287,7 +287,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
287287
move_from_span: Span,
288288
ty: Ty<'_>,
289289
is_index: Option<bool>,
290-
) -> Diag<'tcx> {
290+
) -> Diag<'cx> {
291291
let type_name = match (&ty.kind(), is_index) {
292292
(&ty::Array(_, _), Some(true)) | (&ty::Array(_, _), None) => "array",
293293
(&ty::Slice(_), _) => "slice",
@@ -308,7 +308,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
308308
&self,
309309
move_from_span: Span,
310310
container_ty: Ty<'_>,
311-
) -> Diag<'tcx> {
311+
) -> Diag<'cx> {
312312
struct_span_code_err!(
313313
self.dcx(),
314314
move_from_span,
@@ -325,7 +325,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
325325
verb: &str,
326326
optional_adverb_for_moved: &str,
327327
moved_path: Option<String>,
328-
) -> Diag<'tcx> {
328+
) -> Diag<'cx> {
329329
let moved_path = moved_path.map(|mp| format!(": `{mp}`")).unwrap_or_default();
330330

331331
struct_span_code_err!(
@@ -344,7 +344,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
344344
span: Span,
345345
path: &str,
346346
reason: &str,
347-
) -> Diag<'tcx> {
347+
) -> Diag<'cx> {
348348
struct_span_code_err!(
349349
self.dcx(),
350350
span,
@@ -362,7 +362,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
362362
immutable_place: &str,
363363
immutable_section: &str,
364364
action: &str,
365-
) -> Diag<'tcx> {
365+
) -> Diag<'cx> {
366366
struct_span_code_err!(
367367
self.dcx(),
368368
mutate_span,
@@ -380,7 +380,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
380380
&self,
381381
span: Span,
382382
yield_span: Span,
383-
) -> Diag<'tcx> {
383+
) -> Diag<'cx> {
384384
let coroutine_kind = self.body.coroutine.as_ref().unwrap().coroutine_kind;
385385
struct_span_code_err!(
386386
self.dcx(),
@@ -391,7 +391,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
391391
.with_span_label(yield_span, "possible yield occurs here")
392392
}
393393

394-
pub(crate) fn cannot_borrow_across_destructor(&self, borrow_span: Span) -> Diag<'tcx> {
394+
pub(crate) fn cannot_borrow_across_destructor(&self, borrow_span: Span) -> Diag<'cx> {
395395
struct_span_code_err!(
396396
self.dcx(),
397397
borrow_span,
@@ -400,7 +400,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
400400
)
401401
}
402402

403-
pub(crate) fn path_does_not_live_long_enough(&self, span: Span, path: &str) -> Diag<'tcx> {
403+
pub(crate) fn path_does_not_live_long_enough(&self, span: Span, path: &str) -> Diag<'cx> {
404404
struct_span_code_err!(self.dcx(), span, E0597, "{} does not live long enough", path,)
405405
}
406406

@@ -410,7 +410,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
410410
return_kind: &str,
411411
reference_desc: &str,
412412
path_desc: &str,
413-
) -> Diag<'tcx> {
413+
) -> Diag<'cx> {
414414
struct_span_code_err!(
415415
self.dcx(),
416416
span,
@@ -433,7 +433,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
433433
borrowed_path: &str,
434434
capture_span: Span,
435435
scope: &str,
436-
) -> Diag<'tcx> {
436+
) -> Diag<'cx> {
437437
struct_span_code_err!(
438438
self.dcx(),
439439
closure_span,
@@ -445,7 +445,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
445445
.with_span_label(closure_span, format!("may outlive borrowed value {borrowed_path}"))
446446
}
447447

448-
pub(crate) fn thread_local_value_does_not_live_long_enough(&self, span: Span) -> Diag<'tcx> {
448+
pub(crate) fn thread_local_value_does_not_live_long_enough(&self, span: Span) -> Diag<'cx> {
449449
struct_span_code_err!(
450450
self.dcx(),
451451
span,
@@ -454,7 +454,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
454454
)
455455
}
456456

457-
pub(crate) fn temporary_value_borrowed_for_too_long(&self, span: Span) -> Diag<'tcx> {
457+
pub(crate) fn temporary_value_borrowed_for_too_long(&self, span: Span) -> Diag<'cx> {
458458
struct_span_code_err!(self.dcx(), span, E0716, "temporary value dropped while borrowed",)
459459
}
460460
}

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ enum StorageDeadOrDrop<'tcx> {
7373
Destructor(Ty<'tcx>),
7474
}
7575

76-
impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
76+
impl<'cx, 'tcx> MirBorrowckCtxt<'_, '_, 'cx, 'tcx> {
7777
pub(crate) fn report_use_of_moved_or_uninitialized(
7878
&mut self,
7979
location: Location,
@@ -341,7 +341,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
341341
fn suggest_ref_or_clone(
342342
&self,
343343
mpi: MovePathIndex,
344-
err: &mut Diag<'tcx>,
344+
err: &mut Diag<'cx>,
345345
in_pattern: &mut bool,
346346
move_spans: UseSpans<'tcx>,
347347
) {
@@ -517,7 +517,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
517517
desired_action: InitializationRequiringAction,
518518
span: Span,
519519
use_spans: UseSpans<'tcx>,
520-
) -> Diag<'tcx> {
520+
) -> Diag<'cx> {
521521
// We need all statements in the body where the binding was assigned to later find all
522522
// the branching code paths where the binding *wasn't* assigned to.
523523
let inits = &self.move_data.init_path_map[mpi];
@@ -1441,7 +1441,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
14411441
location: Location,
14421442
(place, _span): (Place<'tcx>, Span),
14431443
borrow: &BorrowData<'tcx>,
1444-
) -> Diag<'tcx> {
1444+
) -> Diag<'cx> {
14451445
let borrow_spans = self.retrieve_borrow_spans(borrow);
14461446
let borrow_span = borrow_spans.args_or_use();
14471447

@@ -1491,7 +1491,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
14911491
(place, span): (Place<'tcx>, Span),
14921492
gen_borrow_kind: BorrowKind,
14931493
issued_borrow: &BorrowData<'tcx>,
1494-
) -> Diag<'tcx> {
1494+
) -> Diag<'cx> {
14951495
let issued_spans = self.retrieve_borrow_spans(issued_borrow);
14961496
let issued_span = issued_spans.args_or_use();
14971497

@@ -1782,7 +1782,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
17821782
err
17831783
}
17841784

1785-
fn suggest_copy_for_type_in_cloned_ref(&self, err: &mut Diag<'tcx>, place: Place<'tcx>) {
1785+
fn suggest_copy_for_type_in_cloned_ref(&self, err: &mut Diag<'cx>, place: Place<'tcx>) {
17861786
let tcx = self.infcx.tcx;
17871787
let hir = tcx.hir();
17881788
let Some(body_id) = tcx.hir_node(self.mir_hir_id()).body_id() else { return };
@@ -2841,7 +2841,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
28412841
drop_span: Span,
28422842
borrow_spans: UseSpans<'tcx>,
28432843
explanation: BorrowExplanation<'tcx>,
2844-
) -> Diag<'tcx> {
2844+
) -> Diag<'cx> {
28452845
debug!(
28462846
"report_local_value_does_not_live_long_enough(\
28472847
{:?}, {:?}, {:?}, {:?}, {:?}\
@@ -3016,7 +3016,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
30163016
&self,
30173017
drop_span: Span,
30183018
borrow_span: Span,
3019-
) -> Diag<'tcx> {
3019+
) -> Diag<'cx> {
30203020
debug!(
30213021
"report_thread_local_value_does_not_live_long_enough(\
30223022
{:?}, {:?}\
@@ -3041,7 +3041,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
30413041
borrow_spans: UseSpans<'tcx>,
30423042
proper_span: Span,
30433043
explanation: BorrowExplanation<'tcx>,
3044-
) -> Diag<'tcx> {
3044+
) -> Diag<'cx> {
30453045
if let BorrowExplanation::MustBeValidFor { category, span, from_closure: false, .. } =
30463046
explanation
30473047
{
@@ -3206,7 +3206,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
32063206
return_span: Span,
32073207
category: ConstraintCategory<'tcx>,
32083208
opt_place_desc: Option<&String>,
3209-
) -> Result<(), Diag<'tcx>> {
3209+
) -> Result<(), Diag<'cx>> {
32103210
let return_kind = match category {
32113211
ConstraintCategory::Return(_) => "return",
32123212
ConstraintCategory::Yield => "yield",
@@ -3299,7 +3299,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
32993299
constraint_span: Span,
33003300
captured_var: &str,
33013301
scope: &str,
3302-
) -> Diag<'tcx> {
3302+
) -> Diag<'cx> {
33033303
let tcx = self.infcx.tcx;
33043304
let args_span = use_span.args_or_use();
33053305

@@ -3411,7 +3411,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
34113411
upvar_span: Span,
34123412
upvar_name: Symbol,
34133413
escape_span: Span,
3414-
) -> Diag<'tcx> {
3414+
) -> Diag<'cx> {
34153415
let tcx = self.infcx.tcx;
34163416

34173417
let escapes_from = tcx.def_descr(self.mir_def_id().to_def_id());

compiler/rustc_borrowck/src/diagnostics/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub(super) struct DescribePlaceOpt {
6969

7070
pub(super) struct IncludingTupleField(pub(super) bool);
7171

72-
impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
72+
impl<'cx, 'tcx> MirBorrowckCtxt<'_, '_, 'cx, 'tcx> {
7373
/// Adds a suggestion when a closure is invoked twice with a moved variable or when a closure
7474
/// is moved after being invoked.
7575
///
@@ -86,7 +86,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
8686
&self,
8787
location: Location,
8888
place: PlaceRef<'tcx>,
89-
diag: &mut Diag<'_>,
89+
diag: &mut Diag<'cx>,
9090
) -> bool {
9191
debug!("add_moved_or_invoked_closure_note: location={:?} place={:?}", location, place);
9292
let mut target = place.local_or_deref_local();

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ enum GroupedMoveError<'tcx> {
9393
},
9494
}
9595

96-
impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
96+
impl<'cx, 'tcx> MirBorrowckCtxt<'_, '_, 'cx, 'tcx> {
9797
pub(crate) fn report_move_errors(&mut self) {
9898
let grouped_errors = self.group_move_errors();
9999
for error in grouped_errors {
@@ -291,7 +291,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
291291
self.buffer_error(err);
292292
}
293293

294-
fn report_cannot_move_from_static(&mut self, place: Place<'tcx>, span: Span) -> Diag<'tcx> {
294+
fn report_cannot_move_from_static(&mut self, place: Place<'tcx>, span: Span) -> Diag<'cx> {
295295
let description = if place.projection.len() == 1 {
296296
format!("static item {}", self.describe_any_place(place.as_ref()))
297297
} else {
@@ -428,7 +428,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
428428
deref_target_place: Place<'tcx>,
429429
span: Span,
430430
use_spans: Option<UseSpans<'tcx>>,
431-
) -> Diag<'tcx> {
431+
) -> Diag<'cx> {
432432
let tcx = self.infcx.tcx;
433433
// Inspect the type of the content behind the
434434
// borrow to provide feedback about why this

0 commit comments

Comments
 (0)