]> git.lizzy.rs Git - rust.git/commitdiff
add some sample UI error test cases
authorNiko Matsakis <niko@alum.mit.edu>
Fri, 17 Feb 2017 15:32:25 +0000 (10:32 -0500)
committerNiko Matsakis <niko@alum.mit.edu>
Fri, 17 Feb 2017 16:01:35 +0000 (11:01 -0500)
These are some samples that I have been focusing on improving over
time. In this PR, I mainly want to stem the bleeding where we in some
cases we show an error that gives you no possible way to divine the
problem.

14 files changed:
src/test/ui/lifetime-errors/ex1-return-one-existing-name-if-else.rs [new file with mode: 0644]
src/test/ui/lifetime-errors/ex1-return-one-existing-name-if-else.stderr [new file with mode: 0644]
src/test/ui/lifetime-errors/ex1b-return-no-names-if-else.rs [new file with mode: 0644]
src/test/ui/lifetime-errors/ex1b-return-no-names-if-else.stderr [new file with mode: 0644]
src/test/ui/lifetime-errors/ex2a-push-one-existing-name.rs [new file with mode: 0644]
src/test/ui/lifetime-errors/ex2a-push-one-existing-name.stderr [new file with mode: 0644]
src/test/ui/lifetime-errors/ex2b-push-no-existing-names.rs [new file with mode: 0644]
src/test/ui/lifetime-errors/ex2b-push-no-existing-names.stderr [new file with mode: 0644]
src/test/ui/lifetime-errors/ex2c-push-inference-variable.rs [new file with mode: 0644]
src/test/ui/lifetime-errors/ex2c-push-inference-variable.stderr [new file with mode: 0644]
src/test/ui/lifetime-errors/ex2d-push-inference-variable-2.rs [new file with mode: 0644]
src/test/ui/lifetime-errors/ex2d-push-inference-variable-2.stderr [new file with mode: 0644]
src/test/ui/lifetime-errors/ex2e-push-inference-variable-3.rs [new file with mode: 0644]
src/test/ui/lifetime-errors/ex2e-push-inference-variable-3.stderr [new file with mode: 0644]

diff --git a/src/test/ui/lifetime-errors/ex1-return-one-existing-name-if-else.rs b/src/test/ui/lifetime-errors/ex1-return-one-existing-name-if-else.rs
new file mode 100644 (file)
index 0000000..30239f4
--- /dev/null
@@ -0,0 +1,15 @@
+// Copyright 2016 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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 {
+    if x > y { x } else { y }
+}
+
+fn main() { }
diff --git a/src/test/ui/lifetime-errors/ex1-return-one-existing-name-if-else.stderr b/src/test/ui/lifetime-errors/ex1-return-one-existing-name-if-else.stderr
new file mode 100644 (file)
index 0000000..85e0542
--- /dev/null
@@ -0,0 +1,25 @@
+error[E0312]: lifetime of reference outlives lifetime of borrowed content...
+  --> $DIR/ex1-return-one-existing-name-if-else.rs:12:27
+   |
+12 |     if x > y { x } else { y }
+   |                           ^
+   |
+note: ...the reference is valid for the lifetime 'a as defined on the body at 11:43...
+  --> $DIR/ex1-return-one-existing-name-if-else.rs:11:44
+   |
+11 |   fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 {
+   |  ____________________________________________^ starting here...
+12 | |     if x > y { x } else { y }
+13 | | }
+   | |_^ ...ending here
+note: ...but the borrowed content is only valid for the anonymous lifetime #1 defined on the body at 11:43
+  --> $DIR/ex1-return-one-existing-name-if-else.rs:11:44
+   |
+11 |   fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 {
+   |  ____________________________________________^ starting here...
+12 | |     if x > y { x } else { y }
+13 | | }
+   | |_^ ...ending here
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/lifetime-errors/ex1b-return-no-names-if-else.rs b/src/test/ui/lifetime-errors/ex1b-return-no-names-if-else.rs
new file mode 100644 (file)
index 0000000..098950e
--- /dev/null
@@ -0,0 +1,15 @@
+// Copyright 2016 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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn foo(x: &i32, y: &i32) -> &i32 {
+    if x > y { x } else { y }
+}
+
+fn main() { }
diff --git a/src/test/ui/lifetime-errors/ex1b-return-no-names-if-else.stderr b/src/test/ui/lifetime-errors/ex1b-return-no-names-if-else.stderr
new file mode 100644 (file)
index 0000000..fccc44c
--- /dev/null
@@ -0,0 +1,10 @@
+error[E0106]: missing lifetime specifier
+  --> $DIR/ex1b-return-no-names-if-else.rs:11:29
+   |
+11 | fn foo(x: &i32, y: &i32) -> &i32 {
+   |                             ^ expected lifetime parameter
+   |
+   = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `x` or `y`
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/lifetime-errors/ex2a-push-one-existing-name.rs b/src/test/ui/lifetime-errors/ex2a-push-one-existing-name.rs
new file mode 100644 (file)
index 0000000..71a1c86
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 2016 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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+struct Ref<'a, T: 'a> {
+    data: &'a T
+}
+
+fn foo<'a>(x: &mut Vec<Ref<'a, i32>>, y: Ref<i32>) {
+    x.push(y);
+}
+
+fn main() { }
diff --git a/src/test/ui/lifetime-errors/ex2a-push-one-existing-name.stderr b/src/test/ui/lifetime-errors/ex2a-push-one-existing-name.stderr
new file mode 100644 (file)
index 0000000..6f42a9f
--- /dev/null
@@ -0,0 +1,27 @@
+error[E0308]: mismatched types
+  --> $DIR/ex2a-push-one-existing-name.rs:16:12
+   |
+16 |     x.push(y);
+   |            ^ lifetime mismatch
+   |
+   = note: expected type `Ref<'a, i32>`
+              found type `Ref<'_, i32>`
+note: the anonymous lifetime #2 defined on the body at 15:51...
+  --> $DIR/ex2a-push-one-existing-name.rs:15:52
+   |
+15 |   fn foo<'a>(x: &mut Vec<Ref<'a, i32>>, y: Ref<i32>) {
+   |  ____________________________________________________^ starting here...
+16 | |     x.push(y);
+17 | | }
+   | |_^ ...ending here
+note: ...does not necessarily outlive the lifetime 'a as defined on the body at 15:51
+  --> $DIR/ex2a-push-one-existing-name.rs:15:52
+   |
+15 |   fn foo<'a>(x: &mut Vec<Ref<'a, i32>>, y: Ref<i32>) {
+   |  ____________________________________________________^ starting here...
+16 | |     x.push(y);
+17 | | }
+   | |_^ ...ending here
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/lifetime-errors/ex2b-push-no-existing-names.rs b/src/test/ui/lifetime-errors/ex2b-push-no-existing-names.rs
new file mode 100644 (file)
index 0000000..09038d8
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 2016 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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+struct Ref<'a, T: 'a> {
+    data: &'a T
+}
+
+fn foo(x: &mut Vec<Ref<i32>>, y: Ref<i32>) {
+    x.push(y);
+}
+
+fn main() { }
diff --git a/src/test/ui/lifetime-errors/ex2b-push-no-existing-names.stderr b/src/test/ui/lifetime-errors/ex2b-push-no-existing-names.stderr
new file mode 100644 (file)
index 0000000..edc1c23
--- /dev/null
@@ -0,0 +1,27 @@
+error[E0308]: mismatched types
+  --> $DIR/ex2b-push-no-existing-names.rs:16:12
+   |
+16 |     x.push(y);
+   |            ^ lifetime mismatch
+   |
+   = note: expected type `Ref<'_, i32>`
+              found type `Ref<'_, i32>`
+note: the anonymous lifetime #3 defined on the body at 15:43...
+  --> $DIR/ex2b-push-no-existing-names.rs:15:44
+   |
+15 |   fn foo(x: &mut Vec<Ref<i32>>, y: Ref<i32>) {
+   |  ____________________________________________^ starting here...
+16 | |     x.push(y);
+17 | | }
+   | |_^ ...ending here
+note: ...does not necessarily outlive the anonymous lifetime #2 defined on the body at 15:43
+  --> $DIR/ex2b-push-no-existing-names.rs:15:44
+   |
+15 |   fn foo(x: &mut Vec<Ref<i32>>, y: Ref<i32>) {
+   |  ____________________________________________^ starting here...
+16 | |     x.push(y);
+17 | | }
+   | |_^ ...ending here
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/lifetime-errors/ex2c-push-inference-variable.rs b/src/test/ui/lifetime-errors/ex2c-push-inference-variable.rs
new file mode 100644 (file)
index 0000000..cb083f7
--- /dev/null
@@ -0,0 +1,20 @@
+// Copyright 2016 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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+struct Ref<'a, T: 'a> {
+    data: &'a T
+}
+
+fn foo<'a, 'b, 'c>(x: &'a mut Vec<Ref<'b, i32>>, y: Ref<'c, i32>) {
+    let z = Ref { data: y.data };
+    x.push(z);
+}
+
+fn main() { }
diff --git a/src/test/ui/lifetime-errors/ex2c-push-inference-variable.stderr b/src/test/ui/lifetime-errors/ex2c-push-inference-variable.stderr
new file mode 100644 (file)
index 0000000..e34d5f0
--- /dev/null
@@ -0,0 +1,8 @@
+error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
+  --> $DIR/ex2c-push-inference-variable.rs:16:13
+   |
+16 |     let z = Ref { data: y.data };
+   |             ^^^
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/lifetime-errors/ex2d-push-inference-variable-2.rs b/src/test/ui/lifetime-errors/ex2d-push-inference-variable-2.rs
new file mode 100644 (file)
index 0000000..bcb7583
--- /dev/null
@@ -0,0 +1,21 @@
+// Copyright 2016 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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+struct Ref<'a, T: 'a> {
+    data: &'a T
+}
+
+fn foo<'a, 'b, 'c>(x: &'a mut Vec<Ref<'b, i32>>, y: Ref<'c, i32>) {
+    let a: &mut Vec<Ref<i32>> = x;
+    let b = Ref { data: y.data };
+    a.push(b);
+}
+
+fn main() { }
diff --git a/src/test/ui/lifetime-errors/ex2d-push-inference-variable-2.stderr b/src/test/ui/lifetime-errors/ex2d-push-inference-variable-2.stderr
new file mode 100644 (file)
index 0000000..f0799a1
--- /dev/null
@@ -0,0 +1,8 @@
+error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
+  --> $DIR/ex2d-push-inference-variable-2.rs:17:13
+   |
+17 |     let b = Ref { data: y.data };
+   |             ^^^
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/lifetime-errors/ex2e-push-inference-variable-3.rs b/src/test/ui/lifetime-errors/ex2e-push-inference-variable-3.rs
new file mode 100644 (file)
index 0000000..2d05adb
--- /dev/null
@@ -0,0 +1,21 @@
+// Copyright 2016 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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+struct Ref<'a, T: 'a> {
+    data: &'a T
+}
+
+fn foo<'a, 'b, 'c>(x: &'a mut Vec<Ref<'b, i32>>, y: Ref<'c, i32>) {
+    let a: &mut Vec<Ref<i32>> = x;
+    let b = Ref { data: y.data };
+    Vec::push(a, b);
+}
+
+fn main() { }
diff --git a/src/test/ui/lifetime-errors/ex2e-push-inference-variable-3.stderr b/src/test/ui/lifetime-errors/ex2e-push-inference-variable-3.stderr
new file mode 100644 (file)
index 0000000..c479be8
--- /dev/null
@@ -0,0 +1,8 @@
+error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
+  --> $DIR/ex2e-push-inference-variable-3.rs:17:13
+   |
+17 |     let b = Ref { data: y.data };
+   |             ^^^
+
+error: aborting due to previous error
+