Skip to content

Commit

Permalink
Updated tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Regueiro committed Mar 24, 2019
1 parent 3be9417 commit 74a623f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/librustc/infer/opaque_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
// For soundness, we need to ensure that every region that is captured by the opaque type
// but does not explicitly appear in the opaque type outlives the actual (concrete) type.
// This allows for invariant lifetimes to be captured by opaque types as long as
// short-lived lifetimes are not permitted to escape and cause UB.
// short-lived lifetimes are not permitted to escape and cause UB.
let opaque_substs_regions = opaque_defn.substs.regions().collect();
let captured_regions = concrete_ty_regions.difference(&opaque_substs_regions);
for captured_region in captured_regions {
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/impl-trait/issue-55608-captures-empty-region.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// run-pass

// This used to ICE because it creates an `impl Trait` that captures a
// hidden empty region.

#![feature(conservative_impl_trait)]

fn server() -> impl FilterBase2 { //~ ERROR [E0700]
fn server() -> impl FilterBase2 {
segment2(|| { loop { } }).map2(|| "")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// In contrast to `region-escape-via-bound-invariant`, in this case we
// In contrast to `region-escape-via-bound.rs`, 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`.
Expand Down
14 changes: 10 additions & 4 deletions src/test/ui/impl-trait/region-escape-via-bound.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Test that we do not allow the region `'x` to escape in the impl
// trait **even though** `'y` escapes, which outlives `'x`.
// run-pass

// Test that we allow the region `'x` to escape in the impl
// because 'y` escapes, which outlives `'x`.
//
// See https://github.com/rust-lang/rust/issues/46541 for more details.

Expand All @@ -14,10 +16,14 @@ 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 [E0700]
// ^ hidden type for `impl Trait` captures lifetime that does not appear in bounds
// because it outlives the lifetime that *does* appear in the bounds, `'y`
where 'x: 'y
{
x
}

fn main() { }
fn main() {
let x = 123;
let _ = foo(Cell::new(&x));
}
29 changes: 29 additions & 0 deletions src/test/ui/impl-trait/region-escape-via-swap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// compile-fail

// See https://github.com/rust-lang/rust/pull/57870#issuecomment-457333709 for more details.

trait Swap: Sized {
fn swap(self, other: Self);
}

impl<T> Swap for &mut T {
fn swap(self, other: Self) {
std::mem::swap(self, other);
}
}

// The hidden relifetimegion `'b` should not be allowed to escape this function, since it may not
// outlive '`a`, in which case we have a dangling reference.
fn hide<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a {
//~^ lifetime mismatch [E0623]
x
}

fn dangle() -> &'static [i32; 3] {
let mut res = &[4, 5, 6];
let x = [1, 2, 3];
hide(&mut res).swap(hide(&mut &x));
res
}

fn main() {}
12 changes: 12 additions & 0 deletions src/test/ui/impl-trait/region-escape-via-swap.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0623]: lifetime mismatch
--> $DIR/region-escape-via-swap.rs:17:50
|
LL | fn hide<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a {
| ----- ^^^^^^^^^^^^^^
| | |
| | ...but data from `x` is returned here
| this parameter and the return type are declared with different lifetimes...

error: aborting due to previous error

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

0 comments on commit 74a623f

Please sign in to comment.