Skip to content

Commit 5480b47

Browse files
committed
Auto merge of #62629 - matthewjasper:cleanup-borrowck-errors, r=petrochenkov
Cleanup borrowck errors This removes some of the unnecessary code that allowed sharing error reporting between two borrow checkers. closes #59193
2 parents 9bb855c + 9709b73 commit 5480b47

File tree

8 files changed

+128
-322
lines changed

8 files changed

+128
-322
lines changed

src/librustc_mir/borrow_check/conflict_errors.rs

+27-53
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use super::{InitializationRequiringAction, PrefixSet};
2222
use super::error_reporting::{IncludingDowncast, UseSpans};
2323
use crate::dataflow::drop_flag_effects;
2424
use crate::dataflow::indexes::{MovePathIndex, MoveOutIndex};
25-
use crate::util::borrowck_errors::{BorrowckErrors, Origin};
25+
use crate::util::borrowck_errors;
2626

2727
#[derive(Debug)]
2828
struct MoveSite {
@@ -89,12 +89,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
8989
Some(name) => format!("`{}`", name),
9090
None => "value".to_owned(),
9191
};
92-
let mut err = self.infcx.tcx.cannot_act_on_uninitialized_variable(
92+
let mut err = self.cannot_act_on_uninitialized_variable(
9393
span,
9494
desired_action.as_noun(),
9595
&self.describe_place_with_options(moved_place, IncludingDowncast(true))
9696
.unwrap_or_else(|| "_".to_owned()),
97-
Origin::Mir,
9897
);
9998
err.span_label(span, format!("use of possibly uninitialized {}", item_msg));
10099

@@ -120,12 +119,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
120119

121120
let msg = ""; //FIXME: add "partially " or "collaterally "
122121

123-
let mut err = self.infcx.tcx.cannot_act_on_moved_value(
122+
let mut err = self.cannot_act_on_moved_value(
124123
span,
125124
desired_action.as_noun(),
126125
msg,
127126
self.describe_place_with_options(&moved_place, IncludingDowncast(true)),
128-
Origin::Mir,
129127
);
130128

131129
self.add_moved_or_invoked_closure_note(
@@ -267,7 +265,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
267265
"report_move_out_while_borrowed: location={:?} place={:?} span={:?} borrow={:?}",
268266
location, place, span, borrow
269267
);
270-
let tcx = self.infcx.tcx;
271268
let value_msg = match self.describe_place(place) {
272269
Some(name) => format!("`{}`", name),
273270
None => "value".to_owned(),
@@ -283,10 +280,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
283280
let move_spans = self.move_spans(place, location);
284281
let span = move_spans.args_or_use();
285282

286-
let mut err = tcx.cannot_move_when_borrowed(
283+
let mut err = self.cannot_move_when_borrowed(
287284
span,
288285
&self.describe_place(place).unwrap_or_else(|| "_".to_owned()),
289-
Origin::Mir,
290286
);
291287
err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_msg));
292288
err.span_label(span, format!("move out of {} occurs here", value_msg));
@@ -315,8 +311,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
315311
(place, _span): (&Place<'tcx>, Span),
316312
borrow: &BorrowData<'tcx>,
317313
) -> DiagnosticBuilder<'cx> {
318-
let tcx = self.infcx.tcx;
319-
320314
let borrow_spans = self.retrieve_borrow_spans(borrow);
321315
let borrow_span = borrow_spans.args_or_use();
322316

@@ -325,13 +319,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
325319
let use_spans = self.move_spans(place, location);
326320
let span = use_spans.var_or_use();
327321

328-
let mut err = tcx.cannot_use_when_mutably_borrowed(
322+
let mut err = self.cannot_use_when_mutably_borrowed(
329323
span,
330324
&self.describe_place(place).unwrap_or_else(|| "_".to_owned()),
331325
borrow_span,
332326
&self.describe_place(&borrow.borrowed_place)
333327
.unwrap_or_else(|| "_".to_owned()),
334-
Origin::Mir,
335328
);
336329

337330
borrow_spans.var_span_label(&mut err, {
@@ -376,7 +369,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
376369
};
377370

378371
// FIXME: supply non-"" `opt_via` when appropriate
379-
let tcx = self.infcx.tcx;
380372
let first_borrow_desc;
381373
let mut err = match (
382374
gen_borrow_kind,
@@ -388,7 +380,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
388380
) {
389381
(BorrowKind::Shared, lft, _, BorrowKind::Mut { .. }, _, rgt) => {
390382
first_borrow_desc = "mutable ";
391-
tcx.cannot_reborrow_already_borrowed(
383+
self.cannot_reborrow_already_borrowed(
392384
span,
393385
&desc_place,
394386
&msg_place,
@@ -398,12 +390,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
398390
rgt,
399391
&msg_borrow,
400392
None,
401-
Origin::Mir,
402393
)
403394
}
404395
(BorrowKind::Mut { .. }, _, lft, BorrowKind::Shared, rgt, _) => {
405396
first_borrow_desc = "immutable ";
406-
tcx.cannot_reborrow_already_borrowed(
397+
self.cannot_reborrow_already_borrowed(
407398
span,
408399
&desc_place,
409400
&msg_place,
@@ -413,42 +404,38 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
413404
rgt,
414405
&msg_borrow,
415406
None,
416-
Origin::Mir,
417407
)
418408
}
419409

420410
(BorrowKind::Mut { .. }, _, _, BorrowKind::Mut { .. }, _, _) => {
421411
first_borrow_desc = "first ";
422-
tcx.cannot_mutably_borrow_multiply(
412+
self.cannot_mutably_borrow_multiply(
423413
span,
424414
&desc_place,
425415
&msg_place,
426416
issued_span,
427417
&msg_borrow,
428418
None,
429-
Origin::Mir,
430419
)
431420
}
432421

433422
(BorrowKind::Unique, _, _, BorrowKind::Unique, _, _) => {
434423
first_borrow_desc = "first ";
435-
tcx.cannot_uniquely_borrow_by_two_closures(
424+
self.cannot_uniquely_borrow_by_two_closures(
436425
span,
437426
&desc_place,
438427
issued_span,
439428
None,
440-
Origin::Mir,
441429
)
442430
}
443431

444432
(BorrowKind::Mut { .. }, _, _, BorrowKind::Shallow, _, _)
445433
| (BorrowKind::Unique, _, _, BorrowKind::Shallow, _, _) => {
446-
let mut err = tcx.cannot_mutate_in_match_guard(
434+
let mut err = self.cannot_mutate_in_match_guard(
447435
span,
448436
issued_span,
449437
&desc_place,
450438
"mutably borrow",
451-
Origin::Mir,
452439
);
453440
borrow_spans.var_span_label(
454441
&mut err,
@@ -462,7 +449,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
462449

463450
(BorrowKind::Unique, _, _, _, _, _) => {
464451
first_borrow_desc = "first ";
465-
tcx.cannot_uniquely_borrow_by_one_closure(
452+
self.cannot_uniquely_borrow_by_one_closure(
466453
span,
467454
container_name,
468455
&desc_place,
@@ -471,13 +458,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
471458
"it",
472459
"",
473460
None,
474-
Origin::Mir,
475461
)
476462
},
477463

478464
(BorrowKind::Shared, lft, _, BorrowKind::Unique, _, _) => {
479465
first_borrow_desc = "first ";
480-
tcx.cannot_reborrow_already_uniquely_borrowed(
466+
self.cannot_reborrow_already_uniquely_borrowed(
481467
span,
482468
container_name,
483469
&desc_place,
@@ -487,13 +473,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
487473
"",
488474
None,
489475
second_borrow_desc,
490-
Origin::Mir,
491476
)
492477
}
493478

494479
(BorrowKind::Mut { .. }, _, lft, BorrowKind::Unique, _, _) => {
495480
first_borrow_desc = "first ";
496-
tcx.cannot_reborrow_already_uniquely_borrowed(
481+
self.cannot_reborrow_already_uniquely_borrowed(
497482
span,
498483
container_name,
499484
&desc_place,
@@ -503,7 +488,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
503488
"",
504489
None,
505490
second_borrow_desc,
506-
Origin::Mir,
507491
)
508492
}
509493

@@ -833,10 +817,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
833817
}
834818
}
835819

836-
let mut err = self.infcx.tcx.path_does_not_live_long_enough(
820+
let mut err = self.path_does_not_live_long_enough(
837821
borrow_span,
838822
&format!("`{}`", name),
839-
Origin::Mir,
840823
);
841824

842825
if let Some(annotation) = self.annotate_argument_and_return_for_borrow(borrow) {
@@ -925,9 +908,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
925908
let borrow_spans = self.retrieve_borrow_spans(borrow);
926909
let borrow_span = borrow_spans.var_or_use();
927910

928-
let mut err = self.infcx
929-
.tcx
930-
.cannot_borrow_across_destructor(borrow_span, Origin::Mir);
911+
let mut err = self.cannot_borrow_across_destructor(borrow_span);
931912

932913
let what_was_dropped = match self.describe_place(place) {
933914
Some(name) => format!("`{}`", name.as_str()),
@@ -978,9 +959,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
978959
drop_span, borrow_span
979960
);
980961

981-
let mut err = self.infcx
982-
.tcx
983-
.thread_local_value_does_not_live_long_enough(borrow_span, Origin::Mir);
962+
let mut err = self.thread_local_value_does_not_live_long_enough(borrow_span);
984963

985964
err.span_label(
986965
borrow_span,
@@ -1024,8 +1003,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10241003
}
10251004
}
10261005

1027-
let tcx = self.infcx.tcx;
1028-
let mut err = tcx.temporary_value_borrowed_for_too_long(proper_span, Origin::Mir);
1006+
let mut err = self.temporary_value_borrowed_for_too_long(proper_span);
10291007
err.span_label(
10301008
proper_span,
10311009
"creates a temporary which is freed while still in use",
@@ -1068,8 +1046,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10681046
category: ConstraintCategory,
10691047
opt_place_desc: Option<&String>,
10701048
) -> Option<DiagnosticBuilder<'cx>> {
1071-
let tcx = self.infcx.tcx;
1072-
10731049
let return_kind = match category {
10741050
ConstraintCategory::Return => "return",
10751051
ConstraintCategory::Yield => "yield",
@@ -1132,12 +1108,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11321108
}
11331109
};
11341110

1135-
let mut err = tcx.cannot_return_reference_to_local(
1111+
let mut err = self.cannot_return_reference_to_local(
11361112
return_span,
11371113
return_kind,
11381114
reference_desc,
11391115
&place_desc,
1140-
Origin::Mir,
11411116
);
11421117

11431118
if return_span != borrow_span {
@@ -1158,11 +1133,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11581133
) -> DiagnosticBuilder<'cx> {
11591134
let tcx = self.infcx.tcx;
11601135

1161-
let mut err = tcx.cannot_capture_in_long_lived_closure(
1136+
let mut err = self.cannot_capture_in_long_lived_closure(
11621137
args_span,
11631138
captured_var,
11641139
var_span,
1165-
Origin::Mir,
11661140
);
11671141

11681142
let suggestion = match tcx.sess.source_map().span_to_snippet(args_span) {
@@ -1218,7 +1192,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12181192
"function"
12191193
};
12201194

1221-
let mut err = tcx.borrowed_data_escapes_closure(escape_span, escapes_from, Origin::Mir);
1195+
let mut err = borrowck_errors::borrowed_data_escapes_closure(
1196+
tcx,
1197+
escape_span,
1198+
escapes_from,
1199+
);
12221200

12231201
err.span_label(
12241202
upvar_span,
@@ -1360,14 +1338,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13601338
let loan_spans = self.retrieve_borrow_spans(loan);
13611339
let loan_span = loan_spans.args_or_use();
13621340

1363-
let tcx = self.infcx.tcx;
13641341
if loan.kind == BorrowKind::Shallow {
1365-
let mut err = tcx.cannot_mutate_in_match_guard(
1342+
let mut err = self.cannot_mutate_in_match_guard(
13661343
span,
13671344
loan_span,
13681345
&self.describe_place(place).unwrap_or_else(|| "_".to_owned()),
13691346
"assign",
1370-
Origin::Mir,
13711347
);
13721348
loan_spans.var_span_label(
13731349
&mut err,
@@ -1379,11 +1355,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13791355
return;
13801356
}
13811357

1382-
let mut err = tcx.cannot_assign_to_borrowed(
1358+
let mut err = self.cannot_assign_to_borrowed(
13831359
span,
13841360
loan_span,
13851361
&self.describe_place(place).unwrap_or_else(|| "_".to_owned()),
1386-
Origin::Mir,
13871362
);
13881363

13891364
loan_spans.var_span_label(
@@ -1444,11 +1419,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14441419
Some(decl) => (self.describe_place(err_place), decl.source_info.span),
14451420
};
14461421

1447-
let mut err = self.infcx.tcx.cannot_reassign_immutable(
1422+
let mut err = self.cannot_reassign_immutable(
14481423
span,
14491424
place_description.as_ref().map(AsRef::as_ref).unwrap_or("_"),
14501425
from_arg,
1451-
Origin::Mir,
14521426
);
14531427
let msg = if from_arg {
14541428
"cannot assign to immutable argument"

src/librustc_mir/borrow_check/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ use crate::dataflow::MoveDataParamEnv;
4141
use crate::dataflow::{do_dataflow, DebugFormatted};
4242
use crate::dataflow::EverInitializedPlaces;
4343
use crate::dataflow::{MaybeInitializedPlaces, MaybeUninitializedPlaces};
44-
use crate::util::borrowck_errors::{BorrowckErrors, Origin};
4544

4645
use self::borrow_set::{BorrowData, BorrowSet};
4746
use self::flows::Flows;
@@ -422,8 +421,8 @@ fn downgrade_if_error(diag: &mut Diagnostic) {
422421
}
423422
}
424423

425-
pub struct MirBorrowckCtxt<'cx, 'tcx> {
426-
infcx: &'cx InferCtxt<'cx, 'tcx>,
424+
crate struct MirBorrowckCtxt<'cx, 'tcx> {
425+
crate infcx: &'cx InferCtxt<'cx, 'tcx>,
427426
body: &'cx Body<'tcx>,
428427
mir_def_id: DefId,
429428
move_data: &'cx MoveData<'tcx>,
@@ -1499,11 +1498,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14991498
debug!("check_for_local_borrow({:?})", borrow);
15001499

15011500
if borrow_of_local_data(&borrow.borrowed_place) {
1502-
let err = self.infcx.tcx
1503-
.cannot_borrow_across_generator_yield(
1501+
let err = self.cannot_borrow_across_generator_yield(
15041502
self.retrieve_borrow_spans(borrow).var_or_use(),
15051503
yield_span,
1506-
Origin::Mir,
15071504
);
15081505

15091506
err.buffer(&mut self.errors_buffer);

0 commit comments

Comments
 (0)