Compile-time type hierarchy information in pattern switch
brian.goetz at oracle.com
Wed Apr 4 17:07:17 UTC 2018
The intended implementation strategy is to lower complex switches to
densely-numbered `int` switches, and then invoke a classifier function
that takes a target and returns the int corresponding to the lowered
case number. The classifier function will be an `invokedynamic`, whose
static bootstrap will contain a summary of the patterns. (We've already
done this for switches on strings, enums, longs, non-dense ints, etc.)
To deliver an early error, that means that (a) the compiler must encode
through the static argument list all the assumptions it needs verified
at runtime (e.g., `String <: Object`), and (b) at linkage time (the
first time the switch is executed), those have to be tested.
Doing so is plenty easy, but there's a startup cost, which could be as
bad as _O(n^2)_, if I have to validate that no two case labels are
ordered inconsistently with subtyping.
A possible mitigation is to do the check as a system assertion, which
only gets run if we are run with `-esa`; we then might still have some
static code bloat (depending on how we encode the assumptions), but at
least skip the dynamic check most of the time.
On 4/4/2018 1:01 PM, Mark Raynsford wrote:
> I'm still giving thought to everything you've written, but I am
> wondering: How feasible is it to get the above to fail early with an
> informative exception/Error?
More information about the amber-spec-observers