summaryrefslogtreecommitdiffstats
path: root/tests/ui/traits/object/issue-44454-3.rs
blob: bff727035346405c006b9365637a671359b7b3e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Taken from https://github.com/rust-lang/rust/issues/44454#issuecomment-1332781290

use std::any::Any;

trait Animal<X>: 'static {}

trait Projector {
    type Foo;
}

impl<X> Projector for dyn Animal<X> {
    type Foo = X;
}

fn make_static<'a, T>(t: &'a T) -> &'static T {
    let x: <dyn Animal<&'a T> as Projector>::Foo = t;
    let any = generic::<dyn Animal<&'a T>, &'a T>(x);
    //~^ ERROR: lifetime may not live long enough
    any.downcast_ref::<&'static T>().unwrap()
}

fn generic<T: Projector + Animal<U> + ?Sized, U>(x: <T as Projector>::Foo) -> Box<dyn Any> {
    make_static_any(x)
}

fn make_static_any<U: 'static>(u: U) -> Box<dyn Any> {
    Box::new(u)
}

fn main() {
    let a = make_static(&"salut".to_string());
    println!("{}", *a);
}