oridb / mc

Myrddin Compiler
MIT License
387 stars 34 forks source link

Compiler errors when matching on tuples containing tuples #159

Closed snocl closed 6 years ago

snocl commented 6 years ago

I ran into some weird behavior when matching on tuples containing tuples.

Differing patterns considered conflicting:

const main = {
    match ((0, 1), 2)
    | (_, 0):
    | (_, 1):            // compile error: pattern matched by earlier case
    | _:
    ;;
}

Match with catch-all pattern considered nonexhaustive:

const main = {
    match ((0, 1), 2)    // compile error: nonexhaustive pattern set in match statement
    | ((0, 0), 0):
    | ((0, 0), 1):
    | _:
    ;;
}

In both cases, the order of the elements is significant; the following cases compile without error:

const main = {
    match (0, (1, 2))
    | (0, _):
    | (1, _):
    | _:
    ;;
}
const main = {
    match (0, (1, 2))
    | (0, (0, 0)):
    | (1, (0, 0)):
    | _:
    ;;
}

I tested this on the most recent version of master, ebbdfc2.

oridb commented 6 years ago

Acking this, will be looking into it in the next day or two.

oridb commented 6 years ago

Looks like there's a bug expanding wildcards over nested tuples. I think we're treating the inner tuple as having one element. A fix is pending.

As a temporary workaround, you should be able to expand out the wildcards:

 match ((0, 1), 2)
 | ...:
 | ((_, _), _): ...
 ;;