Skip to content

A Vec's element type is inferred from its literal: let xs: Vec = [1, 2, 3], and let xs = [1, 2, 3] where a use demands a Vec #2144

Closed

Description

@ehartford

Eric, 2026-10-04, on README's let xs: Vec[i32] = [1, 2, 3]: "cannot the T of xs be inferred?" and "even java infers vector type from the value."

Today (v0.15.3.0, main a45324c), with fn total(xs: &Vec[i32]) and fn archive(xs: Vec[i32]):

Spelling Result
let xs = [1, 2, 3] then total(xs) [3]i32; "wrong argument type in call to 'total'"
let xs: Vec = [1, 2, 3] "type mismatch in binding"
let xs: Vec[i32] = [1, 2, 3] works; the only spelling that does

Spec: §4.3c: "The concrete collection is selected by expected type, with sensible defaults"; rule 1: "The element form [a, b, c] defaults to a fixed array [T; N] (§4.3a). When the expected type is Vec[T], HashSet[T], or BTreeSet[T], the literal builds that collection instead."

Wanted:

  • A. let xs: Vec = [1, 2, 3] is Vec[i32]: the annotation names the collection and the literal decides its arguments (likewise HashSet, BTreeSet, BTreeMap). Nothing in §4.3c forbids it; the compiler refuses it.
  • B. let xs = [1, 2, 3] is a Vec[i32] when its uses demand one (a &Vec[i32] or Vec[i32] parameter, a push), the rule D88/D89 gave numeric literals ("takes the type its uses demand"). With no deciding use it stays the array. This changes §4.3c rule 1 and needs blessed words.

Others: Java var xs = List.of(1, 2, 3) is List<Integer>; Rust let xs = vec![1, 2, 3] is Vec<i32> and Vec<_> leaves the element to inference; Swift let xs = [1, 2, 3] is [Int]; Go and Zig spell the element type.

Then: README's ownership example drops : Vec[i32].

Activity

  1. ehartford commented on Oct 5, 2026

    @ehartford
    ContributorAuthor

    Proposed rule (Eric agrees in principle)

    A. let xs: Vec = [1, 2, 3] — an expected type that names the collection but not its element type takes the element type from the literal. Nothing is ambiguous, so requiring Vec[i32] is the compiler asking for what it already knows.

    B. let xs = [1, 2, 3] settled by use. The literal's kind (fixed array, Vec, HashSet) starts open, as an integer literal's width does.

    • A use that requires one kind settles it: an argument to a &Vec[i32] parameter, a Vec[i32] return, a Vec field, a Vec-only method.
    • Uses that require different kinds are an error asking the user to spell the type (two meanings remain).
    • With no requiring use, the fixed-array default stands.
    • Inference stays within one function; it never crosses a signature.

    Not an implicit fixed-array-to-Vec conversion at the call site: that would be a hidden allocation and copy, which must be spelled. Under B the allocation happens at the literal, exactly as let xs: Vec[i32] = [1, 2, 3] does today.

    C. The Python-programmer case must compile:

    var xs = []
    xs.push(1)
    xs.push(2)
    

    push exists only on growable collections, so it settles the kind (Vec); its argument settles the element type (i32). Calls every candidate supports (len(), for x in xs) settle nothing.

    Implementation notes

    • Existing machinery: SemaCheck.w's pending generic bindings already settle var xs = Vec.new() from a later push, and var best = None (var best = None passes check, then codegen fails: aggregate enum payload missing destination payload type #2103). B is the mirror image: element type known, kind open. C has both open.
    • Kind-neutral uses before the settling use (print(xs.len()) before total(xs)): look ahead over the binding's uses (there is a later-use helper near SemaCheck.w:33173) and settle before checking them, so the move checker sees the final type from the start (Vec has Drop, [i32; 3] does not).
    • Generalize, don't add a third special case. The pending mechanism currently names Vec/HashMap/HashSet, new/with_capacity, and None separately. Make it one concept, a binding with open dimensions (kind and/or type arguments) settled by use, with Vec.new(), None and literals as instances.
    • Lowering: retype the literal node on settlement so MIR builds the settled collection.

    Acceptance

    • let xs: Vec = [1, 2, 3] is Vec[i32]
    • let xs = [1, 2, 3]; total(xs) with total(xs: &Vec[i32]) compiles
    • print(xs.len()) before the settling use compiles
    • conflicting requiring uses → a located error naming both uses and suggesting an annotation
    • var xs = []; xs.push(1) is Vec[i32]
    • a literal with no requiring use is still [T; N]
    • README's ownership example becomes let xs = [1, 2, 3]
  2. ehartford commented on Oct 5, 2026

    @ehartford
    ContributorAuthor

    Settled since (Eric, 2026-10-04)

    How the kind is chosen. A bracket literal can only become a kind §4.3c lists (fixed array, Vec, HashSet, BTreeSet; HashMap/BTreeMap for the key: value form). A use settles the kind only when exactly one candidate fits it:

    • push(x) → Vec (the sets use insert)
    • insert(x) fits HashSet and BTreeSet → error asking for an annotation (Vec.insert takes two arguments, so arity rules it out)
    • len(), contains(), iteration → settle nothing

    Making another type literal-buildable later may turn settled code ambiguous (a compile error); it must never silently change the kind.

    Element type. Pushes onto a pending empty literal behave as if their arguments were the literal's elements: var xs = []; xs.push(a); xs.push(b) types exactly as [a, b]. Integer literals stay open until a requiring use, as in a literal, so a later Vec[i64] use gives Vec[i64].

    Mixed element types are an error, with a fix. xs.push(1); xs.push("potato") reports both pushes and offers the With idiom:

    error: mixed element types: i32 (push at line 2) and str (push at line 3)
    help: a list of different kinds of things is an enum
    

    plus an LSP quick-fix that declares the enum and wraps the pushes.

    Out of scope here: an any type and anonymous union types. Neither is planned; mixed data is served by enums, Box[dyn Trait] with downcast patterns, and (separately) an owned dynamic Value type in the standard library.

    Additional acceptance

    • insert(1) on a pending literal → ambiguity error suggesting HashSet or BTreeSet
    • var xs = []; xs.push(1) then a Vec[i64] use → Vec[i64]
    • mixed pushes → the error above, naming both lines
    • var xs = [] with no settling use → error asking for the element type
  3. ehartford commented on Oct 5, 2026

    @ehartford
    ContributorAuthor

    A bare generic name as an annotation is broken for every generic, not only for literals (v0.15.3.0):

    • let o: Option = Some(1) → internal compiler error: "invalid MIR before codegen … local _1 has ty=94, a generic declaration with no …"
    • let v: Vec = Vec.new() then v.push(1) → "MIR lowering failed for function 'main'"
    • let xs: Vec = [1, 2, 3] → "type mismatch in binding"

    Part A is therefore one rule in Sema's binding check: an annotation naming a generic type without arguments is completed from the initializer (its type when the base matches; for a collection literal, the collection of the literal's element type), and a binding whose arguments nothing decides is an error that says so. The ICE goes with it.

  4. ehartford commented on Oct 5, 2026

    @ehartford
    ContributorAuthor

    Landed (main 6b0c99d): the annotation half (#2146) and D93's use demands (#2155): a parameter, a typed let, a return, and a method exactly one collection has, with the element type settled by the demand, the two-demand error, and the empty literal. Still owed from §4.3c rule 1: assignment to or from a typed place (xs = some_vec, place = xs) and a script's top-level bindings. README drops the annotation when those are in.

  5. ehartford commented on Oct 5, 2026

    @ehartford
    ContributorAuthor

    Complete on main 3207aac: the annotation half (#2146), D93's demands from parameters, typed lets, returns and methods (#2155), and assignment to or from a typed place (#2159). Scripts' top-level bindings follow the same rule. README's examples read let xs = [1, 2, 3].

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