Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler error matching variable with enum with all unit variants #5417

Open
xunilrj opened this issue Dec 21, 2023 · 1 comment
Open

Compiler error matching variable with enum with all unit variants #5417

xunilrj opened this issue Dec 21, 2023 · 1 comment

Comments

@xunilrj
Copy link
Contributor

xunilrj commented Dec 21, 2023

The following code:

enum OnlyUnits {
    A: ()
}

match some_value {
    OnlyUnits::A(x) => {}
}

generates a compiler error:

 error
            --> /home/xunilrj/github/sway/test/src/e2e_vm_tests/test_programs/should_pass/language/logging/src/main.sw:59:11
             |
          57 | 
          58 | 
          59 |     match F::A {
             |           ^^^^ Internal compiler error: Failed to get variant type from enum in `unsigned downcast`.
          Please file an issue on the repository and include the code that triggered this error.
          60 |         F::A(x) => {}
          61 |     }
             |
          ____

The problem seems to be a optimization that we do when an enum only has unit variants (its type is { u64 }, only the discriminant) and the fact that the match "tries" to access the variant data (the second element of the struct).

@ironcev
Copy link
Member

ironcev commented Jul 20, 2024

Actually, the compilation shouldn't reach the IR generation stage because of the type mismatch. The issue is that we don't check variant type in the pattern against the declared variant type, if the pattern contains variables (as x in the above example). See #6286.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants