Skip to content

Commit edfec66

Browse files
committed
Add new bckerr_code variant for my warning-cycled error for rust-lang#31567.
My new variant, for better or worse, is carrying data that needs the `'tcx` lifetime, so I am threading that onto `bckerr_code`, for better or worse. One might reasonably aruge that the `BckError` already carries a `mc::cmt<'tcx>`, but I claim that distinguishing the identified destructor type is important for the quality of the presented error message. (I am also passing along the identified `owner` lvalue, which is probably of more use for internal debugging and less for error reporting, but we may still want to try to work it into the user-facing message as well.)
1 parent a292d79 commit edfec66

File tree

1 file changed

+32
-4
lines changed
  • src/librustc_borrowck/borrowck

1 file changed

+32
-4
lines changed

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc::dep_graph::DepNode;
2424
use rustc::hir::map as hir_map;
2525
use rustc::hir::map::blocks::FnParts;
2626
use rustc::cfg;
27+
use rustc::lint;
2728
use rustc::middle::dataflow::DataFlowContext;
2829
use rustc::middle::dataflow::BitwiseOperator;
2930
use rustc::middle::dataflow::DataFlowOperator;
@@ -559,21 +560,27 @@ pub fn opt_loan_path<'tcx>(cmt: &mc::cmt<'tcx>) -> Option<Rc<LoanPath<'tcx>>> {
559560
// Errors
560561

561562
// Errors that can occur
562-
#[derive(PartialEq)]
563-
pub enum bckerr_code {
563+
#[derive(Clone, PartialEq, Debug)]
564+
pub enum bckerr_code<'tcx> {
564565
err_mutbl,
565566
err_out_of_scope(ty::Region, ty::Region), // superscope, subscope
567+
err_out_of_scope_dtor {
568+
superscope: ty::Region,
569+
subscope: ty::Region,
570+
owner: mc::cmt<'tcx>,
571+
dtor_ty: ty::Ty<'tcx>,
572+
},
566573
err_borrowed_pointer_too_short(ty::Region, ty::Region), // loan, ptr
567574
}
568575

569576
// Combination of an error code and the categorization of the expression
570577
// that caused it
571-
#[derive(PartialEq)]
578+
#[derive(Clone, PartialEq, Debug)]
572579
pub struct BckError<'tcx> {
573580
span: Span,
574581
cause: AliasableViolationKind,
575582
cmt: mc::cmt<'tcx>,
576-
code: bckerr_code
583+
code: bckerr_code<'tcx>,
577584
}
578585

579586
#[derive(Copy, Clone, Debug, PartialEq)]
@@ -610,6 +617,14 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
610617
pub fn report(&self, err: BckError<'tcx>) {
611618
// Catch and handle some particular cases.
612619
match (&err.code, &err.cause) {
620+
(&err_out_of_scope_dtor { .. }, _cause) => {
621+
// Issue #31567: remove special case code post warning cycle
622+
self.tcx.sess.add_lint(lint::builtin::BORROW_OUTLIVES_OWNER_WITH_DTOR,
623+
err.cmt.id,
624+
err.span,
625+
self.bckerr_to_string(&err));
626+
return;
627+
}
613628
(&err_out_of_scope(ty::ReScope(_), ty::ReStatic),
614629
&BorrowViolation(euv::ClosureCapture(span))) |
615630
(&err_out_of_scope(ty::ReScope(_), ty::ReFree(..)),
@@ -893,6 +908,18 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
893908
}
894909
}
895910
}
911+
err_out_of_scope_dtor { ref dtor_ty, .. } => {
912+
let msg = match opt_loan_path(&err.cmt) {
913+
None => "borrowed value".to_string(),
914+
Some(lp) => {
915+
format!("`{}`", self.loan_path_to_string(&lp))
916+
}
917+
};
918+
format!("owner of {} does not live long enough; \
919+
it carries a type {} with a destructor \
920+
capable of modifying the borrowed data",
921+
msg, dtor_ty)
922+
}
896923
err_out_of_scope(..) => {
897924
let msg = match opt_loan_path(&err.cmt) {
898925
None => "borrowed value".to_string(),
@@ -1052,6 +1079,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10521079
}
10531080
}
10541081

1082+
err_out_of_scope_dtor { superscope: super_scope, subscope: sub_scope, .. } |
10551083
err_out_of_scope(super_scope, sub_scope) => {
10561084
self.tcx.note_and_explain_region(
10571085
db,

0 commit comments

Comments
 (0)