Skip to content

Commit

Permalink
expand the patterns test with a bunch more scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Sep 10, 2018
1 parent 2f6628e commit a871053
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 5 deletions.
80 changes: 79 additions & 1 deletion src/test/ui/nll/user-annotations/patterns.rs
Expand Up @@ -2,7 +2,23 @@

#![feature(nll)]

fn main() {
fn variable_no_initializer() {
// FIXME: It is unclear to me whether this should be an error or not.

let x = 22;
let y: &'static u32;
y = &x;
}

fn variable_with_initializer() {
let x = 22;
let y: &'static u32 = &x; //~ ERROR
}

fn underscore_with_initializer() {
let x = 22;
let _: &'static u32 = &x; //~ ERROR

let _: Vec<&'static String> = vec![&String::new()];
//~^ ERROR borrowed value does not live long enough [E0597]

Expand All @@ -12,3 +28,65 @@ fn main() {
let (_a, b): (Vec<&'static String>, _) = (vec![&String::new()], 44);
//~^ ERROR borrowed value does not live long enough [E0597]
}

fn pair_underscores_with_initializer() {
let x = 22;
let (_, _): (&'static u32, u32) = (&x, 44); //~ ERROR
}

fn pair_variable_with_initializer() {
let x = 22;
let (y, _): (&'static u32, u32) = (&x, 44); //~ ERROR
}

struct Single<T> { value: T }

fn struct_single_field_variable_with_initializer() {
let x = 22;
let Single { value: y }: Single<&'static u32> = Single { value: &x }; //~ ERROR
}

fn struct_single_field_underscore_with_initializer() {
let x = 22;
let Single { value: _ }: Single<&'static u32> = Single { value: &x }; //~ ERROR
}

struct Double<T> { value1: T, value2: T }

fn struct_double_field_underscore_with_initializer() {
let x = 22;
let Double { value1: _, value2: _ }: Double<&'static u32> = Double {
value1: &x, //~ ERROR
value2: &44,
};
}

fn static_to_a_to_static_through_variable<'a>(x: &'a u32) -> &'static u32 {
// The error in this test is inconsistency with
// `static_to_a_to_static_through_tuple`, but "feels right" to
// me. It occurs because we special case the single binding case
// and force the type of `y` to be `&'a u32`, even though the
// right-hand side has type `&'static u32`.

let y: &'a u32 = &22;
y //~ ERROR
}

fn static_to_a_to_static_through_tuple<'a>(x: &'a u32) -> &'static u32 {
// FIXME: The fact that this type-checks is perhaps surprising.
// What happens is that the right-hand side is constrained to have
// type `&'a u32`, which is possible, because it has type
// `&'static u32`. The variable `y` is then forced to have type
// `&'static u32`, but it is constrained only by the right-hand
// side, not the ascribed type, and hence it passes.

let (y, _z): (&'a u32, u32) = (&22, 44);
y
}

fn a_to_static_then_static<'a>(x: &'a u32) -> &'static u32 {
let (y, _z): (&'static u32, u32) = (x, 44); //~ ERROR
y
}

fn main() { }
97 changes: 93 additions & 4 deletions src/test/ui/nll/user-annotations/patterns.stderr
@@ -1,5 +1,26 @@
error[E0597]: `x` does not live long enough
--> $DIR/patterns.rs:15:27
|
LL | let y: &'static u32 = &x; //~ ERROR
| ^^ borrowed value does not live long enough
LL | }
| - `x` dropped here while still borrowed
|
= note: borrowed value must be valid for the static lifetime...

error[E0597]: `x` does not live long enough
--> $DIR/patterns.rs:20:27
|
LL | let _: &'static u32 = &x; //~ ERROR
| ^^ borrowed value does not live long enough
...
LL | }
| - `x` dropped here while still borrowed
|
= note: borrowed value must be valid for the static lifetime...

error[E0597]: borrowed value does not live long enough
--> $DIR/patterns.rs:6:41
--> $DIR/patterns.rs:22:41
|
LL | let _: Vec<&'static String> = vec![&String::new()];
| ^^^^^^^^^^^^^ - temporary value only lives until here
Expand All @@ -9,7 +30,7 @@ LL | let _: Vec<&'static String> = vec![&String::new()];
= note: borrowed value must be valid for the static lifetime...

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

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

error: aborting due to 3 previous errors
error[E0597]: `x` does not live long enough
--> $DIR/patterns.rs:34:40
|
LL | let (_, _): (&'static u32, u32) = (&x, 44); //~ ERROR
| ^^ borrowed value does not live long enough
LL | }
| - `x` dropped here while still borrowed
|
= note: borrowed value must be valid for the static lifetime...

error[E0597]: `x` does not live long enough
--> $DIR/patterns.rs:39:40
|
LL | let (y, _): (&'static u32, u32) = (&x, 44); //~ ERROR
| ^^ borrowed value does not live long enough
LL | }
| - `x` dropped here while still borrowed
|
= note: borrowed value must be valid for the static lifetime...

error[E0597]: `x` does not live long enough
--> $DIR/patterns.rs:46:69
|
LL | let Single { value: y }: Single<&'static u32> = Single { value: &x }; //~ ERROR
| ^^ borrowed value does not live long enough
LL | }
| - `x` dropped here while still borrowed
|
= note: borrowed value must be valid for the static lifetime...

error[E0597]: `x` does not live long enough
--> $DIR/patterns.rs:51:69
|
LL | let Single { value: _ }: Single<&'static u32> = Single { value: &x }; //~ ERROR
| ^^ borrowed value does not live long enough
LL | }
| - `x` dropped here while still borrowed
|
= note: borrowed value must be valid for the static lifetime...

error[E0597]: `x` does not live long enough
--> $DIR/patterns.rs:59:17
|
LL | value1: &x, //~ ERROR
| ^^ borrowed value does not live long enough
...
LL | }
| - `x` dropped here while still borrowed
|
= note: borrowed value must be valid for the static lifetime...

error: unsatisfied lifetime constraints
--> $DIR/patterns.rs:72:5
|
LL | fn static_to_a_to_static_through_variable<'a>(x: &'a u32) -> &'static u32 {
| -- lifetime `'a` defined here
...
LL | y //~ ERROR
| ^ returning this value requires that `'a` must outlive `'static`

error: unsatisfied lifetime constraints
--> $DIR/patterns.rs:88:40
|
LL | fn a_to_static_then_static<'a>(x: &'a u32) -> &'static u32 {
| -- lifetime `'a` defined here
LL | let (y, _z): (&'static u32, u32) = (x, 44); //~ ERROR
| ^^^^^^^ requires that `'a` must outlive `'static`

error: aborting due to 12 previous errors

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

0 comments on commit a871053

Please sign in to comment.