A cast between two specializations of one generic is checked - #389
Merged
Merged
Conversation
`const b: Box<string> = new Box<number>(1)` compiled to a plain ts.Cast between the two class types, and reading `b.v` then read a number as a string and crashed. A cast between two specializations of the same generic class now needs one's type arguments to extend the other's - Box<Dog> as Box<Animal>, or back down as an assertion - which is TypeScript's rule for an assertion too; otherwise it is an error. extendsType's class branch compared only the first type argument and returned, so Pair<number, number> passed as Pair<number, string>. Every type argument has to extend now. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Sep 28, 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.
The cast became a plain
ts.Castbetween the two class types. Two specializations of one generic keep fields of different types in the same place, so reading through the wrong one crashed.Fix (
verifyCastCompatibility,MLIRGenCast.cpp): a cast between two specializations of the same generic class now needs one's type arguments to extend the other's. Otherwise it is an error:cast()is shared by assignments and explicit<T>xcasts. Allowing either direction therefore keeps an upcast (Box<Dog>asBox<Animal>) and the explicit downcast back (<Box<Dog>>up) working, which matches TypeScript's rule for a type assertion.Also fixed (
extendsType,MLIRTypeHelper.h): the class branch compared only the first type argument and returned, soPair<number, number>passed as aPair<number, string>. Now every type argument has to extend.Tests:
specialization-cast/box.tsandpair.ts: error tests matched on the message.00generic_specialization_cast.ts: an upcast, a downcast and a covariantPair, registered for compile, jit and the corpus. It is also clean under-mm=rc --verify-ownership.Results: the suite passes locally, 3019/3019, and the DefaultLib tests pass 156/156 in both jit and compile modes.
Not changed: casts between unrelated classes are still unchecked.
class A { v: number }toclass B { v: string }compiles and crashes the same way. Catching that needs a structural check, because TypeScript allows two unrelated classes of the same shape. That's left for its own change.🤖 Generated with Claude Code