|
| 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 | +// Given `<expr> as Box<Trait>`, we should be able to infer that a |
| 12 | +// `Box<_>` is the expected type. |
| 13 | + |
| 14 | +trait Foo { fn foo(&self) -> u32; } |
| 15 | +impl Foo for u32 { fn foo(&self) -> u32 { *self } } |
| 16 | + |
| 17 | +// (another impl to ensure trait-matching cannot just choose from a singleton set) |
| 18 | +impl Foo for () { fn foo(&self) -> u32 { -176 } } |
| 19 | + |
| 20 | +trait Boxed { fn make() -> Self; } |
| 21 | +impl Boxed for Box<u32> { fn make() -> Self { Box::new(7) } } |
| 22 | + |
| 23 | +// (another impl to ensure trait-matching cannot just choose from a singleton set) |
| 24 | +impl Boxed for () { fn make() -> Self { () } } |
| 25 | + |
| 26 | +fn boxed_foo() { |
| 27 | + let b7 = Boxed::make() as Box<Foo>; |
| 28 | + assert_eq!(b7.foo(), 7); |
| 29 | +} |
| 30 | + |
| 31 | +trait Refed<'a,T> { fn make(&'a T) -> Self; } |
| 32 | +impl<'a> Refed<'a, u32> for &'a u32 { fn make(x: &'a u32) -> Self { x } } |
| 33 | + |
| 34 | +// (another impl to ensure trait-matching cannot just choose from a singleton set) |
| 35 | +impl<'a,'b> Refed<'a, ()> for &'b () { fn make(_: &'a ()) -> Self { static U: () = (); &U } } |
| 36 | + |
| 37 | +fn refed_foo() { |
| 38 | + let a = 8; |
| 39 | + let b7 = Refed::make(&a) as &Foo; |
| 40 | + assert_eq!(b7.foo(), 8); |
| 41 | +} |
| 42 | + |
| 43 | +fn check_subtyping_works() { |
| 44 | + fn inner<'short, 'long:'short>(_s: &'short u32, |
| 45 | + l: &'long u32) -> &'short (Foo+'short) { |
| 46 | + Refed::make(l) as &Foo |
| 47 | + } |
| 48 | + |
| 49 | + let a = 9; |
| 50 | + let b = 10; |
| 51 | + let r = inner(&b, &a); |
| 52 | + assert_eq!(r.foo(), 9); |
| 53 | +} |
| 54 | + |
| 55 | +pub fn main() { |
| 56 | + boxed_foo(); |
| 57 | + refed_foo(); |
| 58 | + check_subtyping_works(); |
| 59 | +} |
0 commit comments