Skip to content

A typed list literal accepts elements of another type: let ys: List[i32] = ["a", "b"] and let zs: List[str] = [1, 2] pass check #2348

Closed

Description

@ehartford

Spec: §4.3c (a bracket literal is typed by its demand), §4.2.6 (no implicit conversion between unrelated types).

Repro (installed compiler on main, 14a720bd7):

fn main:
    let ys: List[i32] = ["a", "b"]
    let zs: List[str] = [1, 2]
    print(f"{ys[0]} {zs.len()}")

Expected: two type errors (a str where an i32 is demanded, an integer where a str is).
Actual: with check passes with no diagnostic; with run prints 1 for a program that reads a str's first word as an i32.

Found while writing the D116 ++ compile-error fixture (err_d116_mixed_elements.w): the element types must be one, and the first draft of the fixture was accepted because the annotated literal never rejected its elements. The ++ check now refuses the mix on its own; the typed-literal gap is separate.

Activity

  1. ehartford commented on Oct 12, 2026

    @ehartford
    ContributorAuthor

    Narrowed on main c97a00852:

    fn take(xs: List[i32]) -> isize: xs.len()
    let a: List[i32] = ["a"]        // accepted
    let b: List[i32] = [1, "a"]     // refused: "sequence literal expressions do not establish one compatible owned result type"
    let c = take(["z"])             // accepted
    var d: List[i32] = List.new()
    d.push("q")                     // refused: wrong argument type in call to 'List.push'
    

    So the hole is a homogeneous literal whose elements all have one type that is not the demanded element: check_array_literal sets expected_elem from the annotation or parameter, and check_list_literal_elem compares types_compatible(elem_ty, actual_ty) then falls to emit_argument_type_mismatch("List.literal", …), yet no diagnostic lands; either types_compatible(i32, str) answers yes on this path or the mismatch emitter drops the "List.literal" callee. A trace at check_list_literal_elem with the two types decides it; one build.

  2. ehartford commented on Oct 12, 2026

    @ehartford
    ContributorAuthor

    Traced one step further on branch gaps-2: for let a: List[i32] = ["a"] and take(["z"]), check_list_literal_elem is never reached (a trace there printed nothing for the file), so the homogeneous literal is typed by a different route under an annotation or parameter demand (the D93/D113 literal-by-use path, collection_literal_hints / literal_decision), which never compares the literal's own element type with the demanded one. The fix belongs where that route adopts the demanded List type for the literal: compare its element with the elements' established type and refuse a mismatch there.

  3. ehartford commented on Oct 12, 2026

    @ehartford
    ContributorAuthor

    Fixed on branch gaps-3 (from a31a7df; gate running): in check_array_literal, after resolve_contextual_join, the joined element type must be compatible with the demanded element (or an arithmetic widening of it); otherwise sequence literal element is \str` where `i32` is demanded (§4.3c)`. The literal was typed as the demanded collection regardless. Fixture test/compile_errors/err_2348_typed_literal_foreign_element.w; the D93/D113/D116/D128 literal-typing tests and int-local-rechecks=0 unchanged.

  4. added 2 commits that reference this issue on Oct 12, 2026
  5. ehartford commented on Oct 12, 2026

    @ehartford
    ContributorAuthor

    Fixed: a sequence literal typed by a demand must hold the demanded element (check_array_literal, §4.3c); fixture err_2348_typed_literal_foreign_element.w.

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