-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compiler runs out of memory in classes with lots of static, generic functions #7097
Comments
@ahejlsberg do you have an idea of how to fix this? I don't know whether the fix is to make |
@sandersn We instantiate the (anonymous) static side of a class because it might reference one of the type parameters being mapped. For example: function foo<T>(x: T) {
class C {
static y: T = x;
}
return C;
}
let y = foo(5).y; // Type of y is number Now, as the example above illustrates, we really only need to instantiate anonymous types if they are nested within the class or function that introduced the type parameters being mapped (because only then could members of the anonymous types possibly reference those type parameters). We don't currently do that check and I think that's the problem you're running into. In your example there really is no need to instantiate Unfortunately, there is currently no easy way to check which type parameters are being mapped by a particular type mapper (which is why we don't currently do the check), but I guess we need to figure something out. |
I have a strawman PR at #7138. It's probably wrong (and currently the mapper extension is ugly too), but I think a bad solution is good for contrasting with a better solution. |
This repro is based on ListWrapper from Angular 2, a class that contains nothing but static functions. Most of them are generic.
When I test the upcoming
strictThis
flag,ListWrapper
causes the compiler to run out of memory. The code below is equivalent to making all thethis
arguments explicit asdit: typeof ListWrapper
. Nobody would write code like this today, but withstrictThis
, static functions all get an extra argument likethis: typeof ListWrapper
by default.The reason for the failure is that when inferring type arguments for a call, such as
ListWrapper.clone
, its signature is first retrieved:Which gives
<T>(dit: typeof ListWrapper, array: T[]): T[]
. Then the signature is instantiated with the inferred type parameters:(dit: typeof ListWrapper, array: number[]): number[]
.Unfortunately, this instantiation causes
typeof ListWrapper
to be instantiated as well since it's an anonymous type. This creates a new type whose target is the original but is otherwise identical. Now, checking that the arguments(ListWrapper, [1,2,3,4])
are applicable to this instantiated signature will check that the argumenttypeof ListWrapper
(un-instantiated) is applicable to the parametertypeof ListWrapper
(instantiated). This checks spirals out of control because the types are not identical, so all properties need to be checked. But when any generic method is checked, the same instantiated-type mismatch happens again and the process recurs until the compiler runs out of memory.The text was updated successfully, but these errors were encountered: