Skip to content

by-val and by-move can be dangerous when combined #2443

Closed

Description

@nikomatsakis

Currently, the compiler does not account for the fact that "by value" passing of ref-counted boxes is in fact a borrow. This can be fixed easily enough in borrowck (or, I imagine, in alias). In any case, I added an xfail'd test (by-val-and-by-move.rs) that demonstrates the problem. This test fails (note the environment variable RUST_POISON_ON_FREE---without that, the test may accidentally pass even though it is accessing a freed pointer).

// xfail-test
// exec-env:RUST_POISON_ON_FREE

fn it_takes_two(x: @int, -y: @int) -> int {
    free(y);
    #debug["about to deref"];
    *x
}

fn free<T>(-t: T) {
}

fn main() {
    let z = @3;
    assert 3 == it_takes_two(z, z);
}

Activity

  1. nikomatsakis commented on Jun 8, 2012

    @nikomatsakis
    ContributorAuthor

    just realized---this should be easy to fix with borrowck, but we don't handle it now. we don't seem to get the scope right when checking that there is no move from a borrow ptr.

  2. graydon commented on Aug 31, 2012

    @graydon
    Contributor

    Obsolete given scheduled removal of modes (or at least WONTFIX).

  3. added a commit that references this issue on May 13, 2023
    25c5451
  4. added a commit that references this issue on Jun 3, 2023
    053edf4
  5. added a commit that references this issue on Jun 3, 2025
  6. added a commit that references this issue on Aug 21, 2026
    b236f6a
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

    A-lifetimesArea: Lifetimes / regions

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions