Skip to content

Function return does not always perform a move #995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
brson opened this issue Sep 29, 2011 · 4 comments
Closed

Function return does not always perform a move #995

brson opened this issue Sep 29, 2011 · 4 comments

Comments

@brson
Copy link
Contributor

brson commented Sep 29, 2011

This is a problem for unique pointers

use std;
import std::unsafe;

fn f(addr: @mutable int) -> ~int {
    let i = ~100;
    let a: int = unsafe::reinterpret_cast(i);
    log a;
    *addr = a;
    // Since i is not a temporary it gets copied, which is not what you expect
    ret i;
}

fn main() {
    let addr = @mutable 0;
    let i <- f(addr);
    let expected = *addr;
    let actual: int = unsafe::reinterpret_cast(i);
    log expected;
    log actual;
    assert expected == actual;
}
@marijnh
Copy link
Contributor

marijnh commented Sep 30, 2011

It should not always perform a move. Consider returning foo.x, moving out of a data structure is invalid. Whether it moves should definitely depend on the type. For uniques, returning something that can't be moved is (if I understand correctly) an error. For other types, it should continue to implicitly copy.

@brson
Copy link
Contributor Author

brson commented Sep 30, 2011

Uniques don't have to be moved. They are allowed to be copied; they just don't like it. What approach can we take to make returning more likely to be a move? Is it true that it's always safe to do a move when returning a local?

@marijnh
Copy link
Contributor

marijnh commented Sep 30, 2011

The optimization proposed in https://github.com/graydon/rust/issues/925 would help here.

@marijnh
Copy link
Contributor

marijnh commented Nov 22, 2011

This has been fixed by #1177

@marijnh marijnh closed this as completed Nov 22, 2011
coastalwhite pushed a commit to coastalwhite/rust that referenced this issue Aug 5, 2023
celinval pushed a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants