auto merge of #16917 : nick29581/rust/cross-trait, r=pcwalton
Closes #15349 r? @pcwalton (or anyone else)
This commit is contained in:
commit
f7ec95efbb
14 changed files with 54 additions and 25 deletions
22
src/test/compile-fail/cross-borrow-trait.rs
Normal file
22
src/test/compile-fail/cross-borrow-trait.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// Copyright 2012-2013-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 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// Test that cross-borrowing (implicitly converting from `Box<T>` to `&T`) is
|
||||
// forbidden when `T` is a trait.
|
||||
|
||||
struct Foo;
|
||||
trait Trait {}
|
||||
impl Trait for Foo {}
|
||||
|
||||
pub fn main() {
|
||||
let x: Box<Trait> = box Foo;
|
||||
let _y: &Trait = x; //~ ERROR mismatched types: expected `&Trait`, found `Box<Trait>`
|
||||
}
|
||||
|
||||
|
|
@ -16,19 +16,19 @@ trait X {}
|
|||
impl<'a, T> X for B<'a, T> {}
|
||||
|
||||
fn f<'a, T, U>(v: Box<A<T>+'static>) -> Box<X+'static> {
|
||||
box B(v) as Box<X>
|
||||
box B(&*v) as Box<X>
|
||||
}
|
||||
|
||||
fn g<'a, T: 'static>(v: Box<A<T>>) -> Box<X+'static> {
|
||||
box B(v) as Box<X> //~ ERROR cannot infer
|
||||
box B(&*v) as Box<X> //~ ERROR cannot infer
|
||||
}
|
||||
|
||||
fn h<'a, T, U>(v: Box<A<U>+'static>) -> Box<X+'static> {
|
||||
box B(v) as Box<X>
|
||||
box B(&*v) as Box<X>
|
||||
}
|
||||
|
||||
fn i<'a, T, U>(v: Box<A<U>>) -> Box<X+'static> {
|
||||
box B(v) as Box<X> //~ ERROR cannot infer
|
||||
box B(&*v) as Box<X> //~ ERROR cannot infer
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ impl Trait for Foo {}
|
|||
|
||||
pub fn main() {
|
||||
{
|
||||
let _x: &Trait = box Foo as Box<Trait>;
|
||||
let _x: &Trait = &*(box Foo as Box<Trait>);
|
||||
}
|
||||
unsafe {
|
||||
assert!(DROP_RAN);
|
||||
|
|
|
|||
|
|
@ -36,5 +36,5 @@ pub fn main() {
|
|||
let s: Box<S> = box S { s: 5 };
|
||||
print_s(&*s);
|
||||
let t: Box<T> = s as Box<T>;
|
||||
print_t(t);
|
||||
print_t(&*t);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ impl Trait for Struct {
|
|||
|
||||
fn g(x: Box<Trait>) {
|
||||
x.printme();
|
||||
let y: &Trait = x;
|
||||
let y: &Trait = &*x;
|
||||
y.printme();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -120,8 +120,8 @@ pub fn main() {
|
|||
assert_eq!(field_invoke2(&s2), 3);
|
||||
|
||||
let m : Box<Trait> = make_val();
|
||||
assert_eq!(object_invoke1(m), (4,5));
|
||||
assert_eq!(object_invoke2(m), 5);
|
||||
assert_eq!(object_invoke1(&*m), (4,5));
|
||||
assert_eq!(object_invoke2(&*m), 5);
|
||||
|
||||
// The RefMakerTrait above is pretty strange (i.e. it is strange
|
||||
// to consume a value of type T and return a &T). Easiest thing
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue