Skip to content

Commit f0e385d

Browse files
Flesh out tests more
1 parent 1005970 commit f0e385d

11 files changed

+143
-7
lines changed

tests/ui/borrowck/alias-liveness/gat-static.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ trait Foo {
88
fn assoc(&mut self) -> Self::Assoc<'_>;
99
}
1010

11-
fn test<T>(mut t: T)
11+
fn overlapping_mut<T>(mut t: T)
1212
where
1313
T: Foo,
1414
for<'a> T::Assoc<'a>: 'static,
@@ -17,4 +17,13 @@ where
1717
let b = t.assoc();
1818
}
1919

20+
fn live_past_borrow<T>(mut t: T)
21+
where
22+
T: Foo,
23+
for<'a> T::Assoc<'a>: 'static {
24+
let x = t.assoc();
25+
drop(t);
26+
drop(x);
27+
}
28+
2029
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// known-bug: #42940
2+
3+
trait Captures<'a> {}
4+
impl<T> Captures<'_> for T {}
5+
6+
trait Outlives<'a>: 'a {}
7+
impl<'a, T: 'a> Outlives<'a> for T {}
8+
9+
// Test that we treat `for<'a> Opaque: 'a` as `Opaque: 'static`
10+
fn test<'o>(v: &'o Vec<i32>) -> impl Captures<'o> + for<'a> Outlives<'a> {}
11+
12+
fn statik() -> impl Sized {
13+
test(&vec![])
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0716]: temporary value dropped while borrowed
2+
--> $DIR/higher-ranked-outlives-for-capture.rs:13:11
3+
|
4+
LL | test(&vec![])
5+
| ------^^^^^^-
6+
| | |
7+
| | creates a temporary value which is freed while still in use
8+
| argument requires that borrow lasts for `'static`
9+
LL | }
10+
| - temporary value is freed at the end of this statement
11+
|
12+
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0716`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// check-pass
2+
3+
trait Captures<'a> {}
4+
impl<T> Captures<'_> for T {}
5+
6+
trait Outlives<'a>: 'a {}
7+
impl<'a, T: 'a> Outlives<'a> for T {}
8+
9+
// Test that we treat `for<'a> Opaque: 'a` as `Opaque: 'static`
10+
fn test<'o>(v: &'o Vec<i32>) -> impl Captures<'o> + for<'a> Outlives<'a> {}
11+
12+
fn opaque_doesnt_use_temporary() {
13+
let a = test(&vec![]);
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// check-pass
2+
3+
// Check that opaques capturing early and late-bound vars correctly mark
4+
// regions required to be live using the item bounds.
5+
6+
trait Captures<'a> {}
7+
impl<T> Captures<'_> for T {}
8+
9+
fn captures_temp_late<'a>(x: &'a Vec<i32>) -> impl Sized + Captures<'a> + 'static {}
10+
fn captures_temp_early<'a: 'a>(x: &'a Vec<i32>) -> impl Sized + Captures<'a> + 'static {}
11+
12+
fn test() {
13+
let x = captures_temp_early(&vec![]);
14+
let y = captures_temp_late(&vec![]);
15+
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// known-bug: #42940
2+
3+
trait Trait {}
4+
impl Trait for () {}
5+
6+
fn foo<'a>(s: &'a str) -> impl Trait + 'static {
7+
bar(s)
8+
}
9+
10+
fn bar<P: AsRef<str>>(s: P) -> impl Trait + 'static {
11+
()
12+
}
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0700]: hidden type for `impl Trait + 'static` captures lifetime that does not appear in bounds
2+
--> $DIR/opaque-type-param.rs:7:5
3+
|
4+
LL | fn foo<'a>(s: &'a str) -> impl Trait + 'static {
5+
| -- -------------------- opaque type defined here
6+
| |
7+
| hidden type `impl Trait + 'static` captures the lifetime `'a` as defined here
8+
LL | bar(s)
9+
| ^^^^^^
10+
11+
error: aborting due to previous error
12+
13+
For more information about this error, try `rustc --explain E0700`.

tests/ui/borrowck/alias-liveness/rpit-static.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33
trait Captures<'a> {}
44
impl<T> Captures<'_> for T {}
55

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

8-
fn main() {
8+
fn overlapping_mut() {
9+
let i = &mut 1;
10+
let x = foo(i);
11+
let y = foo(i);
12+
}
13+
14+
fn live_past_borrow() {
915
let y;
1016
{
11-
let x = 1;
12-
y = foo(&x);
17+
let x = &mut 1;
18+
y = foo(x);
1319
}
1420
}
21+
22+
fn main() {}

tests/ui/borrowck/alias-liveness/rpitit-static.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ trait Foo {
44
fn rpitit(&mut self) -> impl Sized + 'static;
55
}
66

7-
fn test<T: Foo>(mut t: T) {
7+
fn live_past_borrow<T: Foo>(mut t: T) {
8+
let x = t.rpitit();
9+
drop(t);
10+
drop(x);
11+
}
12+
13+
fn overlapping_mut<T: Foo>(mut t: T) {
814
let a = t.rpitit();
915
let b = t.rpitit();
1016
}

tests/ui/borrowck/alias-liveness/rtn-static.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ trait Foo {
77
fn borrow(&mut self) -> impl Sized + '_;
88
}
99

10+
fn live_past_borrow<T: Foo<borrow(): 'static>>(mut t: T) {
11+
let x = t.borrow();
12+
drop(t);
13+
drop(x);
14+
}
15+
1016
// Test that the `'_` item bound in `borrow` does not cause us to
1117
// overlook the `'static` RTN bound.
12-
fn test<T: Foo<borrow(): 'static>>(mut t: T) {
18+
fn overlapping_mut<T: Foo<borrow(): 'static>>(mut t: T) {
1319
let x = t.borrow();
1420
let x = t.borrow();
1521
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// check-pass
2+
// issue: 116794
3+
4+
// Uncaptured lifetimes should not be required to be live.
5+
6+
struct Invariant<T>(*mut T);
7+
8+
fn opaque<'a: 'a>(_: &'a str) -> Invariant<impl Sized> {
9+
Invariant(&mut ())
10+
}
11+
12+
fn main() {
13+
let x = opaque(&String::new());
14+
drop(x);
15+
}

0 commit comments

Comments
 (0)