diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2017-02-17 10:32:25 -0500 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2017-02-17 11:01:35 -0500 |
| commit | 5167ac87199cbf58d9cc46e3ba34dd3d75c88e4c (patch) | |
| tree | b6125521b19a548388cf8cac9dd09cc254ea7d01 | |
| parent | dc0bb3f2839c13ab42feacd423f728fbfd2f2f7a (diff) | |
| download | rust-5167ac87199cbf58d9cc46e3ba34dd3d75c88e4c.tar.gz rust-5167ac87199cbf58d9cc46e3ba34dd3d75c88e4c.zip | |
add some sample UI error test cases
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, 243 insertions, 0 deletions
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 index 00000000000..30239f4c094 --- /dev/null +++ b/src/test/ui/lifetime-errors/ex1-return-one-existing-name-if-else.rs @@ -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 index 00000000000..85e05422ab3 --- /dev/null +++ b/src/test/ui/lifetime-errors/ex1-return-one-existing-name-if-else.stderr @@ -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 index 00000000000..098950e13b3 --- /dev/null +++ b/src/test/ui/lifetime-errors/ex1b-return-no-names-if-else.rs @@ -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 index 00000000000..fccc44caac8 --- /dev/null +++ b/src/test/ui/lifetime-errors/ex1b-return-no-names-if-else.stderr @@ -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 index 00000000000..71a1c865e09 --- /dev/null +++ b/src/test/ui/lifetime-errors/ex2a-push-one-existing-name.rs @@ -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 index 00000000000..6f42a9f679a --- /dev/null +++ b/src/test/ui/lifetime-errors/ex2a-push-one-existing-name.stderr @@ -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 index 00000000000..09038d8ce90 --- /dev/null +++ b/src/test/ui/lifetime-errors/ex2b-push-no-existing-names.rs @@ -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 index 00000000000..edc1c2362de --- /dev/null +++ b/src/test/ui/lifetime-errors/ex2b-push-no-existing-names.stderr @@ -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 index 00000000000..cb083f778de --- /dev/null +++ b/src/test/ui/lifetime-errors/ex2c-push-inference-variable.rs @@ -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 index 00000000000..e34d5f0a2f3 --- /dev/null +++ b/src/test/ui/lifetime-errors/ex2c-push-inference-variable.stderr @@ -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 index 00000000000..bcb7583beef --- /dev/null +++ b/src/test/ui/lifetime-errors/ex2d-push-inference-variable-2.rs @@ -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 index 00000000000..f0799a17a46 --- /dev/null +++ b/src/test/ui/lifetime-errors/ex2d-push-inference-variable-2.stderr @@ -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 index 00000000000..2d05adb7ecd --- /dev/null +++ b/src/test/ui/lifetime-errors/ex2e-push-inference-variable-3.rs @@ -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 index 00000000000..c479be88b13 --- /dev/null +++ b/src/test/ui/lifetime-errors/ex2e-push-inference-variable-3.stderr @@ -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 + |
