A field of a union of same-layout object types is read from the base type - #384
Merged
Merged
Conversation
…type
`s.kind` / `s.r` on `s: { kind: "circle", r: number } | { kind: "square", r:
number }` failed verification: "'ts.PropertyRef' op operand #0 must be ...
tuple reference type ... but got '!ts.ref<!ts.union<...>>'". Such a union
needs no tag and is stored as its base type, and property access switched to
that type (actualType = baseType), but the value it accessed stayed the union,
and MLIRPropertyAccessCodeLogic still held the original expression: the tuple
accessor read the field through the union's reference.
The value is now cast to the base type, and the access logic is given it
(MLIRPropertyAccessCodeLogic::setExpression). Found in the #231 review.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ASDAlexander77
force-pushed
the
union-of-object-types-property-access
branch
from
September 28, 2026 08:33
4c2bf05 to
1532cb2
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.
Reading a field through a union of object types that share one layout failed verification:
It also broke
s.kind === "circle"ands.r !== nullon such a union. Unions of interfaces were not affected. Found while reviewing #231.Cause
A union whose members share one layout needs no tag (
isUnionTypeNeedsTagreturns false) and is stored as its base type.mlirGenPropertyAccessExpressionBaseLogicswitched to that type (actualType = baseType), but two things still referred to the union:MLIRPropertyAccessCodeLogicstill held the original expression, which was theLoadof the union variable.The tuple accessor took the reference behind that load (
getExprLoadRefValue) and built aPropertyRefonref<union>.Fix
When the union collapses to its base type:
MLIRPropertyAccessCodeLogic::setExpression.The field is then extracted from a value of the base type.
Tests
00union_object_types_access.ts(compile, jit, and the rc/none corpus). It covers a discriminant test, a field read, and a!== nullcheck on a nullable field of such a union.Not in this PR
With members of different layouts,
if (s.kind === "circle") return …; return s.w;still fails. That needs the early-exit narrowing in #383. Narrowing inside theifalready works.🤖 Generated with Claude Code