mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 19:58:32 +00:00
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.
This commit is contained in:
parent
459353e107
commit
a96b16e8c3
@ -183,6 +183,12 @@ fn need_shared_or_pinned_ctor(tcx: ty::ctxt, a: @ast::expr, descr: str) {
|
|||||||
ast::expr_rec(_, _) {
|
ast::expr_rec(_, _) {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
ast::expr_unary(ast::uniq(_), _) {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
ast::expr_tup(_) {
|
||||||
|
true
|
||||||
|
}
|
||||||
_ { false }
|
_ { false }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -266,14 +272,12 @@ fn check_stmt(tcx: ty::ctxt, stmt: @ast::stmt) {
|
|||||||
for (let_style, local) in locals {
|
for (let_style, local) in locals {
|
||||||
alt local.node.init {
|
alt local.node.init {
|
||||||
option::some({op: ast::init_assign., expr}) {
|
option::some({op: ast::init_assign., expr}) {
|
||||||
need_expr_kind(tcx, expr,
|
need_shared_or_pinned_ctor(tcx, expr,
|
||||||
ast::kind_shared,
|
"local initializer");
|
||||||
"local initializer");
|
|
||||||
}
|
}
|
||||||
option::some({op: ast::init_move., expr}) {
|
option::some({op: ast::init_move., expr}) {
|
||||||
// FIXME: Should be as above but moving may be the
|
need_shared_or_pinned_ctor(tcx, expr,
|
||||||
// only way available currently to assign a resource
|
"local initializer");
|
||||||
// to a local.
|
|
||||||
}
|
}
|
||||||
option::none. { /* fall through */ }
|
option::none. { /* fall through */ }
|
||||||
}
|
}
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
// error-pattern: mismatched kind
|
|
||||||
|
|
||||||
resource r(b: bool) {
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
// Kind analysis considers this a copy, which isn't strictly true,
|
|
||||||
// but for many assignment initializers could be. To actually
|
|
||||||
// assign a resource to a local we can still use a move
|
|
||||||
// initializer.
|
|
||||||
let i = r(true);
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
// error-pattern: mismatched kind
|
|
||||||
|
|
||||||
resource r(b: bool) {
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let i = ~r(true);
|
|
||||||
}
|
|
7
src/test/run-pass/resource-assign-is-not-copy.rs
Normal file
7
src/test/run-pass/resource-assign-is-not-copy.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
resource r(i: int) {
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// Even though this looks like a copy, it's guaranteed not to be
|
||||||
|
let a = r(0);
|
||||||
|
}
|
11
src/test/run-pass/unique-pinned-nocopy-2.rs
Normal file
11
src/test/run-pass/unique-pinned-nocopy-2.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
resource r(i: @mutable int) {
|
||||||
|
*i = *i + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let i = @mutable 0;
|
||||||
|
{
|
||||||
|
let j = ~r(i);
|
||||||
|
}
|
||||||
|
assert *i == 1;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user