roc-lang / roc

A fast, friendly, functional language.
https://roc-lang.org
Universal Permissive License v1.0
4.46k stars 313 forks source link

Type mismatches should focus on difference, not complete type #6958

Open Anton-4 opened 3 months ago

Anton-4 commented 3 months ago

There is a lot of noise in this error message, the important part should be focused on; the mismatch between Str and List Str

── TYPE MISMATCH in examples/helloWorld.roc ────────────────────────────────────

Something is off with the then branch of this if expression:

6│  myResult: U64 -> Result Str [TooSmall]
7│  myResult = \x ->
8│      if x > 5 then
9│          Ok ["large enough!"]
            ^^^^^^^^^^^^^^^^^^^^

This Ok tag application has the type:

    [
        Err [TooSmall],
        Ok (List Str),
    ]

But the type annotation on myResult says it should be:

    Result Str [TooSmall]

────────────────────────────────────────────────────────────────────────────────

Code to produce the error:

app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.12.0/Lb8EgiejTUzbggO2HVVuPJFkwvvsfW6LojkLR20kTVE.tar.br" }

import pf.Stdout
import pf.Task

myResult: U64 -> Result Str [TooSmall]
myResult = \x ->
    if x > 5 then
        Ok ["large enough!"]
    else
        Err TooSmall

main =
    Stdout.line! (Inspect.toStr (myResult 6))
Anton-4 commented 3 months ago

Somewhat related issues: https://github.com/roc-lang/roc/issues/6957, https://github.com/roc-lang/roc/issues/6906