Skip to content

Commit 5167ac8

Browse files
committed
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.
1 parent dc0bb3f commit 5167ac8

14 files changed

+243
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 {
12+
if x > y { x } else { y }
13+
}
14+
15+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0312]: lifetime of reference outlives lifetime of borrowed content...
2+
--> $DIR/ex1-return-one-existing-name-if-else.rs:12:27
3+
|
4+
12 | if x > y { x } else { y }
5+
| ^
6+
|
7+
note: ...the reference is valid for the lifetime 'a as defined on the body at 11:43...
8+
--> $DIR/ex1-return-one-existing-name-if-else.rs:11:44
9+
|
10+
11 | fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 {
11+
| ____________________________________________^ starting here...
12+
12 | | if x > y { x } else { y }
13+
13 | | }
14+
| |_^ ...ending here
15+
note: ...but the borrowed content is only valid for the anonymous lifetime #1 defined on the body at 11:43
16+
--> $DIR/ex1-return-one-existing-name-if-else.rs:11:44
17+
|
18+
11 | fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 {
19+
| ____________________________________________^ starting here...
20+
12 | | if x > y { x } else { y }
21+
13 | | }
22+
| |_^ ...ending here
23+
24+
error: aborting due to previous error
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn foo(x: &i32, y: &i32) -> &i32 {
12+
if x > y { x } else { y }
13+
}
14+
15+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error[E0106]: missing lifetime specifier
2+
--> $DIR/ex1b-return-no-names-if-else.rs:11:29
3+
|
4+
11 | fn foo(x: &i32, y: &i32) -> &i32 {
5+
| ^ expected lifetime parameter
6+
|
7+
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `x` or `y`
8+
9+
error: aborting due to previous error
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Ref<'a, T: 'a> {
12+
data: &'a T
13+
}
14+
15+
fn foo<'a>(x: &mut Vec<Ref<'a, i32>>, y: Ref<i32>) {
16+
x.push(y);
17+
}
18+
19+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/ex2a-push-one-existing-name.rs:16:12
3+
|
4+
16 | x.push(y);
5+
| ^ lifetime mismatch
6+
|
7+
= note: expected type `Ref<'a, i32>`
8+
found type `Ref<'_, i32>`
9+
note: the anonymous lifetime #2 defined on the body at 15:51...
10+
--> $DIR/ex2a-push-one-existing-name.rs:15:52
11+
|
12+
15 | fn foo<'a>(x: &mut Vec<Ref<'a, i32>>, y: Ref<i32>) {
13+
| ____________________________________________________^ starting here...
14+
16 | | x.push(y);
15+
17 | | }
16+
| |_^ ...ending here
17+
note: ...does not necessarily outlive the lifetime 'a as defined on the body at 15:51
18+
--> $DIR/ex2a-push-one-existing-name.rs:15:52
19+
|
20+
15 | fn foo<'a>(x: &mut Vec<Ref<'a, i32>>, y: Ref<i32>) {
21+
| ____________________________________________________^ starting here...
22+
16 | | x.push(y);
23+
17 | | }
24+
| |_^ ...ending here
25+
26+
error: aborting due to previous error
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Ref<'a, T: 'a> {
12+
data: &'a T
13+
}
14+
15+
fn foo(x: &mut Vec<Ref<i32>>, y: Ref<i32>) {
16+
x.push(y);
17+
}
18+
19+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/ex2b-push-no-existing-names.rs:16:12
3+
|
4+
16 | x.push(y);
5+
| ^ lifetime mismatch
6+
|
7+
= note: expected type `Ref<'_, i32>`
8+
found type `Ref<'_, i32>`
9+
note: the anonymous lifetime #3 defined on the body at 15:43...
10+
--> $DIR/ex2b-push-no-existing-names.rs:15:44
11+
|
12+
15 | fn foo(x: &mut Vec<Ref<i32>>, y: Ref<i32>) {
13+
| ____________________________________________^ starting here...
14+
16 | | x.push(y);
15+
17 | | }
16+
| |_^ ...ending here
17+
note: ...does not necessarily outlive the anonymous lifetime #2 defined on the body at 15:43
18+
--> $DIR/ex2b-push-no-existing-names.rs:15:44
19+
|
20+
15 | fn foo(x: &mut Vec<Ref<i32>>, y: Ref<i32>) {
21+
| ____________________________________________^ starting here...
22+
16 | | x.push(y);
23+
17 | | }
24+
| |_^ ...ending here
25+
26+
error: aborting due to previous error
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Ref<'a, T: 'a> {
12+
data: &'a T
13+
}
14+
15+
fn foo<'a, 'b, 'c>(x: &'a mut Vec<Ref<'b, i32>>, y: Ref<'c, i32>) {
16+
let z = Ref { data: y.data };
17+
x.push(z);
18+
}
19+
20+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
2+
--> $DIR/ex2c-push-inference-variable.rs:16:13
3+
|
4+
16 | let z = Ref { data: y.data };
5+
| ^^^
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Ref<'a, T: 'a> {
12+
data: &'a T
13+
}
14+
15+
fn foo<'a, 'b, 'c>(x: &'a mut Vec<Ref<'b, i32>>, y: Ref<'c, i32>) {
16+
let a: &mut Vec<Ref<i32>> = x;
17+
let b = Ref { data: y.data };
18+
a.push(b);
19+
}
20+
21+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
2+
--> $DIR/ex2d-push-inference-variable-2.rs:17:13
3+
|
4+
17 | let b = Ref { data: y.data };
5+
| ^^^
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Ref<'a, T: 'a> {
12+
data: &'a T
13+
}
14+
15+
fn foo<'a, 'b, 'c>(x: &'a mut Vec<Ref<'b, i32>>, y: Ref<'c, i32>) {
16+
let a: &mut Vec<Ref<i32>> = x;
17+
let b = Ref { data: y.data };
18+
Vec::push(a, b);
19+
}
20+
21+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
2+
--> $DIR/ex2e-push-inference-variable-3.rs:17:13
3+
|
4+
17 | let b = Ref { data: y.data };
5+
| ^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)