Skip to content

MIR: borrowed receivers (ch in s, IndexPlace get/set, generator next) are lowered as moves, so the drop-state sees a moved value that is still owned #1505

Closed

Description

@ehartford

Found while landing #1414's validate-ownership rule ("a move of an already-moved place"). The rule judges statement moves only, because three lowerings pass a receiver the callee borrows as an OK_MOVE operand. The drop-state dataflow then marks the receiver Moved after the first call, and the second call "moves a moved place" although nothing was ever moved. Stage2 of main f1e3bd2, --dump-mir:

  1. ch in s (MirLower.lower_str_contains_char, lower_receiver_with_method_autoderef): the STR_CONTAINS_CHAR receiver.

    let email = f"user{n}@example.com"
    assert('@' in email)
    assert('!' not in email)
    
    call const fn <unknown>(move _1, const 64ty3) -> [return: _2, next: bb1];
    ...
    call const fn <unknown>(move _1, const 33ty3) -> [return: _6, next: bb3];
    ...
    drop(_1)   // --dump-drop-plan: state_before=Moved action=skip
    

    Codegen reads the receiver as a value and calls with_str_contains_char_ref (a borrow); run --debug-alloc is clean (leak 0), so the MIR's move is not what runs. (test/spec/spec_ss09_9_the_in_operator.w)

  2. g[i] += 100 over a user IndexPlace (fn get(self: &Self, ...), fn set(mut self: Self, ...)):

    call const fn sym770(move _2, copy _10) -> [return: _11, next: bb6];        // get(&Self)
    call const fn sym771(move _2, copy _10, copy _12) -> [return: _0, ...];     // set(mut self)
    

    (test/behavior/behav_index_place_user.w)

  3. A generator's next() called repeatedly on the same var iter:

    call const fn sym994(move _1) -> [return: _3, next: bb2];
    ...
    call const fn sym994(move _1) -> [return: _16, next: bb9];
    

    (test/spec/spec_ss13_4_no_references_across_yield.w)

In each case the call's ABI (FnAbi: IndirectPlace for the in-place receiver, the &Self reference, the _ref runtime helper) borrows; only the MIR operand says move. Anything that reads MIR ownership believes the value left: the drop plan says skip for a drop that codegen still runs, and #1414's rule, applied to call arguments, rejected these three legal programs (validate-all).

Expected: a borrowed receiver is lowered as the borrow it is (a ref / place operand, or OK_COPY of a place the callee reads in place), so --dump-drop-plan says drop for the final drop of email, iter and g, and #1414's rule can judge call arguments too (MirCore.validate_ownership_body, the terminator arm left out for this).

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