Auto merge of #49041 - nikomatsakis:issue-46541-impl-trait-hidden-lifetimes, r=cramertj
Detect illegal hidden lifetimes in `impl Trait` This branch fixes #46541 -- however, it presently doesn't build because it also *breaks* a number of existing usages of impl Trait. I'm opening it as a WIP for now, just because we want to move on impl Trait, but I'll try to fix the problem in a bit. ~~(The problem is due to the fact that we apparently infer stricter lifetimes in closures that we need to; for example, if you capture a variable of type `&'a &'b u32`, we will put *precisely* those lifetimes into the closure, even if the closure would be happy with `&'a &'a u32`. This causes the present chance to affect things that are not invariant.)~~ fixed r? @cramertj
This commit is contained in:
commit
e575773141
17 changed files with 427 additions and 84 deletions
|
|
@ -36,7 +36,7 @@ fn foo<F>(f: F) where F: FnOnce() -> i32 {
|
|||
// let _2: &'21_1rs D;
|
||||
// ...
|
||||
// let mut _3: ();
|
||||
// let mut _4: [closure@NodeId(22) r:&'21_1rs D];
|
||||
// let mut _4: [closure@NodeId(22) r:&'19s D];
|
||||
// let mut _5: &'21_1rs D;
|
||||
// bb0: {
|
||||
// StorageLive(_1);
|
||||
|
|
@ -54,6 +54,7 @@ fn foo<F>(f: F) where F: FnOnce() -> i32 {
|
|||
// resume;
|
||||
// }
|
||||
// bb2: {
|
||||
// EndRegion('19s);
|
||||
// StorageDead(_4);
|
||||
// _0 = ();
|
||||
// EndRegion('21_1rs);
|
||||
|
|
@ -61,6 +62,7 @@ fn foo<F>(f: F) where F: FnOnce() -> i32 {
|
|||
// drop(_1) -> [return: bb4, unwind: bb1];
|
||||
// }
|
||||
// bb3: {
|
||||
// EndRegion('19s);
|
||||
// EndRegion('21_1rs);
|
||||
// drop(_1) -> bb1;
|
||||
// }
|
||||
|
|
@ -72,7 +74,7 @@ fn foo<F>(f: F) where F: FnOnce() -> i32 {
|
|||
// END rustc.main.SimplifyCfg-qualify-consts.after.mir
|
||||
|
||||
// START rustc.main-{{closure}}.SimplifyCfg-qualify-consts.after.mir
|
||||
// fn main::{{closure}}(_1: [closure@NodeId(22) r:&'21_1rs D]) -> i32 {
|
||||
// fn main::{{closure}}(_1: [closure@NodeId(22) r:&'19s D]) -> i32 {
|
||||
// let mut _0: i32;
|
||||
// let mut _2: i32;
|
||||
//
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
error[E0277]: the trait bound `No: Foo` is not satisfied in `[generator@$DIR/auto-trait-regions.rs:35:15: 39:6 x:&&'static OnlyFooIfStaticRef for<'r> {&'r OnlyFooIfStaticRef, ()}]`
|
||||
error[E0277]: the trait bound `No: Foo` is not satisfied in `[generator@$DIR/auto-trait-regions.rs:35:15: 39:6 x:&&OnlyFooIfStaticRef for<'r> {&'r OnlyFooIfStaticRef, ()}]`
|
||||
--> $DIR/auto-trait-regions.rs:40:5
|
||||
|
|
||||
LL | assert_foo(gen); //~ ERROR the trait bound `No: Foo` is not satisfied
|
||||
| ^^^^^^^^^^ within `[generator@$DIR/auto-trait-regions.rs:35:15: 39:6 x:&&'static OnlyFooIfStaticRef for<'r> {&'r OnlyFooIfStaticRef, ()}]`, the trait `Foo` is not implemented for `No`
|
||||
| ^^^^^^^^^^ within `[generator@$DIR/auto-trait-regions.rs:35:15: 39:6 x:&&OnlyFooIfStaticRef for<'r> {&'r OnlyFooIfStaticRef, ()}]`, the trait `Foo` is not implemented for `No`
|
||||
|
|
||||
= help: the following implementations were found:
|
||||
<No as Foo>
|
||||
= note: required because it appears within the type `OnlyFooIfStaticRef`
|
||||
= note: required because it appears within the type `&OnlyFooIfStaticRef`
|
||||
= note: required because it appears within the type `for<'r> {&'r OnlyFooIfStaticRef, ()}`
|
||||
= note: required because it appears within the type `[generator@$DIR/auto-trait-regions.rs:35:15: 39:6 x:&&'static OnlyFooIfStaticRef for<'r> {&'r OnlyFooIfStaticRef, ()}]`
|
||||
= note: required because it appears within the type `[generator@$DIR/auto-trait-regions.rs:35:15: 39:6 x:&&OnlyFooIfStaticRef for<'r> {&'r OnlyFooIfStaticRef, ()}]`
|
||||
note: required by `assert_foo`
|
||||
--> $DIR/auto-trait-regions.rs:30:1
|
||||
|
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// In contrast to `region-escape-via-bound-invariant`, in this case we
|
||||
// *can* return a value of type `&'x u32`, even though `'x` does not
|
||||
// appear in the bounds. This is because `&` is contravariant, and so
|
||||
// we are *actually* returning a `&'y u32`.
|
||||
//
|
||||
// See https://github.com/rust-lang/rust/issues/46541 for more details.
|
||||
|
||||
// run-pass
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![feature(conservative_impl_trait)]
|
||||
#![feature(in_band_lifetimes)]
|
||||
#![feature(nll)]
|
||||
|
||||
fn foo(x: &'x u32) -> impl Fn() -> &'y u32
|
||||
where 'x: 'y
|
||||
{
|
||||
move || x
|
||||
}
|
||||
|
||||
fn main() { }
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// In contrast to `region-escape-via-bound-invariant`, in this case we
|
||||
// *can* return a value of type `&'x u32`, even though `'x` does not
|
||||
// appear in the bounds. This is because `&` is contravariant, and so
|
||||
// we are *actually* returning a `&'y u32`.
|
||||
//
|
||||
// See https://github.com/rust-lang/rust/issues/46541 for more details.
|
||||
|
||||
// run-pass
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![feature(conservative_impl_trait)]
|
||||
#![feature(in_band_lifetimes)]
|
||||
#![feature(nll)]
|
||||
|
||||
trait Trait<'a> { }
|
||||
|
||||
impl Trait<'b> for &'a u32 { }
|
||||
|
||||
fn foo(x: &'x u32) -> impl Trait<'y>
|
||||
where 'x: 'y
|
||||
{
|
||||
x
|
||||
}
|
||||
|
||||
fn main() { }
|
||||
34
src/test/ui/impl-trait/region-escape-via-bound.rs
Normal file
34
src/test/ui/impl-trait/region-escape-via-bound.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// Test that we do not allow the region `'x` to escape in the impl
|
||||
// trait **even though** `'y` escapes, which outlives `'x`.
|
||||
//
|
||||
// See https://github.com/rust-lang/rust/issues/46541 for more details.
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![feature(conservative_impl_trait)]
|
||||
#![feature(in_band_lifetimes)]
|
||||
#![feature(nll)]
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
trait Trait<'a> { }
|
||||
|
||||
impl Trait<'b> for Cell<&'a u32> { }
|
||||
|
||||
fn foo(x: Cell<&'x u32>) -> impl Trait<'y>
|
||||
//~^ ERROR hidden type for `impl Trait` captures lifetime that does not appear in bounds [E0909]
|
||||
where 'x: 'y
|
||||
{
|
||||
x
|
||||
}
|
||||
|
||||
fn main() { }
|
||||
20
src/test/ui/impl-trait/region-escape-via-bound.stderr
Normal file
20
src/test/ui/impl-trait/region-escape-via-bound.stderr
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
error[E0909]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
|
||||
--> $DIR/region-escape-via-bound.rs:27:29
|
||||
|
|
||||
LL | fn foo(x: Cell<&'x u32>) -> impl Trait<'y>
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
|
||||
note: hidden type `std::cell::Cell<&'x u32>` captures the lifetime 'x as defined on the function body at 27:1
|
||||
--> $DIR/region-escape-via-bound.rs:27:1
|
||||
|
|
||||
LL | / fn foo(x: Cell<&'x u32>) -> impl Trait<'y>
|
||||
LL | | //~^ ERROR hidden type for `impl Trait` captures lifetime that does not appear in bounds [E0909]
|
||||
LL | | where 'x: 'y
|
||||
LL | | {
|
||||
LL | | x
|
||||
LL | | }
|
||||
| |_^
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0909`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue