summaryrefslogtreecommitdiff
path: root/tests/ui/specialization/min_specialization/specialize_with_generalize_lifetimes.rs
blob: d90b81f717a6c38454cf2900f2aecda88e833c25 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Regression test for #79457.

#![feature(min_specialization)]

use std::any::Any;

pub trait Tr {
    fn method(self) -> Box<dyn Any + 'static>;
    fn other(self);
}

impl<T: Any + 'static> Tr for T {
    default fn method(self) -> Box<dyn Any + 'static> {
        Box::new(self)
    }

    default fn other(self) {}
}

impl<'a> Tr for &'a i32 {
    //~^ ERROR does not fulfill the required lifetime
    fn other(self) {}
}

fn promote_to_static<'a>(i: &'a i32) -> &'static i32 {
    *i.method().downcast().unwrap()
}

struct Wrapper<'a>(&'a i32);

impl<'a> Tr for Wrapper<'a> {
    //~^ ERROR does not fulfill the required lifetime
    fn other(self) {}
}

fn promote_to_static_2<'a>(w: Wrapper<'a>) -> Wrapper<'static> {
    *w.method().downcast().unwrap()
}

fn main() {
    let i = Box::new(100_i32);
    let static_i: &'static i32 = promote_to_static(&*i);
    drop(i);
    println!("{}", *static_i);

    let j = Box::new(200_i32);
    let static_w: Wrapper<'static> = promote_to_static_2(Wrapper(&*j));
    drop(j);
    println!("{}", *static_w.0);
}