Skip to content

Commit

Permalink
add some sample UI error test cases
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikomatsakis committed Feb 17, 2017
1 parent dc0bb3f commit 5167ac8
Show file tree
Hide file tree
Showing 14 changed files with 243 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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() { }
Original file line number Diff line number Diff line change
@@ -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

15 changes: 15 additions & 0 deletions src/test/ui/lifetime-errors/ex1b-return-no-names-if-else.rs
Original file line number Diff line number Diff line change
@@ -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() { }
10 changes: 10 additions & 0 deletions src/test/ui/lifetime-errors/ex1b-return-no-names-if-else.stderr
Original file line number Diff line number Diff line change
@@ -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

19 changes: 19 additions & 0 deletions src/test/ui/lifetime-errors/ex2a-push-one-existing-name.rs
Original file line number Diff line number Diff line change
@@ -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() { }
27 changes: 27 additions & 0 deletions src/test/ui/lifetime-errors/ex2a-push-one-existing-name.stderr
Original file line number Diff line number Diff line change
@@ -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

19 changes: 19 additions & 0 deletions src/test/ui/lifetime-errors/ex2b-push-no-existing-names.rs
Original file line number Diff line number Diff line change
@@ -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() { }
27 changes: 27 additions & 0 deletions src/test/ui/lifetime-errors/ex2b-push-no-existing-names.stderr
Original file line number Diff line number Diff line change
@@ -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

20 changes: 20 additions & 0 deletions src/test/ui/lifetime-errors/ex2c-push-inference-variable.rs
Original file line number Diff line number Diff line change
@@ -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() { }
Original file line number Diff line number Diff line change
@@ -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

21 changes: 21 additions & 0 deletions src/test/ui/lifetime-errors/ex2d-push-inference-variable-2.rs
Original file line number Diff line number Diff line change
@@ -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() { }
Original file line number Diff line number Diff line change
@@ -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

21 changes: 21 additions & 0 deletions src/test/ui/lifetime-errors/ex2e-push-inference-variable-3.rs
Original file line number Diff line number Diff line change
@@ -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() { }
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5167ac8

Please sign in to comment.