Skip to content

validate-ownership: str as []u8 lowers as a move of the str, so a second cast is "two owners free one value" (runtime drops once, leak count=0) #1719

Closed

Description

@ehartford

Spec: §2.5.1 (one owner), §3.8 (a view observes; a cast to []u8 does not transfer the str).

Repro (any current compiler; also the seed v0.15.2.1, so pre-existing):

fn main:
    let input = "abc".clone()
    let a = input as []u8
    let b = input as []u8
    print(a.len() + b.len())
$ with check cast2.w --validate-all
error: validate-all failed: ownership MIR: fn sym803 stmt10 span=77: move of _1, which a path reaching it already moved out (Moved) and nothing re-initialized: two owners free one value (§2.5.1)
$ with run --debug-alloc cast2.w
6
debug-alloc: leak count=0

Expected: the cast produces a view of input; MIR records no move, --validate-all passes. The program itself is right at runtime (one drop, no leak), so the MIR move record disagrees with what codegen does: the ownership validator reports a double move that the drop schedule does not act on. One of the two is wrong, and the validator is the one that stayed silent-in-reverse: it would flag every program that casts a str to bytes twice.

Where: MirLower.lower_cast (the str as []u8 deref path) emits a move operand for the str source. It should be a place/view read.

Found by the --validate-all sweep over test/behavior on PR #1718: test/behavior/behav_bzip2_facade_roundtrip.w (span 1421, input as []u8 twice) is the in-tree instance.

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