Skip to content

Commit

Permalink
Flesh out tests more
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Oct 17, 2023
1 parent 1005970 commit f0e385d
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 7 deletions.
11 changes: 10 additions & 1 deletion tests/ui/borrowck/alias-liveness/gat-static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait Foo {
fn assoc(&mut self) -> Self::Assoc<'_>;
}

fn test<T>(mut t: T)
fn overlapping_mut<T>(mut t: T)
where
T: Foo,
for<'a> T::Assoc<'a>: 'static,
Expand All @@ -17,4 +17,13 @@ where
let b = t.assoc();
}

fn live_past_borrow<T>(mut t: T)
where
T: Foo,
for<'a> T::Assoc<'a>: 'static {
let x = t.assoc();
drop(t);
drop(x);
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// known-bug: #42940

trait Captures<'a> {}
impl<T> Captures<'_> for T {}

trait Outlives<'a>: 'a {}
impl<'a, T: 'a> Outlives<'a> for T {}

// Test that we treat `for<'a> Opaque: 'a` as `Opaque: 'static`
fn test<'o>(v: &'o Vec<i32>) -> impl Captures<'o> + for<'a> Outlives<'a> {}

fn statik() -> impl Sized {
test(&vec![])
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/higher-ranked-outlives-for-capture.rs:13:11
|
LL | test(&vec![])
| ------^^^^^^-
| | |
| | creates a temporary value which is freed while still in use
| argument requires that borrow lasts for `'static`
LL | }
| - temporary value is freed at the end of this statement
|
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0716`.
16 changes: 16 additions & 0 deletions tests/ui/borrowck/alias-liveness/higher-ranked.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// check-pass

trait Captures<'a> {}
impl<T> Captures<'_> for T {}

trait Outlives<'a>: 'a {}
impl<'a, T: 'a> Outlives<'a> for T {}

// Test that we treat `for<'a> Opaque: 'a` as `Opaque: 'static`
fn test<'o>(v: &'o Vec<i32>) -> impl Captures<'o> + for<'a> Outlives<'a> {}

fn opaque_doesnt_use_temporary() {
let a = test(&vec![]);
}

fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/borrowck/alias-liveness/opaque-capture.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// check-pass

// Check that opaques capturing early and late-bound vars correctly mark
// regions required to be live using the item bounds.

trait Captures<'a> {}
impl<T> Captures<'_> for T {}

fn captures_temp_late<'a>(x: &'a Vec<i32>) -> impl Sized + Captures<'a> + 'static {}
fn captures_temp_early<'a: 'a>(x: &'a Vec<i32>) -> impl Sized + Captures<'a> + 'static {}

fn test() {
let x = captures_temp_early(&vec![]);
let y = captures_temp_late(&vec![]);
}

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/borrowck/alias-liveness/opaque-type-param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// known-bug: #42940

trait Trait {}
impl Trait for () {}

fn foo<'a>(s: &'a str) -> impl Trait + 'static {
bar(s)
}

fn bar<P: AsRef<str>>(s: P) -> impl Trait + 'static {
()
}

fn main() {}
13 changes: 13 additions & 0 deletions tests/ui/borrowck/alias-liveness/opaque-type-param.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0700]: hidden type for `impl Trait + 'static` captures lifetime that does not appear in bounds
--> $DIR/opaque-type-param.rs:7:5
|
LL | fn foo<'a>(s: &'a str) -> impl Trait + 'static {
| -- -------------------- opaque type defined here
| |
| hidden type `impl Trait + 'static` captures the lifetime `'a` as defined here
LL | bar(s)
| ^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0700`.
16 changes: 12 additions & 4 deletions tests/ui/borrowck/alias-liveness/rpit-static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
trait Captures<'a> {}
impl<T> Captures<'_> for T {}

fn foo(x: &i32) -> impl Sized + Captures<'_> + 'static {}
fn foo(x: &mut i32) -> impl Sized + Captures<'_> + 'static {}

fn main() {
fn overlapping_mut() {
let i = &mut 1;
let x = foo(i);
let y = foo(i);
}

fn live_past_borrow() {
let y;
{
let x = 1;
y = foo(&x);
let x = &mut 1;
y = foo(x);
}
}

fn main() {}
8 changes: 7 additions & 1 deletion tests/ui/borrowck/alias-liveness/rpitit-static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ trait Foo {
fn rpitit(&mut self) -> impl Sized + 'static;
}

fn test<T: Foo>(mut t: T) {
fn live_past_borrow<T: Foo>(mut t: T) {
let x = t.rpitit();
drop(t);
drop(x);
}

fn overlapping_mut<T: Foo>(mut t: T) {
let a = t.rpitit();
let b = t.rpitit();
}
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/borrowck/alias-liveness/rtn-static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ trait Foo {
fn borrow(&mut self) -> impl Sized + '_;
}

fn live_past_borrow<T: Foo<borrow(): 'static>>(mut t: T) {
let x = t.borrow();
drop(t);
drop(x);
}

// Test that the `'_` item bound in `borrow` does not cause us to
// overlook the `'static` RTN bound.
fn test<T: Foo<borrow(): 'static>>(mut t: T) {
fn overlapping_mut<T: Foo<borrow(): 'static>>(mut t: T) {
let x = t.borrow();
let x = t.borrow();
}
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/impl-trait/bivariant-lifetime-liveness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// check-pass
// issue: 116794

// Uncaptured lifetimes should not be required to be live.

struct Invariant<T>(*mut T);

fn opaque<'a: 'a>(_: &'a str) -> Invariant<impl Sized> {
Invariant(&mut ())
}

fn main() {
let x = opaque(&String::new());
drop(x);
}

0 comments on commit f0e385d

Please sign in to comment.