Skip to content

Sema: a function returns a view of a global under a parameter or receiver origin; the caller's view escapes §21.1 and dangles #1903

Closed

Description

@ehartford

Spec: §21.1 rule 1 (a call writes every global its callee writes, so a view of that global may not be live across it); §9.1c (globals are places); D39 (a returned reference's origin follows elision: the receiver, else the single borrowed parameter).

Bug: a function may return a view of a global while its signature says the view borrows a parameter (or the receiver). Sema accepts the body, records the result's origin as the parameter, and the caller's view of the global is then invisible to the §21.1 check: a later call that writes the global is accepted, and the view dangles.

var HIDDEN: Vec[i32] = Vec.new()
fn grow():
    for i in 0..1000: HIDDEN.push(i)
fn first(p: &Vec[i32]) -> &i32: &HIDDEN[0]
fn main:
    HIDDEN.push(41)
    let x: Vec[i32] = Vec.new()
    let r = first(&x)
    grow()
    print(*r)

Expected: refused. Either the body is refused (it returns a view whose origin, HIDDEN, is not the declared origin p), or the call's result views HIDDEN and grow() is refused for writing it while r is live.

Actual: with check says ok; with run --debug-alloc prints 5668928 (41 expected): a read of the buffer grow freed. The control, let r = &HIDDEN[0]; grow(), is refused correctly ("call to grow mutates global HIDDEN while r is a live view into it"). A receiver origin (impl Pair: fn hidden() -> &Pair: &HIDDEN) is accepted the same way.

Why it matters for bundles: Eric's 2026-09-29 ruling on bundle write contracts counts only exported globals, on the premise that "the signature's origin already covers" a private global reached through a returned view. It does not: the origin a signature states is not checked against the body's real origin when that origin is a global, so a private global can escape through a returned view in one compilation and across a bundle boundary alike. With no borrowed parameter (fn peek() -> &Pair: &HIDDEN) the source compiles, and only the bundle build refuses it (ambiguous elision).

Workaround: none in the compiler; do not return views of globals.

Found on stack #1898 (fix-globals) during the bundle write-contract work.

Activity

  1. added
    bug
    on Sep 29, 2026
  2. ehartford commented on Oct 3, 2026

    @ehartford
    ContributorAuthor

    Decision brief (coordinator, 2026-10-03; the draft fix on wave-sema was reverted pending this).

    1. Others. Rust: references to a static mut warn, and are denied in edition 2024 (rustc_lint/src/static_mut_refs.rs); mutating a static needs unsafe, so a returned view of a mutable global never reaches safe code. Swift: any global is reachable by any code; exclusive access is enforced at run time. Vale: no mutable globals to compare.

    2. Spec. §21.1 rule 6 records a returned view's origins from "one or more parameters" only; nothing covers a view of a global. §9.1c: globals are places governed by §21.1. D39/D79: a bundle interface exports declared global writes, "exported globals only" (Eric, 2026-09-29).

    3. Mission fit.

    • (A) Refuse a body that returns a view of a global under a parameter/receiver origin. This also outlaws the accessor fn peek() -> &Config: &CONFIG, which compiles today: ceremony for a pattern the compiler can check.
    • (B) A returned view's origins include the globals the body views. The caller's existing global-write check (§21.1 rule 1) then refuses grow() while the view is live. The accessor stays legal and is just as safe. Cost: bundle interfaces must export that origin, which extends the 2026-09-29 'exported globals only' premise to view origins.

    4. Prediction. B, ~60%: it keeps the accessor and follows 'globals are places' (§9.1c) to its conclusion. The tension with the D79 interface premise is why it's Eric's call, not the coordinator's.

    Draft words if B (§21.1 rule 6): "…from one or more parameters, and from every global the body returns a view of; a global origin is part of the function's interface (D79)."

  3. ehartford commented on Oct 3, 2026

    @ehartford
    ContributorAuthor

    Eric's direction (2026-10-03): B, and it widens the 2026-09-29 bundle ruling. Revised draft words for §21.1 rule 6 (need Eric's blessing of the words):

    A returned view's origins are every parameter it derives from and every global the body returns a view of, directly or through a callee's returned view. A signature names a global origin with from G, beside from p. A declared origin the body does not derive the view from is an error, never a silent widening. A global origin is part of the function's interface (D79): a bundle interface records it by an identity that does not export the global, and every exported function that writes that global declares it in its writes clause, whether or not the global is exported.

    Consequence for D79: "exported globals only" becomes "exported globals, and every global that is an origin of an exported function's returned view". The repro's first (declares from p, returns a view of HIDDEN) is refused.

  4. ehartford commented on Oct 3, 2026

    @ehartford
    ContributorAuthor

    Fixed on main in 74e3001 (stack #2032; seed-driven battery GREEN on 2b9ad28, tree identical to main).

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions