Skip to content

A view through ?? loses its temporary's origin: (read_file(p) ?? "").trim() is accepted and reads freed memory (§21.1, D22) #2659

Closed

Description

@ehartford

Spec: §21.1 (a view of a temporary), D22 ("Option, Result, patterns, ?, ?? and eliminators are transparent to view origins").

use std.fs
fn main:
    let p = "some/file.txt"
    let b = (read_file(p) ?? "").trim()     // accepted
    let u = read_file(p).unwrap().trim()    // refused: "`u` binds a view into a temporary `str` that is freed when this statement ends (§21.1)"

b is a view into the str that read_file(p) ?? "" produced, a temporary freed at the end of the statement, exactly as u's is. The unwrap() spelling is refused and the ?? spelling is accepted, and b then reads freed memory. In a program that later builds a list (&["x", b], the argv for std.process.run) the bytes come back as zeros: a 141-byte title arrived at gh as 142 empty arguments ("accepts at most 1 arg(s), received 142").

Route output (installed v0.15.3.0, main dfbafcf):

$ with analyze view4.w 'explain:origin:main:b'
  binding `b` bind at view4.w:4: origins=[] storage=[] deps=[]

The origin is lost at ??: the tool reports no origin where unwrap() keeps it.

Expected: ?? carries the origin of the value it yields (D22), so b is refused with the same §21.1 diagnostic as u. Workaround: bind the ?? result first (let a = read_file(p) ?? "" then a.trim()), which is correct today.

Activity

  1. ehartford commented on Oct 26, 2026

    @ehartford
    ContributorAuthor

    Fixed on main by 9fcb78a (stack #2743, battery GREEN on its top ddb68e8: behavior-tests 1949/1949, gate, uat and battery-checks green).

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