Repository navigation
new of a generic class takes its type arguments from the receiver - #382
Merged
Merged
Conversation
`const m: Map<string, number[]> = new Map()` crashed at run time: Map is
declared `Map<K = any, V = any>`, so `new Map()` built Map<any, any>, and the
cast to the declared type reinterpreted it as a different layout. A generic
without defaults was a compile error instead ("Generic class 'Box' is missing
type arguments"). Found in the BrowserLib sources attached to #231
(EventTarget.ts, `listeners: Map<string, EventListenerCallback[]> = new Map()`).
A `new` without type arguments now takes the receiver's specialization (the
declared variable, field or assignment target) when that is a specialization
of the same generic class. Explicit type arguments are unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Found in
EventTarget.tsof the BrowserLib sources attached to #231. That is thecan't find type by name: EventListenerCallbackitem listed there: the error was a symptom of this bug.TypeScript takes the type arguments of
new C()from the type the value is given to. tslang did not:Mapisclass Map<K = any, V = any>, sonew Map()becameMap<any, any>. The initializer then cast it toMap<string, number[]>, a different specialization with a different layout. The firstsetcrashed at run time. This happens on older builds too.Fix
In
mlirGen(NewExpression), anewwritten without type arguments uses the receiver's type when that type is a specialization of the same generic class. The comparison usesClassInfo::originClassType. It applies when the class named is either the generic itself or its specialization by the defaults.The receiver is the
receiverTypethat is already passed down for:Explicit type arguments are unchanged, and so is a receiver of any other type.
Tests
New
00new_generic_from_receiver.ts(compile, jit, and the rc/none corpus). It covers:The old build fails it with "missing type arguments".
Full release suite: 2993/2993 passed.
TypeScriptCompilerDefaultLib tests, release jit and compile: 156/156 each.
BrowserLib
EventTarget.tsno longer reportsEventListenerCallback. What is left there is the existing rule thatpushon aconstlocal array is an error, plus the missingsplice.Not in this PR
A cast between two different specializations of one generic class, such as
Map<any, any>toMap<string, number[]>, is still accepted silently. That is what turned this into a run-time crash. It should be a compile error.🤖 Generated with Claude Code