Skip to content

Sema accepts length-less [T] binding annotation; produces silently corrupt slice (xs.len() wrong) #622

Closed

Description

@ehartford

Summary

let xs: [i32] = [1, 2, 3] compiles WITHOUT diagnostic and runs corrupted: xs.len() returns 2 (garbage — the binding is slice-typed but holds array-literal bits, so the fat-pointer header is misread). Affects any element type (found while fixing #586: let opts: [?i32] = [...] asserted len()==3 false after the element-typing fix).

Spec

§4 (line ~1628): the array type is [T; N]; the slice type is []T (§4.8). A bracketed length-less [T] binding annotation is NOT a defined type form. Per §20b (forbid, don't warn) sema should REJECT the annotation with a hint ("did you mean [T; N] or []T?") — silently accepting it manufactures a corrupt view.

Repro (HEAD, post-1da33dac)

fn main:
    let xs: [i32] = [1, 2, 3]
    print_i32(xs.len() as i32)   // prints 2, expected: rejection at check time

Candidate fix

resolve_type_expr's bracketed-type branch: a length-less non-[] bracket type in binding/annotation position is an error. Note stage-1 []mut work added in_param_type_position — the rejection must not affect legal []T/[]mut T params.

Activity

  1. ehartford commented on Jul 2, 2026

    @ehartford
    ContributorAuthor

    Ruling (WWED, mission-derived): reject with a teaching hint. [T] as inferred-length sugar was considered and declined: [T] vs []T one character apart with different meanings is a footgun, and the annotation is unnecessary anyway — inference already handles let xs = [1,2,3]. Diagnostic shape: "[i32] is not a type — write [i32; 3], []i32, or drop the annotation (it is inferred)". Guardrail restored (§20b forbid-don't-warn), zero ceremony added. Scheduled in the campaign's robustness track.

  2. ehartford commented on Jul 4, 2026

    @ehartford
    ContributorAuthor

    Re-triage (2026-07-03): the provisional 'reject [T] with a hint' ruling has a wrong premise — dropping this from the easy set.

    Two findings on HEAD:

    1. [T] is not an undefined form — the parser deliberately treats a length-less [T] as slice shorthand for []T (src/Parser.w:7361-7364), and the stdlib relies on it as slice-parameter syntax: lib/std/iter.w:50 pub fn count[T](arr: [T]), :54 contains(arr: [i32], …). Rejecting [T] outright would break those.
    2. The corruption is not [T]-specific — the proper form let xs: []i32 = [1, 2, 3] also returns xs.len() == 2 (garbage). So the real bug is array-literal → slice-binding coercion: assigning an array literal to a slice local binding doesn't build a correct fat-pointer header (or should be rejected as binding a slice view to a temporary array).

    So the fix is a coercion/semantics decision, not a one-line syntax rejection: either (a) materialize the array literal and have the slice binding view it (with correct len/ptr), or (b) reject any array-literal-initialized slice local binding ([]T and [T] alike) with a hint to use [T; N] for a fixed array — but keep [T]/[]T valid in parameter position where call-site array→slice decay works. Needs a ruling on slice-from-literal semantics (§4.8). Reclassified medium; not part of the easy sweep.

  3. ehartford commented on Jul 14, 2026

    @ehartford
    ContributorAuthor

    Fixed in edd472d.

    Corrected diagnosis (the original approach would not have worked)

    The report implicates the [T] surface syntax, and the batch investigation proposed rejecting [T] at the parser. That is wrong on two counts, confirmed by running the current compiler:

    1. The corruption is not specific to [T]. The proper slice spelling corrupts identically:

      let a: []i32 = [1, 2, 3, 4]   // a.len() == 2   (should be 4)
      let a: [i32]  = [1, 2, 3, 4]  // a.len() == 2
      

      xs.len() reads element[1] of the array (2 for [1,2,3], 20 for [10,20,30]), and indexing the "slice" segfaults (rc 139). The array literal is bit-reinterpreted as a slice header {ptr, len}.

    2. Rejecting [T] at the parser would break the stdlib: lib/std/iter.w uses [T]/[i32] parameters, which work correctly (sumlen([10,20,30]) returns 3).

    Real root cause

    check_array_literal (src/SemaCheck.w) returns the expected slice type for an array literal when the expected type is a slice. So let xs: []i32 = [1,2,3] sees matching types at the binding check (line ~8143) — no mismatch — while MIR lowers the literal as an array value. Type and value disagree; codegen then reads the array's raw memory as a slice header.

    Fix

    Binding a slice to an array literal is fundamentally unsafe regardless of lowering: the temporary array is freed at end of statement, leaving the slice dangling (the call-argument coercion path rejects the same temporary at SemaCheck.w:422 for exactly this reason). The named-array form let xs: []i32 = arr is already rejected as a plain "type mismatch in binding". This makes the literal form reject consistently and loudly:

    error: cannot bind an array literal to a slice type; use an array binding `let xs = [...]` or an explicit array type `[T; N]`
    

    Silent corruption and the OOB read are eliminated. Nothing in src/lib/test relied on the pattern (verified). Fixtures: err_slice_binding_from_array_literal[_bracket] (both spellings reject), behav_array_binding_and_slice_param (array binding + slice params still work).

    Follow-up (not a release blocker)

    Proper array-literal-to-slice binding via temporary lifetime extension (Rust-style rvalue promotion) is a separate future enhancement.

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