From 1ed94a56744c0ed4056c22da297d84222f4327f2 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 17 Aug 2012 16:12:07 -0700 Subject: [PATCH] rustc: Prevent destructors from being run twice with the repeated vector syntax --- src/rustc/middle/kind.rs | 9 +++++++++ .../compile-fail/repeat-to-run-dtor-twice.rs | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/test/compile-fail/repeat-to-run-dtor-twice.rs diff --git a/src/rustc/middle/kind.rs b/src/rustc/middle/kind.rs index c41758f7052..c055c460e09 100644 --- a/src/rustc/middle/kind.rs +++ b/src/rustc/middle/kind.rs @@ -307,6 +307,15 @@ fn check_expr(e: @expr, cx: ctx, v: visit::vt) { } } } + expr_repeat(element, count_expr, _) => { + let count = ty::eval_repeat_count(cx.tcx, count_expr, e.span); + if count == 1 { + maybe_copy(cx, element); + } else { + let element_ty = ty::expr_ty(cx.tcx, element); + check_copy(cx, element.id, element_ty, element.span, true); + } + } _ => { } } visit::visit_expr(e, cx, v); diff --git a/src/test/compile-fail/repeat-to-run-dtor-twice.rs b/src/test/compile-fail/repeat-to-run-dtor-twice.rs new file mode 100644 index 00000000000..a96293998d2 --- /dev/null +++ b/src/test/compile-fail/repeat-to-run-dtor-twice.rs @@ -0,0 +1,16 @@ +// Tests that one can't run a destructor twice with the repeated vector +// literal syntax. + +struct Foo { + x: int; + + drop { + io::println("Goodbye!"); + } +} + +fn main() { + let a = Foo { x: 3 }; + let b = [ a, ..5 ]; //~ ERROR copying a noncopyable value +} +