]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/variance-intersection-of-ref-and-opt-ref.rs
af06fe381368be0db0e2e29db81061d0e61f4a47
[rust.git] / src / test / run-pass / variance-intersection-of-ref-and-opt-ref.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 // Elaborated version of the opening example from RFC 738. This failed
12 // to compile before variance because invariance of `Option` prevented
13 // us from approximating the lifetimes of `field1` and `field2` to a
14 // common intersection.
15
16
17 #![allow(dead_code)]
18 #![feature(core)]
19
20 struct List<'l> {
21     field1: &'l i32,
22     field2: Option<&'l i32>,
23 }
24
25 fn foo(field1: &i32, field2: Option<&i32>) -> i32 {
26     let list = List { field1: field1, field2: field2 };
27     *list.field1 + list.field2.cloned().unwrap_or(0)
28 }
29
30 fn main() {
31     let x = 22;
32     let y = Some(3);
33     let z = None;
34     assert_eq!(foo(&x, y.as_ref()), 25);
35     assert_eq!(foo(&x, z.as_ref()), 22);
36 }