A cast between unrelated classes needs their fields to fit - #395
Merged
Merged
Conversation
ASDAlexander77
force-pushed
the
unrelated-class-cast-check
branch
2 times, most recently
from
September 28, 2026 16:37
3b56f08 to
1aff748
Compare
`const b: B = new A(1)` with `A { v: number }` and `B { v: string }`
compiled to a plain ts.Cast, and reading `b.v` read a number as a string
and crashed. Two classes where neither extends the other are now
compatible as in TypeScript, by their fields: one side's fields, inherited
ones included, must all be found in the other with types that extend them
(either direction, as for an assertion). A type guard's narrowing to an
unrelated class (`isB(a)`) is checked at run time and keeps its cast.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ASDAlexander77
force-pushed
the
unrelated-class-cast-check
branch
from
September 28, 2026 17:01
1aff748 to
b029e68
Compare
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.
A cast between two classes where neither extends the other became a plain
ts.Cast. #389 covers two specializations of one generic; this covers unrelated classes.Fix (
verifyUnrelatedClassCast,MLIRGenCast.cpp): two unrelated classes are compatible as in TypeScript, by their members. One side's data fields, inherited ones included, must all be found in the other side with types that extend them. Either direction is enough, as for a type assertion, becausecast()serves both assignments and<T>x. Otherwise it's an error:The check covers fields only, skipping internal ones such as
.vtbland the storage a derived class embeds for its base. A field read at the wrong type is what crashed.Not affected:
isB(a)on anA): the value is checked at run time, and TypeScript narrows it toA & B.addSafeCastStatementkeeps its plain cast for that case;00type_guard_function.tsneeds it.Tests:
class-cast/unrelated.ts: an error test matched on the message.00class_cast_structural.ts: a same-shape unrelated class and a type guard between classes without fields. Registered for compile, jit and the corpus.Results:
Limitation: a cast TypeScript allows between classes whose layouts differ, such as a class to one with a subset of its fields in another order, still compiles to a plain cast of the storage.
🤖 Generated with Claude Code