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

switch over enum not behaving properly #838

Closed
pierrec opened this issue Jul 8, 2023 · 6 comments
Closed

switch over enum not behaving properly #838

pierrec opened this issue Jul 8, 2023 · 6 comments
Labels
Fixed Needs Verification Fixed, but needs verification that it works Missing Feature Feature that is required for the language

Comments

@pierrec
Copy link
Contributor

pierrec commented Jul 8, 2023

Switches over enums should be exhaustive except if default is used.

The following should return an error when compiled as the switch over the enum is missing the RUNNING value.

enum State
{
    STOPPED,
    RUNNING
}

fn bool State.mayOpen(State state) 
{
    switch (state)
    {
        case STOPPED: return true;
    }
    return false;
}

The following should compile but currently returns an error about a missing return.

// https://c3-lang.org/functions/#methods
enum State
{
    STOPPED,
    RUNNING
}

fn bool State.mayOpen(State state) 
{
    switch (state)
    {
        case STOPPED: return true;
        case RUNNING: return false;
    }
}
@lerno
Copy link
Collaborator

lerno commented Jul 8, 2023

This should now be working, try it out!

@pierrec
Copy link
Contributor Author

pierrec commented Jul 9, 2023

In the first case, I am expecting an error as the switch is not exhaustive and there is no default case.
The second case now works, as well as this third one:

enum State
{
    STOPPED,
    RUNNING
}

fn bool State.mayOpen(State state)
{
    switch (state)
    {
        case STOPPED: return true;
        default: return false;
    }
}

@lerno
Copy link
Collaborator

lerno commented Jul 9, 2023

Ah, I see, I know what you want and you're right.

@lerno
Copy link
Collaborator

lerno commented Jul 9, 2023

Test it now.

@lerno
Copy link
Collaborator

lerno commented Jul 9, 2023

Funny story: in the switch tests I actually had test for (1), but those were commented out as I hadn't implemented the check when I wrote the tests.

@lerno lerno added Missing Feature Feature that is required for the language Fixed Needs Verification Fixed, but needs verification that it works labels Jul 9, 2023
@pierrec
Copy link
Contributor Author

pierrec commented Jul 9, 2023

All good!

@pierrec pierrec closed this as completed Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed Needs Verification Fixed, but needs verification that it works Missing Feature Feature that is required for the language
Projects
None yet
Development

No branches or pull requests

2 participants