Skip to content

Commit 2c9aa28

Browse files
committed
Add tests for two untested cases of placeholder relations
During work on rust-lang#130227, I discovered several situations not covered by any previously existing UI test. This commit introudces tests to cover that.
1 parent c4b38a5 commit 2c9aa28

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// This test is a reduced version of a bug introduced during work on type-tests for Polonius.
2+
// The underlying problem is that the 'static bound is lost for a closure that is threaded
3+
// deeply enough, causing an error.
4+
// The bug was first observed in exr-1.4.1/src/image/read/mod.rs:124:5 during perf test.
5+
6+
//@ check-pass
7+
8+
use std::marker::PhantomData;
9+
10+
struct ReadAllLayers<ReadChannels> {
11+
px: PhantomData<ReadChannels>,
12+
}
13+
14+
trait ReadLayers<'s> {}
15+
16+
impl<'s, C> ReadLayers<'s> for ReadAllLayers<C> where C: ReadChannels<'s> {}
17+
18+
fn make_builder<A, Set, Pixels>(
19+
_: Set,
20+
) -> ReadAllLayers<CollectPixels<A, Pixels, Set>>
21+
where
22+
Set: Fn(&mut Pixels),
23+
{
24+
todo!()
25+
}
26+
27+
struct CollectPixels<Pixel, PixelStorage, SetPixel> {
28+
px: PhantomData<(SetPixel, Pixel, PixelStorage)>,
29+
}
30+
31+
impl<'s, PixelStorage, SetPixel: 's> ReadChannels<'s>
32+
for CollectPixels<usize, PixelStorage, SetPixel>
33+
where
34+
SetPixel: Fn(&mut PixelStorage),
35+
{
36+
}
37+
38+
trait ReadChannels<'s> {}
39+
40+
fn from_file<L>(_: L)
41+
where
42+
for<'s> L: ReadLayers<'s>,
43+
{
44+
}
45+
46+
pub fn read_all_rgba_layers_from_file<Set: 'static, Pixels: 'static>(
47+
set_pixel: Set,
48+
) where
49+
Set: Fn(&mut Pixels),
50+
{
51+
from_file(make_builder(set_pixel)); // Error triggered.
52+
}
53+
54+
pub fn main() {}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Test that the preprocessing step correctly handles some
2+
// cases of placeholder leaks.
3+
//
4+
//@ compile-flags:-Zno-leak-check
5+
6+
7+
struct Co<'a>(&'a ());
8+
struct Inv<'a>(*mut &'a ());
9+
struct Contra<'a>(fn(&'a ()));
10+
11+
// `exists<'e> forall<'p> 'p: 'e` -> ERROR
12+
fn p_outlives_e(
13+
x: for<'e> fn(for<'p> fn(fn(fn(Contra<'e>, Co<'p>)))),
14+
) -> fn(fn(fn(for<'unify> fn(Contra<'unify>, Co<'unify>)))) {
15+
x //~ ERROR mismatched types [E0308]
16+
}
17+
18+
// `exists<'e> forall<'p> 'e: 'p` -> Ok, 'e: 'static
19+
fn e_outlives_p_static(
20+
x: for<'e> fn(Inv<'e>, for<'p> fn(fn(fn(Contra<'p>, Co<'e>)))),
21+
) -> fn(Inv<'static>, fn(fn(for<'unify> fn(Contra<'unify>, Co<'unify>)))) {
22+
x
23+
}
24+
25+
// `exists<'e> forall<'p> 'e: 'p` -> Ok, 'e: 'static -> ERROR
26+
fn e_outlives_p_static_err<'not_static>(
27+
x: for<'e> fn(Inv<'e>, for<'p> fn(fn(fn(Contra<'p>, Co<'e>)))),
28+
) -> fn(Inv<'not_static>, fn(fn(for<'unify> fn(Contra<'unify>, Co<'unify>)))) {
29+
x //~ ERROR lifetime may not live long enough
30+
}
31+
32+
fn main() {}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/placeholder-outlives-existential.rs:15:5
3+
|
4+
LL | x
5+
| ^ one type is more general than the other
6+
|
7+
= note: expected fn pointer `fn(fn(fn(for<'unify> fn(Contra<'unify>, Co<'unify>))))`
8+
found fn pointer `for<'e> fn(for<'e, 'p> fn(for<'e, 'p> fn(for<'e, 'p> fn(Contra<'e>, Co<'p>))))`
9+
10+
error: lifetime may not live long enough
11+
--> $DIR/placeholder-outlives-existential.rs:29:5
12+
|
13+
LL | fn e_outlives_p_static_err<'not_static>(
14+
| ----------- lifetime `'not_static` defined here
15+
...
16+
LL | x
17+
| ^ returning this value requires that `'not_static` must outlive `'static`
18+
|
19+
= note: requirement occurs because of the type `Inv<'_>`, which makes the generic argument `'_` invariant
20+
= note: the struct `Inv<'a>` is invariant over the parameter `'a`
21+
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
22+
23+
error: aborting due to 2 previous errors
24+
25+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)