]> git.lizzy.rs Git - rust.git/blob - src/test/ui/regions/regions-trait-1.rs
Rollup merge of #106043 - c410-f3r:moar-errors, r=petrochenkov
[rust.git] / src / test / ui / regions / regions-trait-1.rs
1 // check-pass
2
3 struct Ctxt {
4     v: usize,
5 }
6
7 trait GetCtxt {
8     // Here the `&` is bound in the method definition:
9     fn get_ctxt(&self) -> &Ctxt;
10 }
11
12 struct HasCtxt<'a> {
13     c: &'a Ctxt,
14 }
15
16 impl<'a> GetCtxt for HasCtxt<'a> {
17     // Ok: Have implied bound of WF(&'b HasCtxt<'a>)
18     // so know 'a: 'b
19     // so know &'a Ctxt <: &'b Ctxt
20     fn get_ctxt<'b>(&'b self) -> &'a Ctxt {
21         self.c
22     }
23 }
24
25 fn get_v(gc: Box<dyn GetCtxt + '_>) -> usize {
26     gc.get_ctxt().v
27 }
28
29 fn main() {
30     let ctxt = Ctxt { v: 22 };
31     let hc = HasCtxt { c: &ctxt };
32     assert_eq!(get_v(Box::new(hc) as Box<dyn GetCtxt>), 22);
33 }