Skip to content

Commit 9cf0ff2

Browse files
committed
use helper function for error reporting
1 parent b3a47d9 commit 9cf0ff2

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,32 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14671467

14681468
/// Reports StorageDeadOrDrop of `place` conflicts with `borrow`.
14691469
///
1470+
/// Depending on the origin of the StorageDeadOrDrop, this may be
1471+
/// reported as either a drop or an illegal mutation of a borrowed value.
1472+
/// The latter is preferred when the this is a drop triggered by a
1473+
/// reassignment, as it's more user friendly to report a problem with the
1474+
/// explicit assignment than the implicit drop.
1475+
#[instrument(level = "debug", skip(self))]
1476+
pub(crate) fn report_storage_dead_or_drop_of_borrowed(
1477+
&mut self,
1478+
location: Location,
1479+
place_span: (Place<'tcx>, Span),
1480+
borrow: &BorrowData<'tcx>,
1481+
) {
1482+
// It's sufficient to check the last desugaring as Replace is the last
1483+
// one to be applied.
1484+
if let Some(DesugaringKind::Replace) = place_span.1.desugaring_kind() {
1485+
self.report_illegal_mutation_of_borrowed(location, place_span, borrow)
1486+
} else {
1487+
self.report_borrowed_value_does_not_live_long_enough(
1488+
location,
1489+
borrow,
1490+
place_span,
1491+
Some(WriteKind::StorageDeadOrDrop),
1492+
)
1493+
}
1494+
}
1495+
14701496
/// This means that some data referenced by `borrow` needs to live
14711497
/// past the point where the StorageDeadOrDrop of `place` occurs.
14721498
/// This is usually interpreted as meaning that `place` has too

compiler/rustc_borrowck/src/lib.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use rustc_middle::mir::{InlineAsmOperand, Terminator, TerminatorKind};
4040
use rustc_middle::ty::query::Providers;
4141
use rustc_middle::ty::{self, CapturedPlace, ParamEnv, RegionVid, TyCtxt};
4242
use rustc_session::lint::builtin::UNUSED_MUT;
43-
use rustc_span::{DesugaringKind, Span, Symbol};
43+
use rustc_span::{Span, Symbol};
4444

4545
use either::Either;
4646
use smallvec::SmallVec;
@@ -1184,22 +1184,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11841184
this.report_conflicting_borrow(location, place_span, bk, borrow);
11851185
this.buffer_error(err);
11861186
}
1187-
WriteKind::StorageDeadOrDrop => {
1188-
if let Some(DesugaringKind::Replace) = place_span.1.desugaring_kind() {
1189-
// If this is a drop triggered by a reassignment, it's more user friendly
1190-
// to report a problem with the explicit assignment than the implicit drop.
1191-
this.report_illegal_mutation_of_borrowed(
1192-
location, place_span, borrow,
1193-
)
1194-
} else {
1195-
this.report_borrowed_value_does_not_live_long_enough(
1196-
location,
1197-
borrow,
1198-
place_span,
1199-
Some(kind),
1200-
)
1201-
}
1202-
}
1187+
WriteKind::StorageDeadOrDrop => this
1188+
.report_storage_dead_or_drop_of_borrowed(location, place_span, borrow),
12031189
WriteKind::Mutate => {
12041190
this.report_illegal_mutation_of_borrowed(location, place_span, borrow)
12051191
}

0 commit comments

Comments
 (0)