Symmetric common_descendent for two generic class types (#10831)#16999
Merged
straight-shoota merged 2 commits intoJun 3, 2026
Merged
Conversation
…ng#10831) `x.is_a?(B) && x.is_a?(C)` returns `false` for a runtime value that is actually a `C` when `class C(T) < B(T)`. After the first filter, `x` is narrowed to `B(T)`. The chained `is_a?(C)` then asks `common_descendent(B, C)`, which walked the relationship in one direction only (`common_descendent_instance_and_generic(B, C)`) and couldn't reach `C` from `B`. The intersection collapsed and the filter evaluated to `false`. Try both directions so the relationship between generic class types flows whichever way the chain happens to be written.
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
straight-shoota
approved these changes
Jun 2, 2026
ysbaddaden
approved these changes
Jun 2, 2026
straight-shoota
pushed a commit
that referenced
this pull request
Jun 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 #10831.
The bug
After the first
is_a?(B)narrowsxtoB(T), the chainedis_a?(C)is supposed to narrow further toC(T)— every runtimeCis also aB. Instead it evaluates tofalseeven though the runtime value really is aC.Root cause
Type::common_descendent(GenericClassType, GenericClassType)only walked the relationship in one direction:common_descendent_instance_and_generic(B, C)asks "is there a path from B's parents down to C?" — and there isn't (B is the parent, C is the child). The intersection collapses, the filter narrows to nothing, andis_a?(C)returns false.Fix
Try both directions, so the relationship between two generic class types flows whichever way the chain happens to be written:
The symmetric form mirrors the pattern already used for
VirtualType, VirtualType(line 170) and for theclass C(T) < B(T)family of relationships specifically.Tests
spec/compiler/semantic/is_a_spec.cr—run(...).to_bregression foris_a?(T)can break further type filtering ifTis an uninstantiated generic #10831. Verified to evaluate tofalseon master andtruehere.Note
This was originally part of #16982 along with #11134's virtual metaclass dispatch fix. Per review feedback, they're being split into separate PRs — they're related symptoms but fix different code paths and warrant independent review.