Skip to content

Implicit trait object coercion fixes. #11525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,12 @@ impl mem_categorization_ctxt {
match **adjustment {
ty::AutoObject(..) => {
// Implicity casts a concrete object to trait object
// Result is an rvalue
// so just patch up the type
let expr_ty = ty::expr_ty_adjusted(self.tcx, expr);
self.cat_rvalue_node(expr, expr_ty)
@cmt_ {
ty: expr_ty,
..*self.cat_expr_unadjusted(expr)
}
}

ty::AutoAddEnv(..) => {
Expand Down
15 changes: 4 additions & 11 deletions src/librustc/middle/trans/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,12 @@ pub fn trans_to_datum<'a>(bcx: &'a Block<'a>, expr: &ast::Expr)
}
};
}
AutoObject(ref sigil, ref region, _, _, _, _) => {
AutoObject(..) => {

let adjusted_ty = ty::expr_ty_adjusted(bcx.tcx(), expr);
let scratch = scratch_datum(bcx, adjusted_ty, "__adjust", false);

let trait_store = match *sigil {
ast::BorrowedSigil => ty::RegionTraitStore(region.expect("expected valid region")),
ast::OwnedSigil => ty::UniqTraitStore,
ast::ManagedSigil => ty::BoxTraitStore
};

bcx = meth::trans_trait_cast(bcx, expr, expr.id, SaveIn(scratch.val),
trait_store, false /* no adjustments */);
bcx = meth::trans_trait_cast(bcx, expr, expr.id, SaveIn(scratch.val), Some(datum));

datum = scratch.to_appropriate_datum(bcx);
datum.add_clean(bcx);
Expand Down Expand Up @@ -834,9 +827,9 @@ fn trans_rvalue_dps_unadjusted<'a>(
}
ast::ExprCast(val, _) => {
match ty::get(node_id_type(bcx, expr.id)).sty {
ty::ty_trait(_, _, store, _, _) => {
ty::ty_trait(..) => {
return meth::trans_trait_cast(bcx, val, expr.id,
dest, store, true /* adjustments */);
dest, None);
}
_ => {
bcx.tcx().sess.span_bug(expr.span,
Expand Down
19 changes: 8 additions & 11 deletions src/librustc/middle/trans/meth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,22 +637,14 @@ pub fn trans_trait_cast<'a>(
val: &ast::Expr,
id: ast::NodeId,
dest: expr::Dest,
_store: ty::TraitStore,
do_adjustments: bool)
obj: Option<Datum>)
-> &'a Block<'a> {
let mut bcx = bcx;
let _icx = push_ctxt("impl::trans_cast");

// Pick the right trans function
let trans_into = if do_adjustments {
expr::trans_into
} else {
expr::trans_into_unadjusted
};

let lldest = match dest {
Ignore => {
return trans_into(bcx, val, Ignore);
return expr::trans_into(bcx, val, Ignore);
}
SaveIn(dest) => dest
};
Expand All @@ -667,7 +659,12 @@ pub fn trans_trait_cast<'a>(
llboxdest = PointerCast(bcx,
llboxdest,
type_of(bcx.ccx(), v_ty).ptr_to());
bcx = trans_into(bcx, val, SaveIn(llboxdest));
bcx = match obj {
Some(datum) => {
datum.store_to_dest(bcx, SaveIn(llboxdest))
}
None => expr::trans_into(bcx, val, SaveIn(llboxdest))
};

// Store the vtable into the pair or triple.
// This is structured a bit funny because of dynamic borrow failures.
Expand Down
37 changes: 37 additions & 0 deletions src/test/compile-fail/use-after-move-implicity-coerced-object.rs
Original file line number Diff line number Diff line change
@@ -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 <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.

struct Number {
n: i64
}

impl ToStr for Number {
fn to_str(&self) -> ~str {
self.n.to_str()
}
}

struct List {
list: ~[~ToStr]
}

impl List {
fn push(&mut self, n: ~ToStr) {
self.list.push(n);
}
}

fn main() {
let n = ~Number { n: 42 };
let mut l = ~List { list: ~[] };
l.push(n);
//^~ NOTE: `n` moved here because it has type `~Number`, which is non-copyable (perhaps you meant to use clone()?)
let x = n.to_str(); //~ ERROR: use of moved value: `n`
}
9 changes: 9 additions & 0 deletions src/test/run-pass/trait-coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#[feature(managed_boxes)];

use std::io;

trait Trait {
fn f(&self);
}
Expand All @@ -29,6 +31,10 @@ fn f(x: @Trait) {
x.f();
}

fn foo(mut a: ~Writer) {
a.write(bytes!("Hello\n"));
}

pub fn main() {
let a = Struct { x: 1, y: 2 };
let b: @Trait = @a;
Expand All @@ -38,5 +44,8 @@ pub fn main() {
let d: &Trait = &a;
d.f();
f(@a);

let out = io::stdout();
foo(~out);
}