]> git.lizzy.rs Git - rust.git/blob - tests/ui/type-id-higher-rank-2.rs
Rollup merge of #106625 - Swatinem:ref/cov6, r=nagisa
[rust.git] / tests / ui / type-id-higher-rank-2.rs
1 // run-pass
2 // Test that we can't ignore lifetimes by going through Any.
3
4 use std::any::Any;
5
6 struct Foo<'a>(&'a str);
7
8 fn good(s: &String) -> Foo { Foo(s) }
9
10 fn bad1(s: String) -> Option<&'static str> {
11     let a: Box<dyn Any> = Box::new(good as fn(&String) -> Foo);
12     a.downcast_ref::<fn(&String) -> Foo<'static>>().map(|f| f(&s).0)
13 }
14
15 trait AsStr<'a, 'b> {
16     fn get(&'a self) -> &'b str;
17 }
18
19 impl<'a> AsStr<'a, 'a> for String {
20    fn get(&'a self) -> &'a str { self }
21 }
22
23 fn bad2(s: String) -> Option<&'static str> {
24     let a: Box<dyn Any> = Box::new(Box::new(s) as Box<dyn for<'a> AsStr<'a, 'a>>);
25     a.downcast_ref::<Box<dyn for<'a> AsStr<'a, 'static>>>().map(|x| x.get())
26 }
27
28 fn main() {
29     assert_eq!(bad1(String::from("foo")), None);
30     assert_eq!(bad2(String::from("bar")), None);
31 }