Compile-time type hierarchy information in pattern switch
forax at univ-mlv.fr
Thu Apr 5 20:28:19 UTC 2018
----- Mail original -----
> De: "Brian Goetz" <brian.goetz at oracle.com>
> À: "Tagir Valeev" <amaembo at gmail.com>, "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> Envoyé: Jeudi 5 Avril 2018 21:44:30
> Objet: Re: Compile-time type hierarchy information in pattern switch
>> Is it too harsh to reject the whole class if the assumptions on class
>> hierarchy which were necessary to compile the switch statements used
>> in the class are not valid at runtime?
> That is one of the questions! And the other question is: is this too
> expensive to do this check at runtime, given that it will fail so
> If we can detect it cheaply enough, though, we can also repair the
> situation and fall back to linear testing of patterns. This seems
> better (we can execute the statement the user wrote) than failing. My
> real question is can I punt on trying to detect it, and still optimize
> the common cases down to O(1) dispatch....
the way to detect it is to use the DAG of the supertypes (lazily constructed*), from the last to the first case, the idea is to propagate the index of down to the super types, if during the propagation, you find a supertype which is also a case and with an index lower that the currently propagated, then it's a failure.
* you do not have to actually create the DAG, just be able to traverse it from the subtype to the supertypes.
More information about the amber-spec-experts