about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorPietro Albini <pietro@pietroalbini.org>2018-11-15 11:04:37 +0100
committerGitHub <noreply@github.com>2018-11-15 11:04:37 +0100
commit6ca7bc0eb8648abc8673a971c85b777a6bc62e16 (patch)
treee41d30f6c079806fa821aaa4bbddf74cb6fd9a5a /src/test
parent97d0d8964ecda2830d60f0c46c1ad8de24af539c (diff)
parentdd6398256ec0bde52722831d6b6cf604c9cdf1ed (diff)
downloadrust-6ca7bc0eb8648abc8673a971c85b777a6bc62e16.tar.gz
rust-6ca7bc0eb8648abc8673a971c85b777a6bc62e16.zip
Rollup merge of #55781 - pnkfelix:issue-54382-more-precise-spans-for-temps-and-their-drops, r=davidtwco
More precise spans for temps and their drops

This PR has two main enhancements:

 1. when possible during code generation for a statement (like `expr();`), pass along the span of a statement, and then attribute the drops of temporaries from that statement to the statement's end-point (which will be the semicolon if it is a statement that is terminating by a semicolon).
 2. when evaluating a block expression into a MIR temp, use the span of the block's tail expression (rather than the span of whole block including its statements and curly-braces) for the span of the temp.

Each of these individually increases the precision of our diagnostic output; together they combine to make a much clearer picture about the control flow through the spans.

Fix #54382
Diffstat (limited to 'src/test')
-rw-r--r--src/test/ui/nll/issue-54382-use-span-of-tail-of-block.nll.stderr20
-rw-r--r--src/test/ui/nll/issue-54382-use-span-of-tail-of-block.rs28
-rw-r--r--src/test/ui/nll/issue-54382-use-span-of-tail-of-block.stderr15
3 files changed, 63 insertions, 0 deletions
diff --git a/src/test/ui/nll/issue-54382-use-span-of-tail-of-block.nll.stderr b/src/test/ui/nll/issue-54382-use-span-of-tail-of-block.nll.stderr
new file mode 100644
index 00000000000..c308562c0cc
--- /dev/null
+++ b/src/test/ui/nll/issue-54382-use-span-of-tail-of-block.nll.stderr
@@ -0,0 +1,20 @@
+error[E0597]: `_thing1` does not live long enough
+  --> $DIR/issue-54382-use-span-of-tail-of-block.rs:7:29
+   |
+LL |             D("other").next(&_thing1)
+   |             ----------------^^^^^^^^-
+   |             |               |
+   |             |               borrowed value does not live long enough
+   |             a temporary with access to the borrow is created here ...
+LL |         }
+LL |     }
+   |     - `_thing1` dropped here while still borrowed
+LL | 
+LL |     ;
+   |     - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
+   |
+   = note: The temporary is part of an expression at the end of a block. Consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped.
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0597`.
diff --git a/src/test/ui/nll/issue-54382-use-span-of-tail-of-block.rs b/src/test/ui/nll/issue-54382-use-span-of-tail-of-block.rs
new file mode 100644
index 00000000000..99eafe0e9d1
--- /dev/null
+++ b/src/test/ui/nll/issue-54382-use-span-of-tail-of-block.rs
@@ -0,0 +1,28 @@
+fn main() {
+    {
+        let mut _thing1 = D(Box::new("thing1"));
+        {
+            let _thing2 = D("thing2");
+            side_effects();
+            D("other").next(&_thing1)
+        }
+    }
+
+    ;
+}
+
+#[derive(Debug)]
+struct D<T: std::fmt::Debug>(T);
+
+impl<T: std::fmt::Debug>  Drop for D<T> {
+    fn drop(&mut self) {
+        println!("dropping {:?})", self);
+    }
+}
+
+impl<T: std::fmt::Debug> D<T> {
+    fn next<U: std::fmt::Debug>(&self, _other: U) -> D<U> { D(_other) }
+    fn end(&self) { }
+}
+
+fn side_effects() { }
diff --git a/src/test/ui/nll/issue-54382-use-span-of-tail-of-block.stderr b/src/test/ui/nll/issue-54382-use-span-of-tail-of-block.stderr
new file mode 100644
index 00000000000..eeba7d6bb44
--- /dev/null
+++ b/src/test/ui/nll/issue-54382-use-span-of-tail-of-block.stderr
@@ -0,0 +1,15 @@
+error[E0597]: `_thing1` does not live long enough
+  --> $DIR/issue-54382-use-span-of-tail-of-block.rs:7:30
+   |
+LL |             D("other").next(&_thing1)
+   |                              ^^^^^^^ borrowed value does not live long enough
+LL |         }
+LL |     }
+   |     - `_thing1` dropped here while still borrowed
+LL | 
+LL |     ;
+   |     - borrowed value needs to live until here
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0597`.