]> git.lizzy.rs Git - rust.git/blob - tests/ui/variance-intersection-of-ref-and-opt-ref.rs
Account for method call and indexing when looking for inner-most path in expression
[rust.git] / tests / ui / variance-intersection-of-ref-and-opt-ref.rs
1 // run-pass
2 // Elaborated version of the opening example from RFC 738. This failed
3 // to compile before variance because invariance of `Option` prevented
4 // us from approximating the lifetimes of `field1` and `field2` to a
5 // common intersection.
6
7 #![allow(dead_code)]
8
9 struct List<'l> {
10     field1: &'l i32,
11     field2: Option<&'l i32>,
12 }
13
14 fn foo(field1: &i32, field2: Option<&i32>) -> i32 {
15     let list = List { field1: field1, field2: field2 };
16     *list.field1 + list.field2.cloned().unwrap_or(0)
17 }
18
19 fn main() {
20     let x = 22;
21     let y = Some(3);
22     let z = None;
23     assert_eq!(foo(&x, y.as_ref()), 25);
24     assert_eq!(foo(&x, z.as_ref()), 22);
25 }