]> git.proxmox.com Git - rustc.git/blob - src/test/ui/wf/wf-static-method.rs
New upstream version 1.62.1+dfsg1
[rustc.git] / src / test / ui / wf / wf-static-method.rs
1 // check that static methods don't get to assume their trait-ref
2 // is well-formed.
3 // FIXME(#27579): this is just a bug. However, our checking with
4 // static inherent methods isn't quite working - need to
5 // fix that before removing the check.
6
7 // revisions: base nll
8 // ignore-compare-mode-nll
9 //[nll] compile-flags: -Z borrowck=mir
10
11 trait Foo<'a, 'b, T>: Sized {
12 fn make_me() -> Self { loop {} }
13 fn static_evil(u: &'b u32) -> &'a u32;
14 }
15
16 struct Evil<'a, 'b: 'a>(Option<&'a &'b ()>);
17
18 impl<'a, 'b> Foo<'a, 'b, Evil<'a, 'b>> for () {
19 fn make_me() -> Self { }
20 fn static_evil(u: &'b u32) -> &'a u32 {
21 u
22 //[base]~^ ERROR E0312
23 //[nll]~^^ ERROR lifetime may not live long enough
24 }
25 }
26
27 struct IndirectEvil<'a, 'b: 'a>(Option<&'a &'b ()>);
28
29 impl<'a, 'b> Foo<'a, 'b, ()> for IndirectEvil<'a, 'b> {
30 fn make_me() -> Self { IndirectEvil(None) }
31 fn static_evil(u: &'b u32) -> &'a u32 {
32 let me = Self::make_me();
33 //[base]~^ ERROR lifetime bound not satisfied
34 //[nll]~^^ ERROR lifetime may not live long enough
35 loop {} // (`me` could be used for the lifetime transmute).
36 }
37 }
38
39 impl<'a, 'b> Evil<'a, 'b> {
40 fn inherent_evil(u: &'b u32) -> &'a u32 {
41 u
42 //[base]~^ ERROR E0312
43 //[nll]~^^ ERROR lifetime may not live long enough
44 }
45 }
46
47 // while static methods don't get to *assume* this, we still
48 // *check* that they hold.
49
50 fn evil<'a, 'b>(b: &'b u32) -> &'a u32 {
51 <()>::static_evil(b)
52 //[base]~^ ERROR cannot infer an appropriate lifetime
53 //[nll]~^^ ERROR lifetime may not live long enough
54 }
55
56 fn indirect_evil<'a, 'b>(b: &'b u32) -> &'a u32 {
57 <IndirectEvil>::static_evil(b)
58 //[base]~^ ERROR cannot infer an appropriate lifetime
59 //[nll]~^^ ERROR lifetime may not live long enough
60 }
61
62 fn inherent_evil<'a, 'b>(b: &'b u32) -> &'a u32 {
63 <Evil>::inherent_evil(b)
64 //[base]~^ ERROR cannot infer an appropriate lifetime
65 //[nll]~^^ ERROR lifetime may not live long enough
66 }
67
68
69 fn main() {}