Skip to content

Commit e268c7f

Browse files
committed
auto merge of #9350 : pnkfelix/rust/fsk-issue-4691-catch-bad-fsu-during-compute-moves, r=nikomatsakis
Resolves third bullet of #4691: if the functional-struct-update (FSU) expression `{ a: b, ..s }` causes `s` to move and `s` has a destructor, then the expression is illegal. r? @nikomatsakis
2 parents 58424eb + 4e543f7 commit e268c7f

File tree

4 files changed

+147
-2
lines changed

4 files changed

+147
-2
lines changed

src/librustc/middle/moves.rs

+15
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ use middle::typeck::{method_map};
134134
use util::ppaux;
135135
use util::ppaux::Repr;
136136
use util::common::indenter;
137+
use util::ppaux::UserString;
137138

138139
use std::at_vec;
139140
use std::hashmap::{HashSet, HashMap};
@@ -433,7 +434,21 @@ impl VisitContext {
433434
ty::type_moves_by_default(self.tcx, tf.mt.ty)
434435
});
435436

437+
fn has_dtor(tcx: ty::ctxt, ty: ty::t) -> bool {
438+
use middle::ty::{get,ty_struct,ty_enum};
439+
match get(ty).sty {
440+
ty_struct(did, _) | ty_enum(did, _) => ty::has_dtor(tcx, did),
441+
_ => false,
442+
}
443+
}
444+
436445
if consume_with {
446+
if has_dtor(self.tcx, with_ty) {
447+
self.tcx.sess.span_err(with_expr.span,
448+
fmt!("cannot move out of type `%s`, \
449+
which defines the `Drop` trait",
450+
with_ty.user_string(self.tcx)));
451+
}
437452
self.consume_expr(*with_expr, visitor);
438453
} else {
439454
self.use_expr(*with_expr, Read, visitor);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2012-2013 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+
// Issue 4691: Ensure that functional-struct-update can only copy, not
12+
// move, when the struct implements Drop.
13+
14+
use NC = std::util::NonCopyable;
15+
struct S { a: int, nc: NC }
16+
impl Drop for S { fn drop(&mut self) { } }
17+
18+
struct T { a: int, mv: ~int }
19+
impl Drop for T { fn drop(&mut self) { } }
20+
21+
fn f(s0:S) {
22+
let _s2 = S{a: 2, ..s0}; //~error: cannot move out of type `S`, which defines the `Drop` trait
23+
}
24+
25+
fn g(s0:T) {
26+
let _s2 = T{a: 2, ..s0}; //~error: cannot move out of type `T`, which defines the `Drop` trait
27+
}
28+
29+
fn main() { }

src/test/compile-fail/functional-struct-update-noncopyable.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ impl Drop for A {
2121
}
2222
fn main() {
2323
let a = A { y: Arc::new(1), x: Arc::new(2) };
24-
let _b = A { y: Arc::new(3), ..a };
25-
let _c = a; //~ ERROR use of moved value
24+
let _b = A { y: Arc::new(3), ..a }; //~ ERROR cannot move out of type `A`
25+
let _c = a;
2626
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2012-2013 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+
// Issue 4691: Ensure that functional-struct-updates operates
12+
// correctly and moves rather than copy when appropriate.
13+
14+
use NC = std::util::NonCopyable;
15+
16+
struct ncint { nc: NC, v: int }
17+
fn ncint(v: int) -> ncint { ncint { nc: NC, v: v } }
18+
19+
struct NoFoo { copied: int, noncopy: ncint, }
20+
impl NoFoo {
21+
fn new(x:int,y:int) -> NoFoo { NoFoo { copied: x, noncopy: ncint(y) } }
22+
}
23+
24+
struct MoveFoo { copied: int, moved: ~int, }
25+
impl MoveFoo {
26+
fn new(x:int,y:int) -> MoveFoo { MoveFoo { copied: x, moved: ~y } }
27+
}
28+
29+
struct DropNoFoo { inner: NoFoo }
30+
impl DropNoFoo {
31+
fn new(x:int,y:int) -> DropNoFoo { DropNoFoo { inner: NoFoo::new(x,y) } }
32+
}
33+
impl Drop for DropNoFoo { fn drop(&mut self) { } }
34+
35+
struct DropMoveFoo { inner: MoveFoo }
36+
impl DropMoveFoo {
37+
fn new(x:int,y:int) -> DropMoveFoo { DropMoveFoo { inner: MoveFoo::new(x,y) } }
38+
}
39+
impl Drop for DropMoveFoo { fn drop(&mut self) { } }
40+
41+
42+
fn test0() {
43+
// just copy implicitly copyable fields from `f`, no moves
44+
// (and thus it is okay that these are Drop; compare against
45+
// compile-fail test: borrowck-struct-update-with-dtor.rs).
46+
47+
// Case 1: NonCopyable
48+
let f = DropNoFoo::new(1, 2);
49+
let b = DropNoFoo { inner: NoFoo { noncopy: ncint(3), ..f.inner }};
50+
let c = DropNoFoo { inner: NoFoo { noncopy: ncint(4), ..f.inner }};
51+
assert_eq!(f.inner.copied, 1);
52+
assert_eq!(f.inner.noncopy.v, 2);
53+
54+
assert_eq!(b.inner.copied, 1);
55+
assert_eq!(b.inner.noncopy.v, 3);
56+
57+
assert_eq!(c.inner.copied, 1);
58+
assert_eq!(c.inner.noncopy.v, 4);
59+
60+
// Case 2: Owned
61+
let f = DropMoveFoo::new(5, 6);
62+
let b = DropMoveFoo { inner: MoveFoo { moved: ~7, ..f.inner }};
63+
let c = DropMoveFoo { inner: MoveFoo { moved: ~8, ..f.inner }};
64+
assert_eq!(f.inner.copied, 5);
65+
assert_eq!(*f.inner.moved, 6);
66+
67+
assert_eq!(b.inner.copied, 5);
68+
assert_eq!(*b.inner.moved, 7);
69+
70+
assert_eq!(c.inner.copied, 5);
71+
assert_eq!(*c.inner.moved, 8);
72+
}
73+
74+
fn test1() {
75+
// copying move-by-default fields from `f`, so it moves:
76+
let f = MoveFoo::new(11, 12);
77+
78+
let b = MoveFoo {moved: ~13, ..f};
79+
let c = MoveFoo {copied: 14, ..f};
80+
assert_eq!(b.copied, 11);
81+
assert_eq!(*b.moved, 13);
82+
assert_eq!(c.copied, 14);
83+
assert_eq!(*c.moved, 12);
84+
}
85+
86+
fn test2() {
87+
// move non-copyable field
88+
let f = NoFoo::new(21, 22);
89+
let b = NoFoo {noncopy: ncint(23), ..f};
90+
let c = NoFoo {copied: 24, ..f};
91+
assert_eq!(b.copied, 21);
92+
assert_eq!(b.noncopy.v, 23);
93+
assert_eq!(c.copied, 24);
94+
assert_eq!(c.noncopy.v, 22);
95+
}
96+
97+
fn main() {
98+
test0();
99+
test1();
100+
test2();
101+
}

0 commit comments

Comments
 (0)