From f4fc43cb20a09e5db76f355596205c48cd8b4615 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 13 Jun 2018 14:51:53 -0300 Subject: [PATCH] Suggest that values are dropped in the opposite order they are defined --- src/librustc/mir/mod.rs | 13 ++++++++++ .../borrow_check/error_reporting.rs | 23 ++++++++++------- src/librustc_mir/borrow_check/mod.rs | 6 +++-- .../borrow_check/nll/explain_borrow/mod.rs | 25 +++++++++++++++++-- .../dropck-eyepatch-extern-crate.nll.stderr | 2 ++ .../dropck/dropck-eyepatch-reorder.nll.stderr | 2 ++ src/test/ui/dropck/dropck-eyepatch.nll.stderr | 2 ++ src/test/ui/error-codes/E0597.nll.stderr | 2 ++ src/test/ui/generator/dropck.nll.stderr | 4 +++ .../ui/span/dropck-object-cycle.nll.stderr | 2 ++ .../dropck_direct_cycle_with_drop.nll.stderr | 4 +++ .../ui/span/dropck_misc_variants.nll.stderr | 4 +++ ...opck-child-has-items-via-parent.nll.stderr | 2 ++ ...ue-24805-dropck-trait-has-items.nll.stderr | 6 +++++ .../issue-24895-copy-clone-dropck.nll.stderr | 2 ++ src/test/ui/span/issue-26656.nll.stderr | 2 ++ .../ui/span/issue28498-reject-ex1.nll.stderr | 2 ++ ...ssue28498-reject-lifetime-param.nll.stderr | 2 ++ .../issue28498-reject-passed-to-fn.nll.stderr | 2 ++ .../issue28498-reject-trait-bound.nll.stderr | 2 ++ .../send-is-not-static-std-sync.nll.stderr | 2 ++ .../vec_refs_data_with_early_death.nll.stderr | 4 +++ 22 files changed, 102 insertions(+), 13 deletions(-) diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index 20a0df2a171..b4a4e812e03 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -302,6 +302,19 @@ impl<'tcx> Mir<'tcx> { } } + /// Check if `sub` is a sub scope of `sup` + pub fn is_sub_scope(&self, mut sub: SourceScope, sup: SourceScope) -> bool { + loop { + if sub == sup { + return true; + } + match self.source_scopes[sub].parent_scope { + None => return false, + Some(p) => sub = p, + } + } + } + /// Return the return type, it always return first element from `local_decls` array pub fn return_ty(&self) -> Ty<'tcx> { self.local_decls[RETURN_PLACE].ty diff --git a/src/librustc_mir/borrow_check/error_reporting.rs b/src/librustc_mir/borrow_check/error_reporting.rs index 38d1ac2cb4a..2460c213759 100644 --- a/src/librustc_mir/borrow_check/error_reporting.rs +++ b/src/librustc_mir/borrow_check/error_reporting.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use borrow_check::WriteKind; use syntax_pos::Span; use rustc::middle::region::ScopeTree; use rustc::mir::{BorrowKind, Field, Local, LocalKind, Location, Operand}; @@ -162,7 +163,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { format!("borrow of {} occurs here", borrow_msg), ); err.span_label(span, format!("move out of {} occurs here", value_msg)); - self.explain_why_borrow_contains_point(context, borrow, &mut err); + self.explain_why_borrow_contains_point(context, borrow, None, &mut err); err.emit(); } @@ -182,7 +183,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { Origin::Mir, ); - self.explain_why_borrow_contains_point(context, borrow, &mut err); + self.explain_why_borrow_contains_point(context, borrow, None, &mut err); err.emit(); } @@ -380,7 +381,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { ); } - self.explain_why_borrow_contains_point(context, issued_borrow, &mut err); + self.explain_why_borrow_contains_point(context, issued_borrow, None, &mut err); err.emit(); } @@ -389,8 +390,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { &mut self, context: Context, borrow: &BorrowData<'tcx>, - drop_span: Span, + place_span: (&Place<'tcx>, Span), + kind: Option, ) { + let drop_span = place_span.1; let scope_tree = self.tcx.region_scope_tree(self.mir_def_id); let root_place = self.prefixes(&borrow.borrowed_place, PrefixSet::All) .last() @@ -450,6 +453,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { drop_span, borrow_span, proper_span, + kind.map(|k| (k, place_span.0)), ); } (RegionKind::ReEarlyBound(_), None) @@ -495,7 +499,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { drop_span, format!("`{}` dropped here while still borrowed", name), ); - self.explain_why_borrow_contains_point(context, borrow, &mut err); + self.explain_why_borrow_contains_point(context, borrow, None, &mut err); err.emit(); } @@ -517,7 +521,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { "temporary value dropped here while still borrowed", ); err.note("consider using a `let` binding to increase its lifetime"); - self.explain_why_borrow_contains_point(context, borrow, &mut err); + self.explain_why_borrow_contains_point(context, borrow, None, &mut err); err.emit(); } @@ -530,6 +534,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { drop_span: Span, borrow_span: Span, _proper_span: Span, + kind_place: Option<(WriteKind, &Place<'tcx>)>, ) { debug!( "report_unscoped_local_value_does_not_live_long_enough(\ @@ -544,7 +549,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { err.span_label(borrow_span, "borrowed value does not live long enough"); err.span_label(drop_span, "borrowed value only lives until here"); - self.explain_why_borrow_contains_point(context, borrow, &mut err); + self.explain_why_borrow_contains_point(context, borrow, kind_place, &mut err); err.emit(); } @@ -570,7 +575,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { err.span_label(proper_span, "temporary value does not live long enough"); err.span_label(drop_span, "temporary value only lives until here"); - self.explain_why_borrow_contains_point(context, borrow, &mut err); + self.explain_why_borrow_contains_point(context, borrow, None, &mut err); err.emit(); } @@ -588,7 +593,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { Origin::Mir, ); - self.explain_why_borrow_contains_point(context, loan, &mut err); + self.explain_why_borrow_contains_point(context, loan, None, &mut err); err.emit(); } diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs index 122b2df4766..3d3275a6b17 100644 --- a/src/librustc_mir/borrow_check/mod.rs +++ b/src/librustc_mir/borrow_check/mod.rs @@ -1050,7 +1050,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { this.report_borrowed_value_does_not_live_long_enough( context, borrow, - place_span.1, + place_span, + Some(kind), ); } WriteKind::Mutate => { @@ -1328,7 +1329,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { self.report_borrowed_value_does_not_live_long_enough( context, borrow, - span, + (place, span), + None, ) } } diff --git a/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs b/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs index 2807a4e8857..d43bc239806 100644 --- a/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs +++ b/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs @@ -9,10 +9,10 @@ // except according to those terms. use borrow_check::nll::region_infer::{Cause, RegionInferenceContext}; -use borrow_check::{Context, MirBorrowckCtxt}; +use borrow_check::{Context, MirBorrowckCtxt, WriteKind}; use borrow_check::borrow_set::BorrowData; use rustc::mir::visit::{MirVisitable, PlaceContext, Visitor}; -use rustc::mir::{Local, Location, Mir}; +use rustc::mir::{Local, Location, Mir, Place}; use rustc_data_structures::fx::FxHashSet; use rustc_errors::DiagnosticBuilder; use util::liveness::{self, DefUse, LivenessMode}; @@ -22,11 +22,21 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { /// point from `context`. This is key for the "3-point errors" /// [described in the NLL RFC][d]. /// + /// # Parameters + /// + /// - `borrow`: the borrow in question + /// - `context`: where the borrow occurs + /// - `kind_place`: if Some, this describes the statement that triggered the error. + /// - first half is the kind of write, if any, being performed + /// - second half is the place being accessed + /// - `err`: where the error annotations are going to be added + /// /// [d]: https://rust-lang.github.io/rfcs/2094-nll.html#leveraging-intuition-framing-errors-in-terms-of-points pub(in borrow_check) fn explain_why_borrow_contains_point( &mut self, context: Context, borrow: &BorrowData<'tcx>, + kind_place: Option<(WriteKind, &Place<'tcx>)>, err: &mut DiagnosticBuilder<'_>, ) { let regioncx = &&self.nonlexical_regioncx; @@ -64,6 +74,17 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { local_name ), ); + + if let Some((WriteKind::StorageDeadOrDrop, place)) = kind_place { + if let Place::Local(borrowed_local) = place { + let dropped_local_scope = mir.local_decls[local].visibility_scope; + let borrowed_local_scope = mir.local_decls[*borrowed_local].visibility_scope; + + if mir.is_sub_scope(borrowed_local_scope, dropped_local_scope) { + err.note("values in a scope are dropped in the opposite order they are defined"); + } + } + } } None => { err.span_label( diff --git a/src/test/ui/dropck/dropck-eyepatch-extern-crate.nll.stderr b/src/test/ui/dropck/dropck-eyepatch-extern-crate.nll.stderr index 008ecfeabcb..730ace7858b 100644 --- a/src/test/ui/dropck/dropck-eyepatch-extern-crate.nll.stderr +++ b/src/test/ui/dropck/dropck-eyepatch-extern-crate.nll.stderr @@ -9,6 +9,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `dt` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to previous error diff --git a/src/test/ui/dropck/dropck-eyepatch-reorder.nll.stderr b/src/test/ui/dropck/dropck-eyepatch-reorder.nll.stderr index 233ff198af4..f0df7109ba4 100644 --- a/src/test/ui/dropck/dropck-eyepatch-reorder.nll.stderr +++ b/src/test/ui/dropck/dropck-eyepatch-reorder.nll.stderr @@ -9,6 +9,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `dt` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to previous error diff --git a/src/test/ui/dropck/dropck-eyepatch.nll.stderr b/src/test/ui/dropck/dropck-eyepatch.nll.stderr index 7b27ff01f92..2072bc6a756 100644 --- a/src/test/ui/dropck/dropck-eyepatch.nll.stderr +++ b/src/test/ui/dropck/dropck-eyepatch.nll.stderr @@ -9,6 +9,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `dt` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to previous error diff --git a/src/test/ui/error-codes/E0597.nll.stderr b/src/test/ui/error-codes/E0597.nll.stderr index 459de1bc00b..172db9ced82 100644 --- a/src/test/ui/error-codes/E0597.nll.stderr +++ b/src/test/ui/error-codes/E0597.nll.stderr @@ -9,6 +9,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `x` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to previous error diff --git a/src/test/ui/generator/dropck.nll.stderr b/src/test/ui/generator/dropck.nll.stderr index 7b68c167d4a..01a53cdd6dd 100644 --- a/src/test/ui/generator/dropck.nll.stderr +++ b/src/test/ui/generator/dropck.nll.stderr @@ -9,6 +9,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `gen` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error[E0597]: `ref_` does not live long enough --> $DIR/dropck.rs:22:11 @@ -26,6 +28,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `gen` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to 2 previous errors diff --git a/src/test/ui/span/dropck-object-cycle.nll.stderr b/src/test/ui/span/dropck-object-cycle.nll.stderr index 64a76399da3..b268f6e3ab0 100644 --- a/src/test/ui/span/dropck-object-cycle.nll.stderr +++ b/src/test/ui/span/dropck-object-cycle.nll.stderr @@ -9,6 +9,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `m` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to previous error diff --git a/src/test/ui/span/dropck_direct_cycle_with_drop.nll.stderr b/src/test/ui/span/dropck_direct_cycle_with_drop.nll.stderr index 2a8ef24307d..86a4f0e13b5 100644 --- a/src/test/ui/span/dropck_direct_cycle_with_drop.nll.stderr +++ b/src/test/ui/span/dropck_direct_cycle_with_drop.nll.stderr @@ -9,6 +9,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `d1` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error[E0597]: `d2` does not live long enough --> $DIR/dropck_direct_cycle_with_drop.rs:46:19 @@ -21,6 +23,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `d1` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to 2 previous errors diff --git a/src/test/ui/span/dropck_misc_variants.nll.stderr b/src/test/ui/span/dropck_misc_variants.nll.stderr index 0eeec8e51be..0eb84e4a7d0 100644 --- a/src/test/ui/span/dropck_misc_variants.nll.stderr +++ b/src/test/ui/span/dropck_misc_variants.nll.stderr @@ -8,6 +8,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `_w` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error[E0597]: `v` does not live long enough --> $DIR/dropck_misc_variants.rs:41:27 @@ -20,6 +22,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `_w` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to 2 previous errors diff --git a/src/test/ui/span/issue-24805-dropck-child-has-items-via-parent.nll.stderr b/src/test/ui/span/issue-24805-dropck-child-has-items-via-parent.nll.stderr index 3f18f4d025a..ec1df8aeca1 100644 --- a/src/test/ui/span/issue-24805-dropck-child-has-items-via-parent.nll.stderr +++ b/src/test/ui/span/issue-24805-dropck-child-has-items-via-parent.nll.stderr @@ -9,6 +9,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `_d` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to previous error diff --git a/src/test/ui/span/issue-24805-dropck-trait-has-items.nll.stderr b/src/test/ui/span/issue-24805-dropck-trait-has-items.nll.stderr index 79e607bb392..288c196b27b 100644 --- a/src/test/ui/span/issue-24805-dropck-trait-has-items.nll.stderr +++ b/src/test/ui/span/issue-24805-dropck-trait-has-items.nll.stderr @@ -8,6 +8,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `_d` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error[E0597]: `d1` does not live long enough --> $DIR/issue-24805-dropck-trait-has-items.rs:53:33 @@ -19,6 +21,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `_d` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error[E0597]: `d1` does not live long enough --> $DIR/issue-24805-dropck-trait-has-items.rs:59:20 @@ -30,6 +34,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `_d` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to 3 previous errors diff --git a/src/test/ui/span/issue-24895-copy-clone-dropck.nll.stderr b/src/test/ui/span/issue-24895-copy-clone-dropck.nll.stderr index 550f9d8b60d..c89e68bcd0a 100644 --- a/src/test/ui/span/issue-24895-copy-clone-dropck.nll.stderr +++ b/src/test/ui/span/issue-24895-copy-clone-dropck.nll.stderr @@ -8,6 +8,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `d2` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to previous error diff --git a/src/test/ui/span/issue-26656.nll.stderr b/src/test/ui/span/issue-26656.nll.stderr index 64bb83fc782..bea6dc6b3f6 100644 --- a/src/test/ui/span/issue-26656.nll.stderr +++ b/src/test/ui/span/issue-26656.nll.stderr @@ -8,6 +8,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `zook` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to previous error diff --git a/src/test/ui/span/issue28498-reject-ex1.nll.stderr b/src/test/ui/span/issue28498-reject-ex1.nll.stderr index c5e7a93c763..3ed13f2653c 100644 --- a/src/test/ui/span/issue28498-reject-ex1.nll.stderr +++ b/src/test/ui/span/issue28498-reject-ex1.nll.stderr @@ -9,6 +9,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `foo` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to previous error diff --git a/src/test/ui/span/issue28498-reject-lifetime-param.nll.stderr b/src/test/ui/span/issue28498-reject-lifetime-param.nll.stderr index 25708219bd1..ce9e75b4483 100644 --- a/src/test/ui/span/issue28498-reject-lifetime-param.nll.stderr +++ b/src/test/ui/span/issue28498-reject-lifetime-param.nll.stderr @@ -9,6 +9,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `foo1` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to previous error diff --git a/src/test/ui/span/issue28498-reject-passed-to-fn.nll.stderr b/src/test/ui/span/issue28498-reject-passed-to-fn.nll.stderr index 66157096394..fcdc1c3f945 100644 --- a/src/test/ui/span/issue28498-reject-passed-to-fn.nll.stderr +++ b/src/test/ui/span/issue28498-reject-passed-to-fn.nll.stderr @@ -9,6 +9,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `foo1` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to previous error diff --git a/src/test/ui/span/issue28498-reject-trait-bound.nll.stderr b/src/test/ui/span/issue28498-reject-trait-bound.nll.stderr index bb9d67983de..6300bb3eacd 100644 --- a/src/test/ui/span/issue28498-reject-trait-bound.nll.stderr +++ b/src/test/ui/span/issue28498-reject-trait-bound.nll.stderr @@ -9,6 +9,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `foo1` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to previous error diff --git a/src/test/ui/span/send-is-not-static-std-sync.nll.stderr b/src/test/ui/span/send-is-not-static-std-sync.nll.stderr index d411f0d26f4..6ebbf3e1ec4 100644 --- a/src/test/ui/span/send-is-not-static-std-sync.nll.stderr +++ b/src/test/ui/span/send-is-not-static-std-sync.nll.stderr @@ -63,6 +63,8 @@ LL | } ... LL | } | - borrow later used here, when `tx` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to 6 previous errors diff --git a/src/test/ui/span/vec_refs_data_with_early_death.nll.stderr b/src/test/ui/span/vec_refs_data_with_early_death.nll.stderr index 6074199b89a..9d89fac08ff 100644 --- a/src/test/ui/span/vec_refs_data_with_early_death.nll.stderr +++ b/src/test/ui/span/vec_refs_data_with_early_death.nll.stderr @@ -9,6 +9,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `v` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error[E0597]: `x` does not live long enough --> $DIR/vec_refs_data_with_early_death.rs:27:12 @@ -21,6 +23,8 @@ LL | } | | | borrowed value only lives until here | borrow later used here, when `v` is dropped + | + = note: values in a scope are dropped in the opposite order they are defined error: aborting due to 2 previous errors