Skip to content

Commit a96b16e

Browse files
committed
Make it again possible to initialize resource locals via assignment
Some special cases allow both 'let a <- my_resource(x)' and 'let a = my_resource(x)' to work as expected despite ostensibly being copies and moves.
1 parent 459353e commit a96b16e

File tree

5 files changed

+28
-26
lines changed

5 files changed

+28
-26
lines changed

src/comp/middle/kind.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ fn need_shared_or_pinned_ctor(tcx: ty::ctxt, a: @ast::expr, descr: str) {
183183
ast::expr_rec(_, _) {
184184
true
185185
}
186+
ast::expr_unary(ast::uniq(_), _) {
187+
true
188+
}
189+
ast::expr_tup(_) {
190+
true
191+
}
186192
_ { false }
187193
}
188194
}
@@ -266,14 +272,12 @@ fn check_stmt(tcx: ty::ctxt, stmt: @ast::stmt) {
266272
for (let_style, local) in locals {
267273
alt local.node.init {
268274
option::some({op: ast::init_assign., expr}) {
269-
need_expr_kind(tcx, expr,
270-
ast::kind_shared,
271-
"local initializer");
275+
need_shared_or_pinned_ctor(tcx, expr,
276+
"local initializer");
272277
}
273278
option::some({op: ast::init_move., expr}) {
274-
// FIXME: Should be as above but moving may be the
275-
// only way available currently to assign a resource
276-
// to a local.
279+
need_shared_or_pinned_ctor(tcx, expr,
280+
"local initializer");
277281
}
278282
option::none. { /* fall through */ }
279283
}

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

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/test/compile-fail/unique-pinned-nocopy-2.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource r(i: int) {
2+
}
3+
4+
fn main() {
5+
// Even though this looks like a copy, it's guaranteed not to be
6+
let a = r(0);
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
resource r(i: @mutable int) {
2+
*i = *i + 1;
3+
}
4+
5+
fn main() {
6+
let i = @mutable 0;
7+
{
8+
let j = ~r(i);
9+
}
10+
assert *i == 1;
11+
}

0 commit comments

Comments
 (0)