Skip to content

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

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions