Skip to content

Sema: a view into a temporary escapes its statement silently — let p = args().get(1) reads as "" instead of being rejected #962

Closed

Description

@ehartford

Repro (seed v0.15.1.7-g54651442e)

use std.process
let p = args().get(1)
print(f"view-into-temporary=<{p}> len={p.len()}")
$ with run tmpview.w HELLO
view-into-temporary=<> len=0

Binding the Vec first (let a = args() then a.get(1)) prints HELLO. Under WITH_DEBUG_ALLOC=1 the run reports no leak and no use-after-free, so the freed buffer is not touched: the view is an empty {ptr, len} rather than a dangling one (the runtime's arg vector materialization returns a temporary whose element view is taken and the temporary dropped at end of statement; the observed value is the zeroed slot).

Why it is a bug

D27: xs.get(i) returns &T, a view borrowing xs. Here xs is a temporary that dies at the end of the let, so the view has no living origin. §5.4 (ephemeral views) and §3.4 (returned references carry their origin) make this a compile-time rejection — "view may outlive its origin" is exactly the diagnostic the checker emits for a local that dies; a temporary is the shortest-lived local of all. Instead the program compiles, runs, and yields an empty string, which is the silent-fallback class CLAUDE.md forbids. It cost a real hour today: a with run rewrite script read its target path this way, found nothing to rewrite, and panicked with an empty path in the message.

Expected

with check (and run/build) reject it: "view into a temporary cannot outlive the statement; bind the value first" with the D27 hint (let argv = args()). Origin tracking already distinguishes locals from temporaries for check_returned_view_origins; the same origin must be stamped on element views of a temporary receiver at let initialization.

Found while auditing one-liners (docs/improve_oneliners.md, 2026-09-03).

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