Skip to content

Gate-flip regression: view-origin analysis rejects issue44_opaque_struct_call.w ("view 'G' may outlive its origin 'raw'") #575

Closed

Description

@ehartford

On the flipped-gate staged compiler (#564 tree), the previously green positive test test/behavior/issue44_opaque_struct_call.w now fails:

error: view 'G' may outlive its origin 'raw'
 --> test/behavior/issue44_opaque_struct_call.w:18:5

Site (file unchanged):

var G: *mut Handle = null

fn singleton() -> *mut Handle:
    if G != null:
        return G
    let raw = unsafe { malloc(32usize) }.unwrap()
    let p = raw as *mut Raw
    unsafe:
        (*p).base = raw
    G = p as *mut Handle
    G

raw is a heap allocation from malloc, not a borrowed view, so the global assignment cannot outlive its origin. Pre-flip installed compiler passes the test. Not a §3.8 test-corpus issue; needs a compiler-side fix, so the test was intentionally left unmodified.

Activity

  1. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    Resolved in 1f9ec56: the repaired view-deps analysis (it previously dropped deps when its accumulator realloc'd) legitimately flags the global-keeps-local-allocation shape; issue44's fixture now assigns the global directly from the allocation, same conformance as the six ClangBridge sites and two rt_core sites fixed in the same series. Left open for the maintainer is the design question whether raw heap-allocation results should be exempt from scope-bound view-origin tracking — if so, all those rewrites can relax.

  2. ehartford commented on Jun 13, 2026

    @ehartford
    ContributorAuthor

    Design analysis complete. Recommendation: raw pointers are not views — drop TY_PTR from view-origin tracking everywhere (option b).

    The spec already says so: §22.1 Rule 1's ephemeral set is exactly &T, StrView, &[T] — raw pointers absent; §5.4 defines views as referencing memory "they do not own" (a malloc result owns); §16.11 places raw-pointer validity at the unsafe deref/unsafe fn boundary, not in scope-tracked lifetimes; §22's preamble: "False rejection of actually-safe code is compiler precision debt." And the compiler is already internally inconsistent: the return path exempts raw pointers (TY_REF-only gates at SemaCheck.w:883/5442/6073 — probed: returning a malloc result through a ptr local passes), while the scope-exit path flags them.

    Probed precision profile — the rule is inverted: it reliably rejects the always-safe shape (heap alloc → global) and misses the genuinely dangerous one: var x = 5; G = &raw mut x passes clean (collect_expr_view_deps's NK_UNARY branch only handles UOP_REF, not UOP_RAW_REF_*; the i32 local contributes no dep). All 10 sites rewritten during #564 (6 ClangBridge, 3 rt_core, issue44's fixture) were false positives — heap allocations or out-params, zero real bugs. All 7 in-tree may outlive compile-error tests are &T-based; zero regress under the change.

    Mechanism: drop or tk == TypeKind.TY_PTR at SemaCheck.w:5775 and narrow the assignment-recording gate at :6276 to match the return paths. The 10 rewrites may then optionally relax, and the migrated-PCRE2 view exemption (Sema.w:3216) can likely shrink. Suggested spec note after §21.1 Rule 6: "Raw pointers are not views… A *const/*mut value is an address, not a borrow; validity is asserted where it is dereferenced inside unsafe (§16.11) or across an unsafe fn boundary."

    Rejected alternatives: per-allocator @[returns_owned] (wrong altitude — casts, arithmetic, c_import returns, out-params all keep false-positiving); unsafe-fn-scoped exemption (doesn't fix this issue's safe-fn repro). A provenance-keyed stack-address-escape lint (&raw mut local reaching longer-lived places) is worth a separate design — it can't be a hard error since the runtime legitimately registers stack addresses with guaranteed unregistration (with_scope_track).

  3. ehartford commented on Jul 2, 2026

    @ehartford
    ContributorAuthor

    Closing as fixed on current HEAD: with check test/behavior/issue44_opaque_struct_call.w is clean — no 'view G may outlive its origin raw' (§21.1 Rule 6). The gate-flip regression no longer reproduces.

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