]> git.lizzy.rs Git - rust.git/blob - src/test/ui/wf/wf-static-method.rs
Auto merge of #54720 - davidtwco:issue-51191, r=nikomatsakis
[rust.git] / src / test / ui / wf / wf-static-method.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // check that static methods don't get to assume their trait-ref
12 // is well-formed.
13 // FIXME(#27579): this is just a bug. However, our checking with
14 // static inherent methods isn't quite working - need to
15 // fix that before removing the check.
16
17 trait Foo<'a, 'b, T>: Sized {
18     fn make_me() -> Self { loop {} }
19     fn static_evil(u: &'b u32) -> &'a u32;
20 }
21
22 struct Evil<'a, 'b: 'a>(Option<&'a &'b ()>);
23
24 impl<'a, 'b> Foo<'a, 'b, Evil<'a, 'b>> for () {
25     fn make_me() -> Self { }
26     fn static_evil(u: &'b u32) -> &'a u32 {
27         u //~ ERROR E0312
28     }
29 }
30
31 struct IndirectEvil<'a, 'b: 'a>(Option<&'a &'b ()>);
32
33 impl<'a, 'b> Foo<'a, 'b, ()> for IndirectEvil<'a, 'b> {
34     fn make_me() -> Self { IndirectEvil(None) }
35     fn static_evil(u: &'b u32) -> &'a u32 {
36         let me = Self::make_me(); //~ ERROR lifetime bound not satisfied
37         loop {} // (`me` could be used for the lifetime transmute).
38     }
39 }
40
41 impl<'a, 'b> Evil<'a, 'b> {
42     fn inherent_evil(u: &'b u32) -> &'a u32 {
43         u //~ ERROR E0312
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) //~ ERROR cannot infer an appropriate lifetime
52 }
53
54 fn indirect_evil<'a, 'b>(b: &'b u32) -> &'a u32 {
55     <IndirectEvil>::static_evil(b)
56     //~^ ERROR cannot infer an appropriate lifetime
57 }
58
59 fn inherent_evil<'a, 'b>(b: &'b u32) -> &'a u32 {
60     <Evil>::inherent_evil(b) // bug? shouldn't this be an error
61 }
62
63
64 fn main() {}