Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Emit default case for exhaustive switches on JS backend #6312

Closed
lucaswerkmeister opened this issue Jun 12, 2016 · 4 comments
Closed

Emit default case for exhaustive switches on JS backend #6312

lucaswerkmeister opened this issue Jun 12, 2016 · 4 comments

Comments

@lucaswerkmeister
Copy link
Contributor

The JVM backend always emits this default case if a switch does not have an explicit else case:

else throw new .com.redhat.ceylon.compiler.java.language.EnumeratedTypeError("Supposedly exhaustive switch was not exhaustive");

I think it would be a good idea to do this on the JS backend as well. After all, the JS platform makes even less guarantees at runtime than the JVM (e. g. no guarantee that a String member actually contains a String value), and so a switch might not be exhaustive not only due to compiler bugs (e. g. #6311), but also simply because other parts of the program might not adhere to Ceylon’s typing rules (for example, TypeScript enums are just glorified integers, and there’s nothing to prevent someone from assigning 1337 to an enum with only the cases 0 and 1).

@jvasileff
Copy link
Contributor

This should be a bug IMO, given the language guarantee and the type of bugs that may result when it is not met.

@gavinking gavinking added this to the 1.2.3 milestone Jul 20, 2016
@chochos
Copy link
Contributor

chochos commented Jul 23, 2016

The JS backend already adds this check when dealing with dynamic types.

@chochos
Copy link
Contributor

chochos commented Jul 23, 2016

However, if you do this, with no dynamic blocks:

String|Integer foo() => "It's a string";

shared void run() {
    switch(x=foo())
    case (is Integer) {
      print("It's an int");
    }
    case (is String) {
      print("It's a string");
    }
    print("realmente fue ``className(foo())``");
}

And then override foo with a native file ``function foo() { return 3.14; }` then the switch won't cover that case and it's going to be hell to debug. So I guess this might be necessary after all.

@lucaswerkmeister
Copy link
Contributor Author

Thanks!

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

No branches or pull requests

4 participants