Handle lifetime annotations in unreachable code

We  equate the type in the annotation with the inferred type first so
that we have a fully inferred type to perform the well-formedness check
on.
This commit is contained in:
Matthew Jasper 2019-01-12 14:55:23 +00:00
parent ed871cb368
commit 65fe251634
34 changed files with 514 additions and 166 deletions

View file

@ -1,7 +1,3 @@
// compile-pass
// FIXME(#54943) This test targets the scenario where proving the WF requirements of a user
// type annotation requires checking dead code. This test should actually fail to compile.
#![feature(nll)]
#![allow(warnings)]
@ -11,7 +7,7 @@ fn boo<'a>() {
return;
let x = foo::<&'a u32>();
//~^ ERROR the type `&'a u32` does not fulfill the required lifetime [E0477]
//~^ ERROR lifetime may not live long enough
}
fn main() {}

View file

@ -0,0 +1,11 @@
error: lifetime may not live long enough
--> $DIR/issue-54943.rs:9:13
|
LL | fn boo<'a>() {
| -- lifetime `'a` defined here
...
LL | let x = foo::<&'a u32>();
| ^^^^^^^^^^^^^^ requires that `'a` must outlive `'static`
error: aborting due to previous error

View file

@ -2,7 +2,7 @@ error[E0309]: the associated type `<T as MyTrait<'_>>::Output` may not live long
--> $DIR/projection-where-clause-env-wrong-bound.rs:17:5
|
LL | bar::<T::Output>() //~ ERROR may not live long enough
| ^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^
|
= help: consider adding an explicit lifetime bound `<T as MyTrait<'_>>::Output: 'a`...

View file

@ -2,7 +2,7 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/projection-where-clause-none.rs:16:5
|
LL | bar::<T::Output>() //~ ERROR the parameter type `T` may not live long enough
| ^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^
|
= help: consider adding an explicit lifetime bound `T: 'a`...

View file

@ -0,0 +1,54 @@
// Test that we check that user type annotations are well-formed, even in dead
// code.
#![feature(nll)]
fn uninit<'a>() {
return;
let x: &'static &'a (); //~ ERROR lifetime may not live long enough
}
fn var_type<'a>() {
return;
let x: &'static &'a () = &&(); //~ ERROR lifetime may not live long enough
}
fn uninit_infer<'a>() {
let x: &'static &'a _; //~ ERROR lifetime may not live long enough
x = && ();
}
fn infer<'a>() {
return;
let x: &'static &'a _ = &&(); //~ ERROR lifetime may not live long enough
}
fn uninit_no_var<'a>() {
return;
let _: &'static &'a (); //~ ERROR lifetime may not live long enough
}
fn no_var<'a>() {
return;
let _: &'static &'a () = &&(); //~ ERROR lifetime may not live long enough
}
fn infer_no_var<'a>() {
return;
let _: &'static &'a _ = &&(); //~ ERROR lifetime may not live long enough
}
trait X<'a, 'b> {}
struct C<'a, 'b, T: X<'a, 'b>>(T, &'a (), &'b ());
impl X<'_, '_> for i32 {}
impl<'a> X<'a, 'a> for () {}
// This type annotation is not well-formed because we substitute `()` for `_`.
fn required_substs<'a>() {
return;
let _: C<'static, 'a, _> = C((), &(), &()); //~ ERROR lifetime may not live long enough
}
fn main() {}

View file

@ -0,0 +1,73 @@
error: lifetime may not live long enough
--> $DIR/wf-unreachable.rs:8:12
|
LL | fn uninit<'a>() {
| -- lifetime `'a` defined here
LL | return;
LL | let x: &'static &'a (); //~ ERROR lifetime may not live long enough
| ^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
error: lifetime may not live long enough
--> $DIR/wf-unreachable.rs:13:12
|
LL | fn var_type<'a>() {
| -- lifetime `'a` defined here
LL | return;
LL | let x: &'static &'a () = &&(); //~ ERROR lifetime may not live long enough
| ^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
error: lifetime may not live long enough
--> $DIR/wf-unreachable.rs:17:12
|
LL | fn uninit_infer<'a>() {
| -- lifetime `'a` defined here
LL | let x: &'static &'a _; //~ ERROR lifetime may not live long enough
| ^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
error: lifetime may not live long enough
--> $DIR/wf-unreachable.rs:23:12
|
LL | fn infer<'a>() {
| -- lifetime `'a` defined here
LL | return;
LL | let x: &'static &'a _ = &&(); //~ ERROR lifetime may not live long enough
| ^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
error: lifetime may not live long enough
--> $DIR/wf-unreachable.rs:28:12
|
LL | fn uninit_no_var<'a>() {
| -- lifetime `'a` defined here
LL | return;
LL | let _: &'static &'a (); //~ ERROR lifetime may not live long enough
| ^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
error: lifetime may not live long enough
--> $DIR/wf-unreachable.rs:33:12
|
LL | fn no_var<'a>() {
| -- lifetime `'a` defined here
LL | return;
LL | let _: &'static &'a () = &&(); //~ ERROR lifetime may not live long enough
| ^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
error: lifetime may not live long enough
--> $DIR/wf-unreachable.rs:38:12
|
LL | fn infer_no_var<'a>() {
| -- lifetime `'a` defined here
LL | return;
LL | let _: &'static &'a _ = &&(); //~ ERROR lifetime may not live long enough
| ^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
error: lifetime may not live long enough
--> $DIR/wf-unreachable.rs:51:12
|
LL | fn required_substs<'a>() {
| -- lifetime `'a` defined here
LL | return;
LL | let _: C<'static, 'a, _> = C((), &(), &()); //~ ERROR lifetime may not live long enough
| ^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
error: aborting due to 8 previous errors

View file

@ -0,0 +1,11 @@
// compile-pass
// Check that we don't try to downcast `_` when type-checking the annotation.
fn main() {
let x = Some(Some(Some(1)));
match x {
Some::<Option<_>>(Some(Some(v))) => (),
_ => (),
}
}

View file

@ -0,0 +1,20 @@
error[E0491]: in type `&'a WithAssoc<TheType<'b>>`, reference has a longer lifetime than the data it references
--> $DIR/regions-assoc-type-in-supertrait-outlives-container.rs:45:13
|
LL | let _x: &'a WithAssoc<TheType<'b>> = loop { };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the pointer is valid for the lifetime 'a as defined on the function body at 37:15
--> $DIR/regions-assoc-type-in-supertrait-outlives-container.rs:37:15
|
LL | fn with_assoc<'a,'b>() {
| ^^
note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 37:18
--> $DIR/regions-assoc-type-in-supertrait-outlives-container.rs:37:18
|
LL | fn with_assoc<'a,'b>() {
| ^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0491`.

View file

@ -0,0 +1,13 @@
error: lifetime may not live long enough
--> $DIR/regions-assoc-type-in-supertrait-outlives-container.rs:45:13
|
LL | fn with_assoc<'a,'b>() {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
...
LL | let _x: &'a WithAssoc<TheType<'b>> = loop { };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'b` must outlive `'a`
error: aborting due to previous error

View file

@ -3,6 +3,9 @@
// outlive the location in which the type appears, even when the
// associted type is in a supertype. Issue #22246.
// revisions: ast mir
//[mir]compile-flags: -Z borrowck=mir
#![allow(dead_code)]
///////////////////////////////////////////////////////////////////////////
@ -40,7 +43,8 @@ fn with_assoc<'a,'b>() {
// FIXME (#54943) NLL doesn't enforce WF condition in unreachable code if
// `_x` is changed to `_`
let _x: &'a WithAssoc<TheType<'b>> = loop { };
//~^ ERROR reference has a longer lifetime
//[ast]~^ ERROR reference has a longer lifetime
//[mir]~^^ ERROR lifetime may not live long enough
}
fn main() {

View file

@ -0,0 +1,32 @@
error[E0623]: lifetime mismatch
--> $DIR/regions-free-region-ordering-caller.rs:11:12
|
LL | fn call2<'a, 'b>(a: &'a usize, b: &'b usize) {
| --------- ---------
| |
| these two types are declared with different lifetimes...
LL | let z: Option<&'b &'a usize> = None;//[ast]~ ERROR E0623
| ^^^^^^^^^^^^^^^^^^^^^ ...but data from `a` flows into `b` here
error[E0623]: lifetime mismatch
--> $DIR/regions-free-region-ordering-caller.rs:17:12
|
LL | fn call3<'a, 'b>(a: &'a usize, b: &'b usize) {
| --------- ---------
| |
| these two types are declared with different lifetimes...
LL | let y: Paramd<'a> = Paramd { x: a };
LL | let z: Option<&'b Paramd<'a>> = None;//[ast]~ ERROR E0623
| ^^^^^^^^^^^^^^^^^^^^^^ ...but data from `a` flows into `b` here
error[E0623]: lifetime mismatch
--> $DIR/regions-free-region-ordering-caller.rs:22:12
|
LL | fn call4<'a, 'b>(a: &'a usize, b: &'b usize) {
| --------- --------- these two types are declared with different lifetimes...
LL | let z: Option<&'a &'b usize> = None;//[ast]~ ERROR E0623
| ^^^^^^^^^^^^^^^^^^^^^ ...but data from `b` flows into `a` here
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0623`.

View file

@ -0,0 +1,33 @@
error: lifetime may not live long enough
--> $DIR/regions-free-region-ordering-caller.rs:11:12
|
LL | fn call2<'a, 'b>(a: &'a usize, b: &'b usize) {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
LL | let z: Option<&'b &'a usize> = None;//[ast]~ ERROR E0623
| ^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'b`
error: lifetime may not live long enough
--> $DIR/regions-free-region-ordering-caller.rs:17:12
|
LL | fn call3<'a, 'b>(a: &'a usize, b: &'b usize) {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
LL | let y: Paramd<'a> = Paramd { x: a };
LL | let z: Option<&'b Paramd<'a>> = None;//[ast]~ ERROR E0623
| ^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'b`
error: lifetime may not live long enough
--> $DIR/regions-free-region-ordering-caller.rs:22:12
|
LL | fn call4<'a, 'b>(a: &'a usize, b: &'b usize) {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
LL | let z: Option<&'a &'b usize> = None;//[ast]~ ERROR E0623
| ^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'b` must outlive `'a`
error: aborting due to 3 previous errors

View file

@ -2,19 +2,25 @@
// than the thing it points at and ensure that they result in
// errors. See also regions-free-region-ordering-callee.rs
// revisions: ast mir
//[mir]compile-flags: -Z borrowck=mir
struct Paramd<'a> { x: &'a usize }
fn call2<'a, 'b>(a: &'a usize, b: &'b usize) {
let z: Option<&'b &'a usize> = None;//~ ERROR E0623
let z: Option<&'b &'a usize> = None;//[ast]~ ERROR E0623
//[mir]~^ ERROR lifetime may not live long enough
}
fn call3<'a, 'b>(a: &'a usize, b: &'b usize) {
let y: Paramd<'a> = Paramd { x: a };
let z: Option<&'b Paramd<'a>> = None;//~ ERROR E0623
let z: Option<&'b Paramd<'a>> = None;//[ast]~ ERROR E0623
//[mir]~^ ERROR lifetime may not live long enough
}
fn call4<'a, 'b>(a: &'a usize, b: &'b usize) {
let z: Option<&'a &'b usize> = None;//~ ERROR E0623
let z: Option<&'a &'b usize> = None;//[ast]~ ERROR E0623
//[mir]~^ ERROR lifetime may not live long enough
}
fn main() {}

View file

@ -12,6 +12,21 @@ LL | let z: &'a & usize = &(&y);
LL | }
| - temporary value is freed at the end of this statement
error: aborting due to previous error
error[E0597]: `y` does not live long enough
--> $DIR/regions-free-region-ordering-caller1.rs:9:27
|
LL | fn call1<'a>(x: &'a usize) {
| -- lifetime `'a` defined here
...
LL | let z: &'a & usize = &(&y);
| ----------- ^^^^ borrowed value does not live long enough
| |
| type annotation requires that `y` is borrowed for `'a`
...
LL | }
| - `y` dropped here while still borrowed
For more information about this error, try `rustc --explain E0716`.
error: aborting due to 2 previous errors
Some errors occurred: E0597, E0716.
For more information about an error, try `rustc --explain E0597`.

View file

@ -0,0 +1,37 @@
error[E0491]: in type `&'a WithHrAssoc<TheType<'b>>`, reference has a longer lifetime than the data it references
--> $DIR/regions-outlives-projection-container-hrtb.rs:35:12
|
LL | let _: &'a WithHrAssoc<TheType<'b>> = loop { };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the pointer is valid for the lifetime 'a as defined on the function body at 32:15
--> $DIR/regions-outlives-projection-container-hrtb.rs:32:15
|
LL | fn with_assoc<'a,'b>() {
| ^^
note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 32:18
--> $DIR/regions-outlives-projection-container-hrtb.rs:32:18
|
LL | fn with_assoc<'a,'b>() {
| ^^
error[E0491]: in type `&'a WithHrAssocSub<TheType<'b>>`, reference has a longer lifetime than the data it references
--> $DIR/regions-outlives-projection-container-hrtb.rs:57:12
|
LL | let _: &'a WithHrAssocSub<TheType<'b>> = loop { };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the pointer is valid for the lifetime 'a as defined on the function body at 53:19
--> $DIR/regions-outlives-projection-container-hrtb.rs:53:19
|
LL | fn with_assoc_sub<'a,'b>() {
| ^^
note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 53:22
--> $DIR/regions-outlives-projection-container-hrtb.rs:53:22
|
LL | fn with_assoc_sub<'a,'b>() {
| ^^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0491`.

View file

@ -0,0 +1,24 @@
error: lifetime may not live long enough
--> $DIR/regions-outlives-projection-container-hrtb.rs:35:12
|
LL | fn with_assoc<'a,'b>() {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
...
LL | let _: &'a WithHrAssoc<TheType<'b>> = loop { };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'b` must outlive `'a`
error: lifetime may not live long enough
--> $DIR/regions-outlives-projection-container-hrtb.rs:57:12
|
LL | fn with_assoc_sub<'a,'b>() {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
...
LL | let _: &'a WithHrAssocSub<TheType<'b>> = loop { };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'b` must outlive `'a`
error: aborting due to 2 previous errors

View file

@ -1,6 +1,9 @@
// Test that structs with higher-ranked where clauses don't generate
// "outlives" requirements. Issue #22246.
// revisions: ast mir
//[mir]compile-flags: -Z borrowck=mir
#![allow(dead_code)]
@ -30,7 +33,8 @@ fn with_assoc<'a,'b>() {
// We get an error because 'b:'a does not hold:
let _: &'a WithHrAssoc<TheType<'b>> = loop { };
//~^ ERROR reference has a longer lifetime
//[ast]~^ ERROR reference has a longer lifetime
//[mir]~^^ ERROR lifetime may not live long enough
}
///////////////////////////////////////////////////////////////////////////
@ -51,7 +55,8 @@ fn with_assoc_sub<'a,'b>() {
// below to be well-formed, it is not related to the HR relation.
let _: &'a WithHrAssocSub<TheType<'b>> = loop { };
//~^ ERROR reference has a longer lifetime
//[ast]~^ ERROR reference has a longer lifetime
//[mir]~^^ ERROR lifetime may not live long enough
}

View file

@ -0,0 +1,20 @@
error[E0491]: in type `&'a WithAssoc<TheType<'b>>`, reference has a longer lifetime than the data it references
--> $DIR/regions-outlives-projection-container-wc.rs:37:12
|
LL | let _: &'a WithAssoc<TheType<'b>> = loop { };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the pointer is valid for the lifetime 'a as defined on the function body at 31:15
--> $DIR/regions-outlives-projection-container-wc.rs:31:15
|
LL | fn with_assoc<'a,'b>() {
| ^^
note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 31:18
--> $DIR/regions-outlives-projection-container-wc.rs:31:18
|
LL | fn with_assoc<'a,'b>() {
| ^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0491`.

View file

@ -0,0 +1,13 @@
error: lifetime may not live long enough
--> $DIR/regions-outlives-projection-container-wc.rs:37:12
|
LL | fn with_assoc<'a,'b>() {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
...
LL | let _: &'a WithAssoc<TheType<'b>> = loop { };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'b` must outlive `'a`
error: aborting due to previous error

View file

@ -3,6 +3,9 @@
// outlive the location in which the type appears, even when the
// constraint is in a where clause not a bound. Issue #22246.
// revisions: ast mir
//[mir]compile-flags: -Z borrowck=mir
#![allow(dead_code)]
///////////////////////////////////////////////////////////////////////////
@ -32,7 +35,8 @@ fn with_assoc<'a,'b>() {
// which is &'b (), must outlive 'a.
let _: &'a WithAssoc<TheType<'b>> = loop { };
//~^ ERROR reference has a longer lifetime
//[ast]~^ ERROR reference has a longer lifetime
//[mir]~^^ ERROR lifetime may not live long enough
}
fn main() {