Skip to content

Commit 17f984c

Browse files
committed
librustc: Don't allow use after move of implicitly coerced object. Fixes #11481.
1 parent ab66f76 commit 17f984c

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/librustc/middle/mem_categorization.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,12 @@ impl mem_categorization_ctxt {
345345
match **adjustment {
346346
ty::AutoObject(..) => {
347347
// Implicity casts a concrete object to trait object
348-
// Result is an rvalue
348+
// so just patch up the type
349349
let expr_ty = ty::expr_ty_adjusted(self.tcx, expr);
350-
self.cat_rvalue_node(expr, expr_ty)
350+
@cmt_ {
351+
ty: expr_ty,
352+
..*self.cat_expr_unadjusted(expr)
353+
}
351354
}
352355

353356
ty::AutoAddEnv(..) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2014 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+
struct Number {
12+
n: i64
13+
}
14+
15+
impl ToStr for Number {
16+
fn to_str(&self) -> ~str {
17+
self.n.to_str()
18+
}
19+
}
20+
21+
struct List {
22+
list: ~[~ToStr]
23+
}
24+
25+
impl List {
26+
fn push(&mut self, n: ~ToStr) {
27+
self.list.push(n);
28+
}
29+
}
30+
31+
fn main() {
32+
let n = ~Number { n: 42 };
33+
let mut l = ~List { list: ~[] };
34+
l.push(n);
35+
//^~ NOTE: `n` moved here because it has type `~Number`, which is non-copyable (perhaps you meant to use clone()?)
36+
let x = n.to_str(); //~ ERROR: use of moved value: `n`
37+
}

0 commit comments

Comments
 (0)