Skip to content

Commit

Permalink
Disallow deconstructing destructing structs (fixes #3147)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblum committed Aug 21, 2012
1 parent 849d564 commit c321cdb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/rustc/middle/typeck/check/alt.rs
Expand Up @@ -270,6 +270,12 @@ fn check_pat(pcx: pat_ctxt, pat: @ast::pat, expected: ty::t) {
}
}

// Forbid pattern-matching structs with destructors.
if ty::has_dtor(tcx, class_id) {
tcx.sess.span_err(pat.span, ~"deconstructing struct not allowed \
in pattern (it has a destructor)");
}

// Index the class fields.
let field_map = std::map::box_str_hash();
for class_fields.eachi |i, class_field| {
Expand Down
@@ -0,0 +1,17 @@
struct X {
x: ~str;
drop {
error!("value: %s", self.x);
}
}

fn unwrap(+x: X) -> ~str {
let X { x: y } = x; //~ ERROR deconstructing struct not allowed in pattern
y
}

fn main() {
let x = X { x: ~"hello" };
let y = unwrap(x);
error!("contents: %s", y);
}

0 comments on commit c321cdb

Please sign in to comment.