]> git.lizzy.rs Git - rust.git/blob - src/test/ui/associated-types/cache/project-fn-ret-invariant.rs
1075fd6e0923a896f079d1b974db9e17faee368a
[rust.git] / src / test / ui / associated-types / cache / project-fn-ret-invariant.rs
1 #![feature(unboxed_closures)]
2 // Test for projection cache. We should be able to project distinct
3 // lifetimes from `foo` as we reinstantiate it multiple times, but not
4 // if we do it just once. In this variant, the region `'a` is used in
5 // an invariant position, which affects the results.
6
7 // revisions: ok oneuse transmute krisskross
8 //[ok] check-pass
9
10 #![allow(dead_code, unused_variables)]
11
12 use std::marker::PhantomData;
13
14 struct Type<'a> {
15     // Invariant
16     data: PhantomData<fn(&'a u32) -> &'a u32>,
17 }
18
19 fn foo<'a>() -> Type<'a> {
20     loop {}
21 }
22
23 fn bar<T>(t: T, x: T::Output) -> T::Output
24 where
25     T: FnOnce<()>,
26 {
27     t()
28 }
29
30 #[cfg(ok)] // two instantiations: OK
31 fn baz<'a, 'b>(x: Type<'a>, y: Type<'b>) -> (Type<'a>, Type<'b>) {
32     let a = bar(foo, x);
33     let b = bar(foo, y);
34     (a, b)
35 }
36
37 #[cfg(oneuse)] // one instantiation: BAD
38 fn baz<'a, 'b>(x: Type<'a>, y: Type<'b>) -> (Type<'a>, Type<'b>) {
39     let f = foo; // <-- No consistent type can be inferred for `f` here.
40     let a = bar(f, x);
41     //[oneuse]~^ ERROR lifetime may not live long enough
42     //[oneuse]~| ERROR lifetime may not live long enough
43     let b = bar(f, y);
44     (a, b)
45 }
46
47 #[cfg(transmute)] // one instantiations: BAD
48 fn baz<'a, 'b>(x: Type<'a>) -> Type<'static> {
49     // Cannot instantiate `foo` with any lifetime other than `'a`,
50     // since it is provided as input.
51
52     bar(foo, x) //[transmute]~ ERROR lifetime may not live long enough
53 }
54
55 #[cfg(krisskross)] // two instantiations, mixing and matching: BAD
56 fn transmute<'a, 'b>(x: Type<'a>, y: Type<'b>) -> (Type<'a>, Type<'b>) {
57     let a = bar(foo, y);
58     let b = bar(foo, x);
59     (a, b)
60     //[krisskross]~^ ERROR lifetime may not live long enough
61     //[krisskross]~| ERROR lifetime may not live long enough
62 }
63
64 fn main() {}