Skip to content

Commit 92cbe47

Browse files
committed
Add test for normalization during field-lookup on patterns with ascribed types.
As a drive-by, also added test analogous to existing static_to_a_to_static_through_tuple, but now apply to a struct instead of a tuple.
1 parent f09a0eb commit 92cbe47

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ fn struct_no_initializer() {
4141
y = &x; //~ ERROR
4242
}
4343

44+
45+
fn struct_no_initializer_must_normalize() {
46+
trait Indirect { type Assoc; }
47+
struct StaticU32;
48+
impl Indirect for StaticU32 { type Assoc = &'static u32; }
49+
struct Single2<T: Indirect> { value: <T as Indirect>::Assoc }
50+
51+
let x = 22;
52+
let Single2 { value: mut _y }: Single2<StaticU32>;
53+
_y = &x; //~ ERROR
54+
}
55+
4456
fn variable_with_initializer() {
4557
let x = 22;
4658
let y: &'static u32 = &x; //~ ERROR
@@ -113,6 +125,11 @@ fn static_to_a_to_static_through_tuple<'a>(x: &'a u32) -> &'static u32 {
113125
y //~ ERROR
114126
}
115127

128+
fn static_to_a_to_static_through_struct<'a>(_x: &'a u32) -> &'static u32 {
129+
let Single { value: y }: Single<&'a u32> = Single { value: &22 };
130+
y //~ ERROR
131+
}
132+
116133
fn a_to_static_then_static<'a>(x: &'a u32) -> &'static u32 {
117134
let (y, _z): (&'static u32, u32) = (x, 44); //~ ERROR
118135
y

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

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,17 @@ LL | }
4040
| - `x` dropped here while still borrowed
4141

4242
error[E0597]: `x` does not live long enough
43-
--> $DIR/patterns.rs:46:27
43+
--> $DIR/patterns.rs:53:10
44+
|
45+
LL | let Single2 { value: mut _y }: Single2<StaticU32>;
46+
| ------------------ type annotation requires that `x` is borrowed for `'static`
47+
LL | _y = &x; //~ ERROR
48+
| ^^ borrowed value does not live long enough
49+
LL | }
50+
| - `x` dropped here while still borrowed
51+
52+
error[E0597]: `x` does not live long enough
53+
--> $DIR/patterns.rs:58:27
4454
|
4555
LL | let y: &'static u32 = &x; //~ ERROR
4656
| ------------ ^^ borrowed value does not live long enough
@@ -50,7 +60,7 @@ LL | }
5060
| - `x` dropped here while still borrowed
5161

5262
error[E0597]: `x` does not live long enough
53-
--> $DIR/patterns.rs:51:27
63+
--> $DIR/patterns.rs:63:27
5464
|
5565
LL | let _: &'static u32 = &x; //~ ERROR
5666
| ------------ ^^ borrowed value does not live long enough
@@ -61,7 +71,7 @@ LL | }
6171
| - `x` dropped here while still borrowed
6272

6373
error[E0716]: temporary value dropped while borrowed
64-
--> $DIR/patterns.rs:53:41
74+
--> $DIR/patterns.rs:65:41
6575
|
6676
LL | let _: Vec<&'static String> = vec![&String::new()];
6777
| -------------------- ^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
@@ -70,7 +80,7 @@ LL | let _: Vec<&'static String> = vec![&String::new()];
7080
| type annotation requires that borrow lasts for `'static`
7181

7282
error[E0716]: temporary value dropped while borrowed
73-
--> $DIR/patterns.rs:56:52
83+
--> $DIR/patterns.rs:68:52
7484
|
7585
LL | let (_, a): (Vec<&'static String>, _) = (vec![&String::new()], 44);
7686
| ------------------------- ^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
@@ -79,7 +89,7 @@ LL | let (_, a): (Vec<&'static String>, _) = (vec![&String::new()], 44);
7989
| type annotation requires that borrow lasts for `'static`
8090

8191
error[E0716]: temporary value dropped while borrowed
82-
--> $DIR/patterns.rs:59:53
92+
--> $DIR/patterns.rs:71:53
8393
|
8494
LL | let (_a, b): (Vec<&'static String>, _) = (vec![&String::new()], 44);
8595
| ------------------------- ^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
@@ -88,7 +98,7 @@ LL | let (_a, b): (Vec<&'static String>, _) = (vec![&String::new()], 44);
8898
| type annotation requires that borrow lasts for `'static`
8999

90100
error[E0597]: `x` does not live long enough
91-
--> $DIR/patterns.rs:65:40
101+
--> $DIR/patterns.rs:77:40
92102
|
93103
LL | let (_, _): (&'static u32, u32) = (&x, 44); //~ ERROR
94104
| ------------------- ^^ borrowed value does not live long enough
@@ -98,7 +108,7 @@ LL | }
98108
| - `x` dropped here while still borrowed
99109

100110
error[E0597]: `x` does not live long enough
101-
--> $DIR/patterns.rs:70:40
111+
--> $DIR/patterns.rs:82:40
102112
|
103113
LL | let (y, _): (&'static u32, u32) = (&x, 44); //~ ERROR
104114
| ------------------- ^^ borrowed value does not live long enough
@@ -108,7 +118,7 @@ LL | }
108118
| - `x` dropped here while still borrowed
109119

110120
error[E0597]: `x` does not live long enough
111-
--> $DIR/patterns.rs:75:69
121+
--> $DIR/patterns.rs:87:69
112122
|
113123
LL | let Single { value: y }: Single<&'static u32> = Single { value: &x }; //~ ERROR
114124
| -------------------- ^^ borrowed value does not live long enough
@@ -118,7 +128,7 @@ LL | }
118128
| - `x` dropped here while still borrowed
119129

120130
error[E0597]: `x` does not live long enough
121-
--> $DIR/patterns.rs:80:69
131+
--> $DIR/patterns.rs:92:69
122132
|
123133
LL | let Single { value: _ }: Single<&'static u32> = Single { value: &x }; //~ ERROR
124134
| -------------------- ^^ borrowed value does not live long enough
@@ -128,7 +138,7 @@ LL | }
128138
| - `x` dropped here while still borrowed
129139

130140
error[E0597]: `x` does not live long enough
131-
--> $DIR/patterns.rs:88:17
141+
--> $DIR/patterns.rs:100:17
132142
|
133143
LL | let Double { value1: _, value2: _ }: Double<&'static u32> = Double {
134144
| -------------------- type annotation requires that `x` is borrowed for `'static`
@@ -139,7 +149,7 @@ LL | }
139149
| - `x` dropped here while still borrowed
140150

141151
error: unsatisfied lifetime constraints
142-
--> $DIR/patterns.rs:101:5
152+
--> $DIR/patterns.rs:113:5
143153
|
144154
LL | fn static_to_a_to_static_through_variable<'a>(x: &'a u32) -> &'static u32 {
145155
| -- lifetime `'a` defined here
@@ -148,7 +158,7 @@ LL | y //~ ERROR
148158
| ^ returning this value requires that `'a` must outlive `'static`
149159

150160
error: unsatisfied lifetime constraints
151-
--> $DIR/patterns.rs:113:5
161+
--> $DIR/patterns.rs:125:5
152162
|
153163
LL | fn static_to_a_to_static_through_tuple<'a>(x: &'a u32) -> &'static u32 {
154164
| -- lifetime `'a` defined here
@@ -157,14 +167,23 @@ LL | y //~ ERROR
157167
| ^ returning this value requires that `'a` must outlive `'static`
158168

159169
error: unsatisfied lifetime constraints
160-
--> $DIR/patterns.rs:117:18
170+
--> $DIR/patterns.rs:130:5
171+
|
172+
LL | fn static_to_a_to_static_through_struct<'a>(_x: &'a u32) -> &'static u32 {
173+
| -- lifetime `'a` defined here
174+
LL | let Single { value: y }: Single<&'a u32> = Single { value: &22 };
175+
LL | y //~ ERROR
176+
| ^ returning this value requires that `'a` must outlive `'static`
177+
178+
error: unsatisfied lifetime constraints
179+
--> $DIR/patterns.rs:134:18
161180
|
162181
LL | fn a_to_static_then_static<'a>(x: &'a u32) -> &'static u32 {
163182
| -- lifetime `'a` defined here
164183
LL | let (y, _z): (&'static u32, u32) = (x, 44); //~ ERROR
165184
| ^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
166185

167-
error: aborting due to 17 previous errors
186+
error: aborting due to 19 previous errors
168187

169188
Some errors occurred: E0597, E0716.
170189
For more information about an error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)