Skip to content

Commit 277b4f0

Browse files
committed
Fix soundness hole when unsizing boxes.
1 parent 5f5ed62 commit 277b4f0

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed

src/librustc/middle/expr_use_visitor.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -857,36 +857,30 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
857857
n: uint) {
858858
debug!("walk_autoref expr={}", expr.repr(self.tcx()));
859859

860-
// Match for unique trait coercions first, since we don't need the
861-
// call to cat_expr_autoderefd.
862-
match *autoref {
863-
ty::AutoUnsizeUniq(ty::UnsizeVtable(..)) |
864-
ty::AutoUnsize(ty::UnsizeVtable(..)) => {
865-
assert!(n == 1, format!("Expected exactly 1 deref with Uniq \
866-
AutoRefs, found: {}", n));
867-
let cmt_unadjusted =
868-
return_if_err!(self.mc.cat_expr_unadjusted(expr));
869-
self.delegate_consume(expr.id, expr.span, cmt_unadjusted);
870-
return;
871-
}
872-
_ => {}
873-
}
874-
875-
let cmt_derefd = return_if_err!(
876-
self.mc.cat_expr_autoderefd(expr, n));
877-
debug!("walk_adjustment: cmt_derefd={}",
878-
cmt_derefd.repr(self.tcx()));
879-
880860
match *autoref {
881861
ty::AutoPtr(r, m, _) => {
862+
let cmt_derefd = return_if_err!(
863+
self.mc.cat_expr_autoderefd(expr, n));
864+
debug!("walk_adjustment: cmt_derefd={}",
865+
cmt_derefd.repr(self.tcx()));
866+
882867
self.delegate.borrow(expr.id,
883868
expr.span,
884869
cmt_derefd,
885870
r,
886871
ty::BorrowKind::from_mutbl(m),
887872
AutoRef);
888873
}
889-
ty::AutoUnsizeUniq(_) | ty::AutoUnsize(_) | ty::AutoUnsafe(..) => {}
874+
ty::AutoUnsize(_) |
875+
ty::AutoUnsizeUniq(_) => {
876+
assert!(n == 1, format!("Expected exactly 1 deref with Uniq \
877+
AutoRefs, found: {}", n));
878+
let cmt_unadjusted =
879+
return_if_err!(self.mc.cat_expr_unadjusted(expr));
880+
self.delegate_consume(expr.id, expr.span, cmt_unadjusted);
881+
}
882+
ty::AutoUnsafe(..) => {
883+
}
890884
}
891885
}
892886

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2015 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+
// Check that we report an error if an upcast box is moved twice.
12+
13+
fn consume(_: Box<[i32]>) {
14+
}
15+
16+
fn foo(b: Box<[i32;5]>) {
17+
consume(b);
18+
consume(b); //~ ERROR use of moved value
19+
}
20+
21+
fn main() {
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2015 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+
// Check that we report an error if an upcast box is moved twice.
12+
13+
trait Foo { fn dummy(&self); }
14+
15+
fn consume(_: Box<Foo>) {
16+
}
17+
18+
fn foo(b: Box<Foo+Send>) {
19+
consume(b);
20+
consume(b); //~ ERROR use of moved value
21+
}
22+
23+
fn main() {
24+
}

0 commit comments

Comments
 (0)