Skip to content

Commit c321cdb

Browse files
committed
Disallow deconstructing destructing structs (fixes #3147)
1 parent 849d564 commit c321cdb

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/rustc/middle/typeck/check/alt.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ fn check_pat(pcx: pat_ctxt, pat: @ast::pat, expected: ty::t) {
270270
}
271271
}
272272

273+
// Forbid pattern-matching structs with destructors.
274+
if ty::has_dtor(tcx, class_id) {
275+
tcx.sess.span_err(pat.span, ~"deconstructing struct not allowed \
276+
in pattern (it has a destructor)");
277+
}
278+
273279
// Index the class fields.
274280
let field_map = std::map::box_str_hash();
275281
for class_fields.eachi |i, class_field| {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
struct X {
2+
x: ~str;
3+
drop {
4+
error!("value: %s", self.x);
5+
}
6+
}
7+
8+
fn unwrap(+x: X) -> ~str {
9+
let X { x: y } = x; //~ ERROR deconstructing struct not allowed in pattern
10+
y
11+
}
12+
13+
fn main() {
14+
let x = X { x: ~"hello" };
15+
let y = unwrap(x);
16+
error!("contents: %s", y);
17+
}

0 commit comments

Comments
 (0)