Skip to content

Unannotated generic struct literal: 'cannot infer generic method type parameter T' though the initializer decides it #1739

Closed

Description

@ehartford

Spec: §5 (generic types), §4 inference.

A struct literal of a generic type bound without an annotation leaves the type parameter uninferred for method calls, although the field initializers determine it:

type Stack[T] { items: Vec[T] }
impl[T] Stack[T]:
    fn count() -> i32: self.items.len() as i32
fn main:
    let st = Stack { items: ["p".clone(), "q".clone()] }
    print(st.count())
error: cannot infer generic method type parameter 'T'

let st: Stack[str] = … works. The mission: the initializer already determines T, so the annotation is ceremony. Found testing #1724 (gen methods on generic owners behave the same).

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