gleam icon indicating copy to clipboard operation
gleam copied to clipboard

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

Open yoshi-monster opened this issue 10 months ago • 2 comments

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.

~ 💜

yoshi-monster avatar May 30 '25 23:05 yoshi-monster

Sounds reasonable. Would this be a breaking change in any situations? I can't think of any myself.

lpil avatar May 31 '25 12:05 lpil

The only thing we need to make sure is that variant inference is preserved. Other than that, it should only change error messages when the types don't line up

GearsDatapacks avatar May 31 '25 12:05 GearsDatapacks