Stream: bugs

Topic: Type mismatch with the same type


view this post on Zulip Sven van Caem (Nov 01 2024 at 12:40):

I'm getting the following error:

── TYPE MISMATCH in src/RustGlue.roc ───────────────────────────────────────────

This 2nd argument to |> has an unexpected type:

86│          |> List.map generateItemGroup
                         ^^^^^^^^^^^^^^^^^

This generateItemGroup value is a:

    ItemGroup -> Str

But |> needs its 2nd argument to be:

    ItemGroup -> Str

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

1 error and 1 warning found in 78 ms

Is this a known bug?

view this post on Zulip Brendan Hansknecht (Nov 01 2024 at 14:42):

Kinda,

view this post on Zulip Brendan Hansknecht (Nov 01 2024 at 14:43):

Though if you have a small repro, it would be great to collect

view this post on Zulip Brendan Hansknecht (Nov 01 2024 at 14:44):

I think the issue is that roc needed to the lambdaset and closure captures (which aren't displayed in the type signature, but are still pretty of the type got functions)

view this post on Zulip Sven van Caem (Nov 01 2024 at 19:58):

Thanks for your response!

view this post on Zulip Sven van Caem (Nov 01 2024 at 19:58):

I eventually found that the compiler uses the name ItemGroup to refer to two slightly different types. This was caused by one function that was able to return a tag that wasn't included in its type annotation. I was able to find a very small repro for the latter phenomenon:

view this post on Zulip Sven van Caem (Nov 01 2024 at 19:59):

The following code type checks, and the expect test succeeds (even though it shouldn't):

module [bug]

bug : U8 -> [A]
bug = \i ->
    when i is
        1 -> bug 3
        2 -> A
        _ -> B # <- should be illegal given the annotation

expect bug 1 == B

0 failed and 1 passed in 251 ms.

view this post on Zulip Sven van Caem (Nov 01 2024 at 20:02):

Commenting out the 1 branch results in the type mismatch you'd expect to see in the previous example:

module [bug]

bug : U8 -> [A]
bug = \i ->
    when i is
        # 1 -> bug 3
        2 -> A
        _ -> B

expect bug 1 == B
── TYPE MISMATCH in Bug.roc ────────────────────────────────────────────────────

Something is off with the body of the bug definition:

3│   bug : U8 -> [A]
4│   bug = \i ->
5│>      when i is
6│>          # 1 -> bug 3
7│>          2 -> A
8│>          _ -> B

This when expression produces:

    [B, …]

But the type annotation on bug says it should be:

    […]

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

1 error and 0 warnings found in 47 ms

view this post on Zulip Sven van Caem (Nov 01 2024 at 20:06):

It seems like something about the function being recursive causes the compiler to ignore the type annotation. However, changing the return type to something that's not a tag union does break as expected:

module [bug]

bug : U8 -> Str
bug = \i ->
    when i is
        1 -> bug 3
        2 -> A
        _ -> B

expect bug 1 == B
── TYPE MISMATCH in Bug.roc ────────────────────────────────────────────────────

The 2nd branch of this when does not match all the previous branches:

5│       when i is
6│           1 -> bug 3
7│>          2 -> A
8│           _ -> B

This A tag has the type:

    [A]

But all the previous branches have type:

    Str

All branches of a when must have the same type!

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

1 error and 0 warnings found in 46 ms

view this post on Zulip Sven van Caem (Nov 01 2024 at 20:09):

So the type annotation is not being completely ignored, but the tag union type is being silently extended.

view this post on Zulip Sven van Caem (Nov 01 2024 at 20:27):

Adding in a second type annotation seems to reintroduce the error also:

module [bug, test]

bug : U8 -> [A]
bug = \i ->
    when i is
        1 -> bug 3
        2 -> A
        _ -> B

test : U8 -> [A]
test = bug

expect bug 1 == B
── TYPE MISMATCH in Bug.roc ────────────────────────────────────────────────────

Something is off with the body of the test definition:

10│  test : U8 -> [A]
11│  test = bug
            ^^^

This bug value is a:

    U8 -> [B, …]

But the type annotation on test says it should be:

    U8 -> […]

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

1 error and 0 warnings found in 55 ms

view this post on Zulip Sven van Caem (Nov 01 2024 at 20:28):

...but replacing [A] with a type alias for [A] suppresses it again:

module [bug, test]

OnlyA : [A]

bug : U8 -> OnlyA
bug = \i ->
    when i is
        1 -> bug 3
        2 -> A
        _ -> B

test : U8 -> OnlyA
test = bug

expect bug 1 == B
0 failed and 1 passed in 186 ms.

view this post on Zulip Sven van Caem (Nov 01 2024 at 20:34):

Safe to say I'm puzzled!

view this post on Zulip Brendan Hansknecht (Nov 01 2024 at 22:15):

Oh wow. This is a great set of minimal repros. Can you file an issue? :folded_hands:

view this post on Zulip Sven van Caem (Nov 02 2024 at 12:43):

Sure! I'll file one tomorrow when I'm back at my laptop.

view this post on Zulip Sven van Caem (Nov 03 2024 at 18:20):

#7194

view this post on Zulip Sven van Caem (Nov 07 2024 at 10:23):

I've managed to reproduce the simply transcendental error message that led me to find this bug in the first place:

module [bug, test, wrongAnnotation]

OnlyA : [A]

bug : U8 -> OnlyA
bug = \i ->
    when i is
        1 -> bug 3
        2 -> A
        _ -> B

test : U8 -> OnlyA
test = bug

aToC : OnlyA -> [C]
aToC = \A -> C

wrongAnnotation : [C]
wrongAnnotation = aToC (bug 1)

expect bug 1 == B
── TYPE MISMATCH in Bug.roc ────────────────────────────────────────────────────

This 1st argument to aToC has an unexpected type:

19│  wrongAnnotation = aToC (bug 1)
                             ^^^^^

This bug call produces:

    OnlyA

But aToC needs its 1st argument to be:

    OnlyA

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

1 error and 0 warnings found in 44 ms

view this post on Zulip Sven van Caem (Nov 07 2024 at 10:24):

This feels like it might be a good hint as to what's causing this

view this post on Zulip Sven van Caem (Nov 07 2024 at 10:24):

I've added it to the issue


Last updated: Jul 06 2025 at 12:14 UTC