Skip to content

with expr as mut x: (§7.2 builder) drops the builder before returning it: every Vec pushed inside comes back empty #1290

Closed

Description

@ehartford

Found rewriting `examples/ecs`, `examples/nebula` and `examples/service` under #1150 ruling 4; the spec's own §7.2 example is the repro.

```
fn main:
let v = with Vec.new() as mut v:
v.push(1)
v.push(2)
print(f"{v.len()}") // expected 2; prints 0
let s = with "" as mut acc:
acc = acc ++ "ab"
print(s) // expected ab; prints nothing
```

A struct builder (`with Cfg {} as mut c: c.timeout = 30`) happens to work because the field stores land in the same storage; anything with an owned buffer or a reassignment is lost. The `var out = Vec.new(); out.push(1); out` desugaring (§7.2:2906) works.

Root cause (exact lines, MIR evidence). `src/MirLower.w` `lower_with_binding`: line 12496 schedules `schedule_drop(local, DK_VALUE)` for the builder local, and the Form 2 return at line 12505 hands back `OK_COPY` of that local. The scope-exit drop therefore runs before the copy:

```
bb2: {
drop(_1) @ drop#2 scope-exit _1;
_0 = copy _1;
```

(`with check w.w --dump-mir` on the `with`-form function). The `var` form lowers to `_0 = move _1; _1 = const zst` with no drop. The fix is to move the builder out (`OK_MOVE`, with the scheduled value drop cancelled / reset-on-move), which is exactly the spec's desugaring. Branch `with-builder-move` carries it; it is a drop-scheduling change and runs its own battery (isolation rule).

Installed `with v0.15.2.1-g478b26481` and the main-HEAD stage1s in `printd`/`exdefects` all reproduce.

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