diff --git a/src/librustc_mir/borrow_check/error_reporting.rs b/src/librustc_mir/borrow_check/error_reporting.rs index 34551e8e76f59..9d8b2b709b7d6 100644 --- a/src/librustc_mir/borrow_check/error_reporting.rs +++ b/src/librustc_mir/borrow_check/error_reporting.rs @@ -497,14 +497,34 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { _proper_span: Span, _end_span: Option, ) { + debug!( + "report_unscoped_local_value_does_not_live_long_enough(\ + {:?}, {:?}, {:?}, {:?}, {:?}, {:?}\ + )", + context, + name, + scope_tree, + borrow, + drop_span, + borrow_span + ); + let mut err = self.tcx.path_does_not_live_long_enough(borrow_span, &format!("`{}`", name), Origin::Mir); err.span_label(borrow_span, "borrowed value does not live long enough"); err.span_label(drop_span, "borrowed value only lives until here"); - self.tcx.note_and_explain_region(scope_tree, &mut err, - "borrowed value must be valid for ", - borrow.region, "..."); + + if !self.tcx.sess.nll() { + self.tcx.note_and_explain_region( + scope_tree, + &mut err, + "borrowed value must be valid for ", + borrow.region, + "...", + ); + } + self.explain_why_borrow_contains_point(context, borrow, &mut err); err.emit(); } @@ -519,14 +539,33 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { proper_span: Span, _end_span: Option ) { + debug!( + "report_unscoped_temporary_value_does_not_live_long_enough(\ + {:?}, {:?}, {:?}, {:?}, {:?}\ + )", + context, + scope_tree, + borrow, + drop_span, + proper_span + ); + let mut err = self.tcx.path_does_not_live_long_enough(proper_span, "borrowed value", Origin::Mir); err.span_label(proper_span, "temporary value does not live long enough"); err.span_label(drop_span, "temporary value only lives until here"); - self.tcx.note_and_explain_region(scope_tree, &mut err, - "borrowed value must be valid for ", - borrow.region, "..."); + + if !self.tcx.sess.nll() { + self.tcx.note_and_explain_region( + scope_tree, + &mut err, + "borrowed value must be valid for ", + borrow.region, + "...", + ); + } + self.explain_why_borrow_contains_point(context, borrow, &mut err); err.emit(); } diff --git a/src/test/ui/nll/borrowed-local-error.rs b/src/test/ui/nll/borrowed-local-error.rs new file mode 100644 index 0000000000000..785a38da95980 --- /dev/null +++ b/src/test/ui/nll/borrowed-local-error.rs @@ -0,0 +1,26 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: -Znll-dump-cause + +#![feature(nll)] + +fn gimme(x: &(u32,)) -> &u32 { + &x.0 +} + +fn main() { + let x = gimme({ + let v = (22,); + &v + //~^ ERROR `v` does not live long enough [E0597] + }); + println!("{:?}", x); +} diff --git a/src/test/ui/nll/borrowed-local-error.stderr b/src/test/ui/nll/borrowed-local-error.stderr new file mode 100644 index 0000000000000..3bc1978554821 --- /dev/null +++ b/src/test/ui/nll/borrowed-local-error.stderr @@ -0,0 +1,17 @@ +error[E0597]: `v` does not live long enough + --> $DIR/borrowed-local-error.rs:22:9 + | +LL | let x = gimme({ + | _____________- +LL | | let v = (22,); +LL | | &v + | | ^^ borrowed value does not live long enough +LL | | //~^ ERROR `v` does not live long enough [E0597] +LL | | }); + | |_____-- borrow later used here + | | + | borrowed value only lives until here + +error: aborting due to previous error + +If you want more information on this error, try using "rustc --explain E0597" diff --git a/src/test/ui/nll/borrowed-temporary-error.rs b/src/test/ui/nll/borrowed-temporary-error.rs new file mode 100644 index 0000000000000..e1a6112d173f9 --- /dev/null +++ b/src/test/ui/nll/borrowed-temporary-error.rs @@ -0,0 +1,26 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: -Znll-dump-cause + +#![feature(nll)] + +fn gimme(x: &(u32,)) -> &u32 { + &x.0 +} + +fn main() { + let x = gimme({ + let v = 22; + &(v,) + //~^ ERROR borrowed value does not live long enough [E0597] + }); + println!("{:?}", x); +} diff --git a/src/test/ui/nll/borrowed-temporary-error.stderr b/src/test/ui/nll/borrowed-temporary-error.stderr new file mode 100644 index 0000000000000..f5cb1dccc3786 --- /dev/null +++ b/src/test/ui/nll/borrowed-temporary-error.stderr @@ -0,0 +1,14 @@ +error[E0597]: borrowed value does not live long enough + --> $DIR/borrowed-temporary-error.rs:22:10 + | +LL | &(v,) + | ^^^^ temporary value does not live long enough +LL | //~^ ERROR borrowed value does not live long enough [E0597] +LL | }); + | - temporary value only lives until here +LL | println!("{:?}", x); + | - borrow later used here + +error: aborting due to previous error + +If you want more information on this error, try using "rustc --explain E0597"