Skip to content

let bindings are typechecked using their inferred type not their (narrowed) annotated type #4642

@yoshi-monster

Description

@yoshi-monster

When a let binding produces a type error because of its annotation, the rest of the code is type-checked with the actual inferred type of the binding instead of the (possibly narrowed) annotated type. This potentially produces misleading follow-up type errors:

pub fn main() {
  let x: String = 5  // type error: expected String, got Int
  let y: Int = x     // valid
  let z: String = x  // type error: expected String, got Int
}

I think the assignment to y should produce the type error here, not z. I would have expected one of the major benefits of adding explicit annotations to be to help the compiler produce better errors in exactly those situations. My assumption here of course is that if the user adds an explicit annotation, they want and expect that annotation to be assumed the correct type.

~ 💜

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedContributions encouraged

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions