Skip to content

Function return does not always perform a move #995

Closed

Description

@brson

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

  1. 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.

  2. 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?

  3. marijnh commented on Sep 30, 2011

    @marijnh
    Contributor
  4. marijnh commented on Nov 22, 2011

    @marijnh
    Contributor

    This has been fixed by #1177

  5. added a commit that references this issue on Aug 5, 2023
  6. added 2 commits that reference this issue on Aug 21, 2026
  7. added a commit that references this issue on Oct 12, 2026
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

      Issue actions