]> git.proxmox.com Git - rustc.git/blame - src/test/ui/associated-types/cache/project-fn-ret-contravariant.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / src / test / ui / associated-types / cache / project-fn-ret-contravariant.rs
CommitLineData
3157f602 1#![feature(unboxed_closures)]
3157f602
XL
2
3// Test for projection cache. We should be able to project distinct
4// lifetimes from `foo` as we reinstantiate it multiple times, but not
5// if we do it just once. In this variant, the region `'a` is used in
6// an contravariant position, which affects the results.
7
8// revisions: ok oneuse transmute krisskross
923072b8
FG
9//[ok] check-pass
10//[oneuse] check-pass
3157f602
XL
11
12#![allow(dead_code, unused_variables)]
13
14fn foo<'a>() -> &'a u32 { loop { } }
15
16fn bar<T>(t: T, x: T::Output) -> T::Output
17 where T: FnOnce<()>
18{
19 t()
20}
21
22#[cfg(ok)] // two instantiations: OK
23fn baz<'a,'b>(x: &'a u32, y: &'b u32) -> (&'a u32, &'b u32) {
24 let a = bar(foo, x);
25 let b = bar(foo, y);
26 (a, b)
27}
28
29#[cfg(oneuse)] // one instantiation: OK (surprisingly)
30fn baz<'a,'b>(x: &'a u32, y: &'b u32) -> (&'a u32, &'b u32) {
31 let f /* : fn() -> &'static u32 */ = foo; // <-- inferred type annotated
32 let a = bar(f, x); // this is considered ok because fn args are contravariant...
33 let b = bar(f, y); // ...and hence we infer T to distinct values in each call.
34 (a, b)
35}
36
ea8adc8c
XL
37#[cfg(transmute)] // one instantiations: BAD
38fn baz<'a,'b>(x: &'a u32) -> &'static u32 {
923072b8 39 bar(foo, x) //[transmute]~ ERROR lifetime may not live long enough
ea8adc8c 40}
3157f602 41
ea8adc8c
XL
42#[cfg(krisskross)] // two instantiations, mixing and matching: BAD
43fn transmute<'a,'b>(x: &'a u32, y: &'b u32) -> (&'a u32, &'b u32) {
abe05a73
XL
44 let a = bar(foo, y);
45 let b = bar(foo, x);
923072b8
FG
46 (a, b) //[krisskross]~ ERROR lifetime may not live long enough
47 //[krisskross]~^ ERROR lifetime may not live long enough
ea8adc8c 48}
3157f602 49
923072b8 50fn main() { }