]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/variance-intersection-of-ref-and-opt-ref.rs
Fix run-pass-fulldeps tests
[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 #![allow(dead_code)]
17
18 struct List<'l> {
19     field1: &'l i32,
20     field2: Option<&'l i32>,
21 }
22
23 fn foo(field1: &i32, field2: Option<&i32>) -> i32 {
24     let list = List { field1: field1, field2: field2 };
25     *list.field1 + list.field2.cloned().unwrap_or(0)
26 }
27
28 fn main() {
29     let x = 22;
30     let y = Some(3);
31     let z = None;
32     assert_eq!(foo(&x, y.as_ref()), 25);
33     assert_eq!(foo(&x, z.as_ref()), 22);
34 }