Skip to content

Commit 6b42ad5

Browse files
committed
Enforce copy restrictions on let initializers
1 parent 07e13fe commit 6b42ad5

18 files changed

+67
-18
lines changed

src/comp/middle/kind.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,29 @@ fn check_expr(tcx: ty::ctxt, e: @ast::expr) {
212212
}
213213
}
214214

215+
fn check_stmt(tcx: ty::ctxt, stmt: @ast::stmt) {
216+
alt stmt.node {
217+
ast::stmt_decl(@{node: ast::decl_local(locals), _}, _) {
218+
for (let_style, local) in locals {
219+
alt local.node.init {
220+
option::some({op: ast::init_assign., expr}) {
221+
need_expr_kind(tcx, expr,
222+
ast::kind_shared,
223+
"local initializer");
224+
check_copy(tcx, expr);
225+
}
226+
_ { /* fall through */ }
227+
}
228+
}
229+
}
230+
_ { /* fall through */ }
231+
}
232+
}
233+
215234
fn check_crate(tcx: ty::ctxt, crate: @ast::crate) {
216235
let visit =
217-
visit::mk_simple_visitor(@{visit_expr: bind check_expr(tcx, _)
236+
visit::mk_simple_visitor(@{visit_expr: bind check_expr(tcx, _),
237+
visit_stmt: bind check_stmt(tcx, _)
218238
with *visit::default_simple_visitor()});
219239
visit::visit_crate(*crate, (), visit);
220240
tcx.sess.abort_if_errors();

src/lib/task.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn spawn_inner(-thunk: fn(), notify: option<comm::chan<task_notification>>) ->
114114
let raw_thunk: {code: u32, env: u32} = cast(thunk);
115115

116116
// set up the task pointer
117-
let task_ptr = rust_task_ptr(rustrt::get_task_pointer(id));
117+
let task_ptr <- rust_task_ptr(rustrt::get_task_pointer(id));
118118

119119
assert (ptr::null() != (**task_ptr).stack_ptr);
120120

src/test/compile-fail/resource-let.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// error-pattern: mismatched kind
2+
3+
resource r(b: bool) {
4+
}
5+
6+
fn main() {
7+
// Kind analysis considers this a copy, which isn't strictly true,
8+
// but for many assignment initializers could be. To actually
9+
// assign a resource to a local we can still use a move
10+
// initializer.
11+
let i = r(true);
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// error-pattern: mismatched kind
2+
3+
resource r(b: bool) {
4+
}
5+
6+
fn main() {
7+
let i <- r(true);
8+
let j = i;
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// error-pattern: mismatched kind
2+
3+
resource r(b: bool) {
4+
}
5+
6+
fn main() {
7+
let i = ~r(true);
8+
}

src/test/run-pass/expr-alt-generic-box2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// -*- rust -*-
55
type compare<T> = fn(T, T) -> bool;
66

7-
fn test_generic<T>(expected: T, eq: compare<T>) {
7+
fn test_generic<@T>(expected: T, eq: compare<T>) {
88
let actual: T = alt true { true { expected } };
99
assert (eq(expected, actual));
1010
}

src/test/run-pass/expr-alt-generic-unique1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// -*- rust -*-
44
type compare<T> = fn(~T, ~T) -> bool;
55

6-
fn test_generic<T>(expected: ~T, eq: compare<T>) {
6+
fn test_generic<@T>(expected: ~T, eq: compare<T>) {
77
let actual: ~T = alt true { true { expected } };
88
assert (eq(expected, actual));
99
}

src/test/run-pass/expr-alt-generic-unique2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// -*- rust -*-
55
type compare<T> = fn(T, T) -> bool;
66

7-
fn test_generic<T>(expected: T, eq: compare<T>) {
7+
fn test_generic<@T>(expected: T, eq: compare<T>) {
88
let actual: T = alt true { true { expected } };
99
assert (eq(expected, actual));
1010
}

src/test/run-pass/expr-alt-generic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// -*- rust -*-
55
type compare<T> = fn(T, T) -> bool;
66

7-
fn test_generic<T>(expected: T, eq: compare<T>) {
7+
fn test_generic<@T>(expected: T, eq: compare<T>) {
88
let actual: T = alt true { true { expected } };
99
assert (eq(expected, actual));
1010
}

src/test/run-pass/expr-block-generic-box2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// -*- rust -*-
55
type compare<T> = fn(T, T) -> bool;
66

7-
fn test_generic<T>(expected: T, eq: compare<T>) {
7+
fn test_generic<@T>(expected: T, eq: compare<T>) {
88
let actual: T = { expected };
99
assert (eq(expected, actual));
1010
}

src/test/run-pass/expr-block-generic-unique1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// -*- rust -*-
44
type compare<T> = fn(~T, ~T) -> bool;
55

6-
fn test_generic<T>(expected: ~T, eq: compare<T>) {
6+
fn test_generic<@T>(expected: ~T, eq: compare<T>) {
77
let actual: ~T = { expected };
88
assert (eq(expected, actual));
99
}

src/test/run-pass/expr-block-generic-unique2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// -*- rust -*-
55
type compare<T> = fn(T, T) -> bool;
66

7-
fn test_generic<T>(expected: T, eq: compare<T>) {
7+
fn test_generic<@T>(expected: T, eq: compare<T>) {
88
let actual: T = { expected };
99
assert (eq(expected, actual));
1010
}

src/test/run-pass/expr-block-generic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Tests for standalone blocks as expressions with dynamic type sizes
77
type compare<T> = fn(T, T) -> bool;
88

9-
fn test_generic<T>(expected: T, eq: compare<T>) {
9+
fn test_generic<@T>(expected: T, eq: compare<T>) {
1010
let actual: T = { expected };
1111
assert (eq(expected, actual));
1212
}

src/test/run-pass/expr-if-generic-box2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// -*- rust -*-
55
type compare<T> = fn(T, T) -> bool;
66

7-
fn test_generic<T>(expected: T, not_expected: T, eq: compare<T>) {
7+
fn test_generic<@T>(expected: T, not_expected: T, eq: compare<T>) {
88
let actual: T = if true { expected } else { not_expected };
99
assert (eq(expected, actual));
1010
}

src/test/run-pass/expr-if-generic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Tests for if as expressions with dynamic type sizes
77
type compare<T> = fn(T, T) -> bool;
88

9-
fn test_generic<T>(expected: T, not_expected: T, eq: compare<T>) {
9+
fn test_generic<@T>(expected: T, not_expected: T, eq: compare<T>) {
1010
let actual: T = if true { expected } else { not_expected };
1111
assert (eq(expected, actual));
1212
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

22

3-
fn f<T>(t: T) { let t1: T = t; }
3+
fn f<@T>(t: T) { let t1: T = t; }
44

55
fn main() { let x = {x: @10, y: @12}; f(x); }

src/test/run-pass/type-params-in-for-each.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ iter range(lo: uint, hi: uint) -> uint {
55
while lo_ < hi { put lo_; lo_ += 1u; }
66
}
77

8-
fn create_index<T>(index: [{a: T, b: uint}], hash_fn: fn(T) -> uint) {
8+
fn create_index<@T>(index: [{a: T, b: uint}], hash_fn: fn(T) -> uint) {
99
for each i: uint in range(0u, 256u) { let bucket: [T] = []; }
1010
}
1111

src/test/run-pass/unique-swap2.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ fn test1() {
77
let i = @mutable 100;
88
let j = @mutable 200;
99
{
10-
let x = ~r(i);
11-
let y = ~r(j);
10+
let x <- ~r(i);
11+
let y <- ~r(j);
1212
x <-> y;
1313
assert ***x == 200;
1414
assert ***y == 100;
@@ -20,8 +20,8 @@ fn test1() {
2020
fn test2() {
2121
let i = @mutable 0;
2222
{
23-
let x = ~r(i);
24-
let y = ~r(i);
23+
let x <- ~r(i);
24+
let y <- ~r(i);
2525
x <-> y;
2626
}
2727
assert *i == 2;

0 commit comments

Comments
 (0)