Skip to content

Commit

Permalink
auto merge of #16917 : nick29581/rust/cross-trait, r=pcwalton
Browse files Browse the repository at this point in the history
Closes #15349

r? @pcwalton (or anyone else)
  • Loading branch information
bors committed Sep 3, 2014
2 parents 2e38581 + 7f72884 commit f7ec95e
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/librustc/middle/trans/cleanup.rs
Expand Up @@ -640,7 +640,7 @@ impl<'a> CleanupHelperMethods<'a> for FunctionContext<'a> {
while !popped_scopes.is_empty() {
let mut scope = popped_scopes.pop().unwrap();

if scope.cleanups.iter().any(|c| cleanup_is_suitable_for(*c, label))
if scope.cleanups.iter().any(|c| cleanup_is_suitable_for(&**c, label))
{
let name = scope.block_name("clean");
debug!("generating cleanups for {}", name);
Expand All @@ -649,7 +649,7 @@ impl<'a> CleanupHelperMethods<'a> for FunctionContext<'a> {
None);
let mut bcx_out = bcx_in;
for cleanup in scope.cleanups.iter().rev() {
if cleanup_is_suitable_for(*cleanup, label) {
if cleanup_is_suitable_for(&**cleanup, label) {
bcx_out = cleanup.trans(bcx_out);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/middle/trans/datum.rs
Expand Up @@ -451,6 +451,8 @@ impl Datum<Expr> {
name: &str,
expr_id: ast::NodeId)
-> DatumBlock<'a, Lvalue> {
debug!("to_lvalue_datum self: {}", self.to_string(bcx.ccx()));

assert!(ty::lltype_is_sized(bcx.tcx(), self.ty),
"Trying to convert unsized value to lval");
self.match_kind(
Expand Down
18 changes: 12 additions & 6 deletions src/librustc/middle/trans/expr.rs
Expand Up @@ -2061,11 +2061,17 @@ fn deref_once<'a>(bcx: &'a Block<'a>,
if ty::type_is_sized(bcx.tcx(), content_ty) {
deref_owned_pointer(bcx, expr, datum, content_ty)
} else {
// A fat pointer and an opened DST value have the same represenation
// just different types.
DatumBlock::new(bcx, Datum::new(datum.val,
ty::mk_open(bcx.tcx(), content_ty),
datum.kind))
// A fat pointer and an opened DST value have the same
// represenation just different types. Since there is no
// temporary for `*e` here (because it is unsized), we cannot
// emulate the sized object code path for running drop glue and
// free. Instead, we schedule cleanup for `e`, turning it into
// an lvalue.
let datum = unpack_datum!(
bcx, datum.to_lvalue_datum(bcx, "deref", expr.id));

let datum = Datum::new(datum.val, ty::mk_open(bcx.tcx(), content_ty), LvalueExpr);
DatumBlock::new(bcx, datum)
}
}

Expand Down Expand Up @@ -2094,7 +2100,7 @@ fn deref_once<'a>(bcx: &'a Block<'a>,
// just different types.
DatumBlock::new(bcx, Datum::new(datum.val,
ty::mk_open(bcx.tcx(), content_ty),
datum.kind))
LvalueExpr))
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/librustc/middle/typeck/infer/coercion.rs
Expand Up @@ -327,7 +327,6 @@ impl<'f> Coerce<'f> {

let sty_b = &ty::get(b).sty;
match (sty_a, sty_b) {
(&ty::ty_uniq(_), &ty::ty_rptr(..)) => Err(ty::terr_mismatch),
(&ty::ty_rptr(_, ty::mt{ty: t_a, ..}), &ty::ty_rptr(_, mt_b)) => {
self.unpack_actual_value(t_a, |sty_a| {
match self.unsize_ty(sty_a, mt_b.ty) {
Expand Down Expand Up @@ -511,7 +510,7 @@ impl<'f> Coerce<'f> {
let tcx = self.get_ref().infcx.tcx;

match *sty_a {
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty, ..}) => match ty::get(ty).sty {
ty::ty_rptr(_, ty::mt{ty, ..}) => match ty::get(ty).sty {
ty::ty_trait(box ty::TyTrait {
def_id,
ref substs,
Expand Down
2 changes: 1 addition & 1 deletion src/librustrt/unwind.rs
Expand Up @@ -570,7 +570,7 @@ fn begin_unwind_inner(msg: Box<Any + Send>, file_line: &(&'static str, uint)) ->
n => {
let f: Callback = unsafe { mem::transmute(n) };
let (file, line) = *file_line;
f(msg, file, line);
f(&*msg, file, line);
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/failure.rs
Expand Up @@ -81,7 +81,7 @@ pub fn on_fail(obj: &Any + Send, file: &'static str, line: uint) {
"task '{}' failed at '{}', {}:{}\n",
n, msg, file, line);
if backtrace::log_enabled() {
let _ = backtrace::write(stderr);
let _ = backtrace::write(&mut *stderr);
}
local_stderr.replace(Some(stderr));
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/io/stdio.rs
Expand Up @@ -203,7 +203,7 @@ fn with_task_stdout(f: |&mut Writer| -> IoResult<()>) {
let mut my_stdout = local_stdout.replace(None).unwrap_or_else(|| {
box stdout() as Box<Writer + Send>
});
let result = f(my_stdout);
let result = f(&mut *my_stdout);
local_stdout.replace(Some(my_stdout));
result
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -351,7 +351,7 @@ impl<'a> Parser<'a> {
mut rdr: Box<Reader+'a>)
-> Parser<'a>
{
let tok0 = real_token(rdr);
let tok0 = real_token(&mut *rdr);
let span = tok0.sp;
let placeholder = TokenAndSpan {
tok: token::UNDERSCORE,
Expand Down Expand Up @@ -899,7 +899,7 @@ impl<'a> Parser<'a> {
None
};
let next = if self.buffer_start == self.buffer_end {
real_token(self.reader)
real_token(&mut *self.reader)
} else {
// Avoid token copies with `replace`.
let buffer_start = self.buffer_start as uint;
Expand Down Expand Up @@ -943,7 +943,7 @@ impl<'a> Parser<'a> {
-> R {
let dist = distance as int;
while self.buffer_length() < dist {
self.buffer[self.buffer_end as uint] = real_token(self.reader);
self.buffer[self.buffer_end as uint] = real_token(&mut *self.reader);
self.buffer_end = (self.buffer_end + 1) & 3;
}
f(&self.buffer[((self.buffer_start + dist - 1) & 3) as uint].tok)
Expand Down
22 changes: 22 additions & 0 deletions src/test/compile-fail/cross-borrow-trait.rs
@@ -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>`
}

8 changes: 4 additions & 4 deletions src/test/compile-fail/regions-close-object-into-object.rs
Expand Up @@ -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() {}
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/cleanup-auto-borrow-obj.rs
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/issue-3794.rs
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion src/test/run-pass/new-box.rs
Expand Up @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/run-pass/regions-early-bound-trait-param.rs
Expand Up @@ -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
Expand Down

0 comments on commit f7ec95e

Please sign in to comment.