Skip to content

Generalize moves to allow moves out of any owned content #4384

Closed
@nikomatsakis

Description

@nikomatsakis
Contributor

This program should be legal

struct Foo {
    v: ~[uint]
}

fn make_foo() -> Foo {
    Foo { v: ~[1, 2, 3] }
}

fn main() {
    let v = make_foo().v;
    for v.each |i| { io::println(fmt!("%u", *i)); }
}

But it yields:

/Users/nmatsakis/tmp/bar.rs:10:12: 10:24 error: moving out of immutable field
/Users/nmatsakis/tmp/bar.rs:10     let v = make_foo().v;
                                           ^~~~~~~~~~~~

Activity

nikomatsakis

nikomatsakis commented on Jan 8, 2013

@nikomatsakis
ContributorAuthor

Presumably the problem here is how borrow checker treats moves out of fields contained in rvalues. But that code needs to be somewhat re-worked in general.

nikomatsakis

nikomatsakis commented on Jan 8, 2013

@nikomatsakis
ContributorAuthor

Oh, actually, this is not specific to rvalues really. It's just a generally unnecessary limitation of borrowck.

bblum

bblum commented on Jan 11, 2013

@bblum
Contributor

I'm confused why this isn't specific to rvalues. Isn't let f = make_foo(); let v = foo.v; illegal?

This seems to me like a legal special case because it could be syntax sugar for let Foo { v: v } = make_foo().

nikomatsakis

nikomatsakis commented on Feb 15, 2013

@nikomatsakis
ContributorAuthor

I am editing the title of this issue and expanding it. We should allow moves out of arbitrary owned content. Most of the system is setup for this already, I think the only problem is that borrow check flags such moves as illegal. (But there might be trans work needed to) Examples of things that ought to work but do not:

let a = b.c; // where b.c is an owned path and c has linear type
let d = e[f]; // where e is ~[T] and T is linear

In some cases these can be expressed today using match statements. I see no reason for match statements to be more expressive than other kinds of expressions; moreover, they do not allow e[f].

Currently, the moves code and liveness analysis will allow such moves. They would cause the local variables b and e above to be considered "partially moved" and thus unusable. But then the borrow checker would flag an error for moving out of something that's not a local variable.

nikomatsakis

nikomatsakis commented on Feb 15, 2013

@nikomatsakis
ContributorAuthor

@bblum your example is illegal today but I don't see any good reason for that restriction.

graydon

graydon commented on Apr 30, 2013

@graydon
Contributor

assigning bug; change assignment if you disagree

nikomatsakis

nikomatsakis commented on May 6, 2013

@nikomatsakis
ContributorAuthor

Some FIXMEs related to bits of code that will have to be adjusted.

7 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @graydon@Blei@nikomatsakis@bblum

      Issue actions

        Generalize moves to allow moves out of any owned content · Issue #4384 · rust-lang/rust