diff --git a/src/test/ui/nll/user-annotations/patterns.rs b/src/test/ui/nll/user-annotations/patterns.rs index 855f174f82a1276f97b7e3c228c8210b37576d76..c4ed72c7482deffa3f605a8dfd61d25b1d5d8c97 100644 --- a/src/test/ui/nll/user-annotations/patterns.rs +++ b/src/test/ui/nll/user-annotations/patterns.rs @@ -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] @@ -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 { 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 { 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() { } diff --git a/src/test/ui/nll/user-annotations/patterns.stderr b/src/test/ui/nll/user-annotations/patterns.stderr index f982362d0607b4fb81c929d287e41df6668cfb3b..e1208bfb5ec97ed6fb7867d0eb7561ef77eeafcc 100644 --- a/src/test/ui/nll/user-annotations/patterns.stderr +++ b/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 @@ -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 @@ -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 @@ -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`.