Skip to content

A slice's .len field projection reads 0 (let n = arr.len); std.iter.count returns garbage once its tail is not defaulted #1517

Closed

Description

@ehartford

Found while implementing D60 / §4.10 (the fn-body default fix exposed it). Reproduced on origin/main 5848ae9 (stage1):

fn count3(arr: []i32) -> i64:
    let n = arr.len
    n
fn count5(arr: []i32) -> i64: arr.len()
fn main:
    let a: [i32; 3] = [1, 2, 3]
    print(count3(a[..]))   // 0
    print(count5(a[..]))   // 3

MIR is _2 = copy _1.len (a field projection named len on the slice local); Sema types it as the length, and the compiled read yields 0. str's .len projection reads correctly (let n = s.len gives the byte length). MirBuilder.place_local_type has no type for either projection (struct_field_type of a str/slice is 0).

std.iter.count[T](arr: [T]) -> i32: arr.len (lib/std/iter.w:50) is this read as a tail. On main its value was discarded for i32.default() (the body's default was chosen by the lowered operand's type, which that missing place type reads as Unit — fixed on the D60 branch), so count returned 0 for every slice; with the discard fixed it returns the broken projection read instead (7700496 for a 3-element slice in the run above). No caller in the tree.

Expected: arr.len on a slice reads the slice's length (§4.8a: a slice is (ptr, len)), or Sema rejects the field spelling in favor of .len() — whichever the spec intends.

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