Compiler: make is_a?(union)
work correctly for virtual types
#11176
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #10244
Now the snippet in that issue gives this:
which I believe is more correct than the previous behavior.
Please note that this change only applies to
is_a?
: as you can see,Union(BarB, BarC)
unifies to the parent type. In theory we could also make that change, but it's much more complex (a quick change to this already makes the compiler stop compiling itself, or the std.) But, I think checkingx.is_a?(X | Y)
is much more common than using union types outside ofis_a?
. So for now I'd like to defer this.Also, I need this change to keep working on the interpreter. The way I implemented multidispatch there, I end up generating code like
exp.is_a?(B | C)
and if that resolves to the parent type then it's not the same condition I'm looking for. I could do some hacks around that, or try to implement multidispatch in a different, but much harder, way, but I think it's better if we also fix these small edges in the language.It seems this change also fixes this bug: