Skip to content

Commit a871053

Browse files
committed
expand the patterns test with a bunch more scenarios
1 parent 2f6628e commit a871053

File tree

2 files changed

+172
-5
lines changed

2 files changed

+172
-5
lines changed

src/test/ui/nll/user-annotations/patterns.rs

+79-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,23 @@
22

33
#![feature(nll)]
44

5-
fn main() {
5+
fn variable_no_initializer() {
6+
// FIXME: It is unclear to me whether this should be an error or not.
7+
8+
let x = 22;
9+
let y: &'static u32;
10+
y = &x;
11+
}
12+
13+
fn variable_with_initializer() {
14+
let x = 22;
15+
let y: &'static u32 = &x; //~ ERROR
16+
}
17+
18+
fn underscore_with_initializer() {
19+
let x = 22;
20+
let _: &'static u32 = &x; //~ ERROR
21+
622
let _: Vec<&'static String> = vec![&String::new()];
723
//~^ ERROR borrowed value does not live long enough [E0597]
824

@@ -12,3 +28,65 @@ fn main() {
1228
let (_a, b): (Vec<&'static String>, _) = (vec![&String::new()], 44);
1329
//~^ ERROR borrowed value does not live long enough [E0597]
1430
}
31+
32+
fn pair_underscores_with_initializer() {
33+
let x = 22;
34+
let (_, _): (&'static u32, u32) = (&x, 44); //~ ERROR
35+
}
36+
37+
fn pair_variable_with_initializer() {
38+
let x = 22;
39+
let (y, _): (&'static u32, u32) = (&x, 44); //~ ERROR
40+
}
41+
42+
struct Single<T> { value: T }
43+
44+
fn struct_single_field_variable_with_initializer() {
45+
let x = 22;
46+
let Single { value: y }: Single<&'static u32> = Single { value: &x }; //~ ERROR
47+
}
48+
49+
fn struct_single_field_underscore_with_initializer() {
50+
let x = 22;
51+
let Single { value: _ }: Single<&'static u32> = Single { value: &x }; //~ ERROR
52+
}
53+
54+
struct Double<T> { value1: T, value2: T }
55+
56+
fn struct_double_field_underscore_with_initializer() {
57+
let x = 22;
58+
let Double { value1: _, value2: _ }: Double<&'static u32> = Double {
59+
value1: &x, //~ ERROR
60+
value2: &44,
61+
};
62+
}
63+
64+
fn static_to_a_to_static_through_variable<'a>(x: &'a u32) -> &'static u32 {
65+
// The error in this test is inconsistency with
66+
// `static_to_a_to_static_through_tuple`, but "feels right" to
67+
// me. It occurs because we special case the single binding case
68+
// and force the type of `y` to be `&'a u32`, even though the
69+
// right-hand side has type `&'static u32`.
70+
71+
let y: &'a u32 = &22;
72+
y //~ ERROR
73+
}
74+
75+
fn static_to_a_to_static_through_tuple<'a>(x: &'a u32) -> &'static u32 {
76+
// FIXME: The fact that this type-checks is perhaps surprising.
77+
// What happens is that the right-hand side is constrained to have
78+
// type `&'a u32`, which is possible, because it has type
79+
// `&'static u32`. The variable `y` is then forced to have type
80+
// `&'static u32`, but it is constrained only by the right-hand
81+
// side, not the ascribed type, and hence it passes.
82+
83+
let (y, _z): (&'a u32, u32) = (&22, 44);
84+
y
85+
}
86+
87+
fn a_to_static_then_static<'a>(x: &'a u32) -> &'static u32 {
88+
let (y, _z): (&'static u32, u32) = (x, 44); //~ ERROR
89+
y
90+
}
91+
92+
fn main() { }

src/test/ui/nll/user-annotations/patterns.stderr

+93-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1+
error[E0597]: `x` does not live long enough
2+
--> $DIR/patterns.rs:15:27
3+
|
4+
LL | let y: &'static u32 = &x; //~ ERROR
5+
| ^^ borrowed value does not live long enough
6+
LL | }
7+
| - `x` dropped here while still borrowed
8+
|
9+
= note: borrowed value must be valid for the static lifetime...
10+
11+
error[E0597]: `x` does not live long enough
12+
--> $DIR/patterns.rs:20:27
13+
|
14+
LL | let _: &'static u32 = &x; //~ ERROR
15+
| ^^ borrowed value does not live long enough
16+
...
17+
LL | }
18+
| - `x` dropped here while still borrowed
19+
|
20+
= note: borrowed value must be valid for the static lifetime...
21+
122
error[E0597]: borrowed value does not live long enough
2-
--> $DIR/patterns.rs:6:41
23+
--> $DIR/patterns.rs:22:41
324
|
425
LL | let _: Vec<&'static String> = vec![&String::new()];
526
| ^^^^^^^^^^^^^ - temporary value only lives until here
@@ -9,7 +30,7 @@ LL | let _: Vec<&'static String> = vec![&String::new()];
930
= note: borrowed value must be valid for the static lifetime...
1031

1132
error[E0597]: borrowed value does not live long enough
12-
--> $DIR/patterns.rs:9:52
33+
--> $DIR/patterns.rs:25:52
1334
|
1435
LL | let (_, a): (Vec<&'static String>, _) = (vec![&String::new()], 44);
1536
| ^^^^^^^^^^^^^ - temporary value only lives until here
@@ -19,7 +40,7 @@ LL | let (_, a): (Vec<&'static String>, _) = (vec![&String::new()], 44);
1940
= note: borrowed value must be valid for the static lifetime...
2041

2142
error[E0597]: borrowed value does not live long enough
22-
--> $DIR/patterns.rs:12:53
43+
--> $DIR/patterns.rs:28:53
2344
|
2445
LL | let (_a, b): (Vec<&'static String>, _) = (vec![&String::new()], 44);
2546
| ^^^^^^^^^^^^^ - temporary value only lives until here
@@ -28,6 +49,74 @@ LL | let (_a, b): (Vec<&'static String>, _) = (vec![&String::new()], 44);
2849
|
2950
= note: borrowed value must be valid for the static lifetime...
3051

31-
error: aborting due to 3 previous errors
52+
error[E0597]: `x` does not live long enough
53+
--> $DIR/patterns.rs:34:40
54+
|
55+
LL | let (_, _): (&'static u32, u32) = (&x, 44); //~ ERROR
56+
| ^^ borrowed value does not live long enough
57+
LL | }
58+
| - `x` dropped here while still borrowed
59+
|
60+
= note: borrowed value must be valid for the static lifetime...
61+
62+
error[E0597]: `x` does not live long enough
63+
--> $DIR/patterns.rs:39:40
64+
|
65+
LL | let (y, _): (&'static u32, u32) = (&x, 44); //~ ERROR
66+
| ^^ borrowed value does not live long enough
67+
LL | }
68+
| - `x` dropped here while still borrowed
69+
|
70+
= note: borrowed value must be valid for the static lifetime...
71+
72+
error[E0597]: `x` does not live long enough
73+
--> $DIR/patterns.rs:46:69
74+
|
75+
LL | let Single { value: y }: Single<&'static u32> = Single { value: &x }; //~ ERROR
76+
| ^^ borrowed value does not live long enough
77+
LL | }
78+
| - `x` dropped here while still borrowed
79+
|
80+
= note: borrowed value must be valid for the static lifetime...
81+
82+
error[E0597]: `x` does not live long enough
83+
--> $DIR/patterns.rs:51:69
84+
|
85+
LL | let Single { value: _ }: Single<&'static u32> = Single { value: &x }; //~ ERROR
86+
| ^^ borrowed value does not live long enough
87+
LL | }
88+
| - `x` dropped here while still borrowed
89+
|
90+
= note: borrowed value must be valid for the static lifetime...
91+
92+
error[E0597]: `x` does not live long enough
93+
--> $DIR/patterns.rs:59:17
94+
|
95+
LL | value1: &x, //~ ERROR
96+
| ^^ borrowed value does not live long enough
97+
...
98+
LL | }
99+
| - `x` dropped here while still borrowed
100+
|
101+
= note: borrowed value must be valid for the static lifetime...
102+
103+
error: unsatisfied lifetime constraints
104+
--> $DIR/patterns.rs:72:5
105+
|
106+
LL | fn static_to_a_to_static_through_variable<'a>(x: &'a u32) -> &'static u32 {
107+
| -- lifetime `'a` defined here
108+
...
109+
LL | y //~ ERROR
110+
| ^ returning this value requires that `'a` must outlive `'static`
111+
112+
error: unsatisfied lifetime constraints
113+
--> $DIR/patterns.rs:88:40
114+
|
115+
LL | fn a_to_static_then_static<'a>(x: &'a u32) -> &'static u32 {
116+
| -- lifetime `'a` defined here
117+
LL | let (y, _z): (&'static u32, u32) = (x, 44); //~ ERROR
118+
| ^^^^^^^ requires that `'a` must outlive `'static`
119+
120+
error: aborting due to 12 previous errors
32121

33122
For more information about this error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)