Skip to content

[value; N] with a const (or any non-literal) N builds a 1-element array: the parser's fill desugar silently uses count 1 (§4.3a) #1478

Closed

Description

@ehartford

Found while fixing #1358 (the ast dump renders the desugared fill).

Spec §4.3a Fixed-Size Arrays: "[value; N] repeat: array of N copies of value" and "Length N must be a compile-time constant (integer literal or const)."

Parser.w (the // Array fill: [value; N] branch, ~line 7563) desugars the fill at parse time into an NK_ARRAY_LIT with N copies of the element node, but it evaluates N only when the count is an NK_INT_LIT; for anything else it falls back to fill_count = 1 with no diagnostic:

var fill_count = 0
if self.pool.kind(count_expr) == NodeKind.NK_INT_LIT:
    ...
if fill_count <= 0:
    fill_count = 1

Repro (installed v0.15.2.1):

let N: i64 = 4
fn main:
    let a = [7 as i32; N]
    print(f"{a.len()}")          // expect 4, prints 1
    let b: [4]i32 = [7 as i32; N]
    print(f"{b.len()} {b[3]}")   // expect "4 7", prints "4 0"

[7 as i32; 2 + 2] also has length 1. The typed binding compiles and reads uninitialized/zeroed tail elements: silent wrong code (CLAUDE.md "No Silent Fallbacks").

Related cost: the same desugar stores N element ids in the AST extra table, so var the_cache: [16777216]u8 = [0 as u8; 16777216] (lib/std/tommyds/check_.w) adds 16,777,216 extras (64 MiB) at parse time and every phase that walks the literal walks 16M elements. A fill should stay one node (element + count expression) resolved by Sema, which also gives the const case a place to be evaluated or refused loudly.

Expected: [v; N] with a const N builds N copies; a count that is not a compile-time constant is a compile error naming it.

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