Skip to content

Validate non-Copy ownership duplicated through borrowed MIR places #1132

Closed

Description

@ehartford

with check --validate-all --dump-mir accepts an owning copy of a non-Copy value through a shared reference. The copied header is then dropped, freeing the original owner's storage.

Reproduction (Phase 1 parent e7ddf11, compiler ee1884c):

use std.collections.Vec
fn observe(source: &str):
    let value = *source
    assert(value == "alpha")
fn main:
    var values: Vec[str] = Vec.new()
    values.push("alpha".clone())
    observe(values.get(0))
    assert(values.get(0) == "alpha")

Run with WITH_DEBUG_ALLOC=1 WITH_ALLOC_NO_REUSE=1 WITH_DEBUG_ALLOC_SCRIBBLE=1 with test repro.w: the assertion after observe fails. Validation exits zero and emits:

_3 = copy _1;       // &str
_2 = copy _3.*;     // owning str local
...
drop(_2) @ drop#10 scope-exit _2;

The same lowering in str.cmp corrupts owning SortedVec strings. LLDB observed with_str_free_drop_origin at rt_core.w:2730 returning owned=1 for the left comparison operand; the call came from the inlined str.cmp in the C comparator. The native address trap reported trap-free hit=1 ... origin=drop#struct __drop_struct_16 for that live collection element's payload.

Root producer: MirLower.lower_binding_alias_place omits dereference projections, so lower_let_binding falls through to an owning local; lower_unary emits OK_COPY through the shared reference. The Phase 1 fix records the borrowed projection in Sema and consumes it as a place alias in MIR. This issue tracks the independent validator gap: an owning non-Copy copy through a shared borrow must fail validation unless an explicit materialization/clone establishes an independent owner.

Acceptance: reject the malformed MIR above, accept legitimate borrowed aliases and explicit clones, and retain coverage for equivalent field and dereference projections. D22 §§5, 13.6 and the ownership contract require that observation never acquires the source's drop obligation.

Activity

  1. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): does not reproduce (in today's spelling: Vec is List, D118, and a positional collection is indexed, D71).

    $ cat main.w
    fn observe(source: &str):
        let value = *source
        assert(value == "alpha")
    fn main:
        var values: List[str] = List.new()
        values.push("alpha")
        observe(values[0])
        assert(values[0] == "alpha")
        print("ok")
    $ with run --debug-alloc main.w
    ok
    debug-alloc: leak count=0 allocations=2
    
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