diff --git a/src/test/run-pass/trait-contravariant-self.rs b/src/test/run-pass/trait-contravariant-self.rs new file mode 100644 index 0000000000000..1576c646286a4 --- /dev/null +++ b/src/test/run-pass/trait-contravariant-self.rs @@ -0,0 +1,37 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// This is an interesting test case. We have a trait (Bar) that is +// implemented for a `Box` object (note: no bounds). And then we +// have a `Box` object. The impl for `Box` is applicable +// to `Box` because: +// +// 1. The trait Bar is contravariant w/r/t Self because `Self` appears +// only in argument position. +// 2. The impl provides `Bar for Box` +// 3. The fn `wants_bar()` requires `Bar for Box`. +// 4. `Bar for Box <: Bar for Box` because +// `Box <: Box`. + +trait Foo { } +struct SFoo; +impl Foo for SFoo { } + +trait Bar { fn dummy(&self); } +impl Bar for Box { fn dummy(&self) { } } + +fn wants_bar(b: &B) { } + +fn main() { + let x: Box = (box SFoo); + wants_bar(&x); +} + +