Skip to content

Function return does not always perform a move #995

@brson

Description

@brson
Contributor

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;
}

Activity

marijnh

marijnh commented on Sep 30, 2011

@marijnh
Contributor

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

brson commented on Sep 30, 2011

@brson
ContributorAuthor

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

marijnh commented on Sep 30, 2011

@marijnh
Contributor

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

marijnh

marijnh commented on Nov 22, 2011

@marijnh
Contributor

This has been fixed by #1177

added a commit that references this issue on Aug 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @marijnh@brson

        Issue actions

          Function return does not always perform a move · Issue #995 · rust-lang/rust