Skip to content
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 error - "Maximum call stack size exceeded" #2997

Closed
Arnavion opened this issue May 2, 2015 · 23 comments · Fixed by #3157
Closed

Compiler error - "Maximum call stack size exceeded" #2997

Arnavion opened this issue May 2, 2015 · 23 comments · Fixed by #3157
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue High Priority

Comments

@Arnavion
Copy link
Contributor

Arnavion commented May 2, 2015

(1.5 beta)

This is the most I can reduce it while still having it crash:

class Module {
    public members: Class[];
}

class Namespace {
    public members: (Class | Property)[];
}

class Class {
    public parent: Namespace;
}

class Property {
    public parent: Module | Class;
}

var t: Class | Property;
t.toString();

tsc foo.ts -t es5

G:\src\libjass\node_modules\typescript\bin\tsc.js:0
(function (exports, require, module, __filename, __dirname) { /*! ************
^
RangeError: Maximum call stack size exceeded
    at isRelatedTo (G:\src\libjass\node_modules\typescript\bin\tsc.js)
    at unionTypeRelatedToType (G:\src\libjass\node_modules\typescript\bin\tsc.js:11862:35)
    at isRelatedTo (G:\src\libjass\node_modules\typescript\bin\tsc.js:11787:42)
    at stringIndexTypesRelatedTo (G:\src\libjass\node_modules\typescript\bin\tsc.js:12195:35)
    at objectTypeRelatedTo (G:\src\libjass\node_modules\typescript\bin\tsc.js:11955:43)
    at isRelatedTo (G:\src\libjass\node_modules\typescript\bin\tsc.js:11811:34)
    at propertiesRelatedTo (G:\src\libjass\node_modules\typescript\bin\tsc.js:12042:43)
    at objectTypeRelatedTo (G:\src\libjass\node_modules\typescript\bin\tsc.js:11949:30)
    at isRelatedTo (G:\src\libjass\node_modules\typescript\bin\tsc.js:11811:34)
    at typeRelatedToUnionType (G:\src\libjass\node_modules\typescript\bin\tsc.js:11850:35)

Alternative call stack for the crash (same code):

G:\src\libjass\node_modules\typescript\bin\tsc.js:11929
                    sourceStack = [];
                    ^
RangeError: Maximum call stack size exceeded
    at objectTypeRelatedTo (G:\src\libjass\node_modules\typescript\bin\tsc.js:11929:21)
    at isRelatedTo (G:\src\libjass\node_modules\typescript\bin\tsc.js:11811:34)
    at checkTypeRelatedTo (G:\src\libjass\node_modules\typescript\bin\tsc.js:11723:26)
    at checkTypeSubtypeOf (G:\src\libjass\node_modules\typescript\bin\tsc.js:11703:20)
    at isTypeSubtypeOf (G:\src\libjass\node_modules\typescript\bin\tsc.js:11697:20)
    at isSubtypeOfAny (G:\src\libjass\node_modules\typescript\bin\tsc.js:11363:43)
    at removeSubtypes (G:\src\libjass\node_modules\typescript\bin\tsc.js:11373:21)
    at getUnionType (G:\src\libjass\node_modules\typescript\bin\tsc.js:11410:17)
    at getReducedTypeOfUnionType (G:\src\libjass\node_modules\typescript\bin\tsc.js:11426:36)
    at getApparentType (G:\src\libjass\node_modules\typescript\bin\tsc.js:10824:24)

The original code from which this test case is derived is here.

Edit: Reduced the case some more - removed the fullName getters and simplified the types of member.

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label May 2, 2015
@Arnavion
Copy link
Contributor Author

Arnavion commented May 4, 2015

If it helps, bisect says 1d8fb49 is the first bad commit (from #2778).

@mhegazy mhegazy added this to the TypeScript 1.5.2 milestone May 4, 2015
@JsonFreeman
Copy link
Contributor

I've identified the cause. The problem is that in a union type, we do a reduction step where we remove constituents that are subtypes of other constituents. To do that, we have to use the subtype relation, which calls checkTypeRelatedTo. But in the process of comparing union types, we have to perform subtype reduction again.

checkTypeRelatedTo has way of dealing with infinite recursion if all the infinite recursion happens inside a particular call to it. But in this case, the recursion is spread over multiple calls, and no single call is deep enough for us to catch the recursion.

The options are:

  1. Pull the recursion detection out of checkTypeRelatedTo so that all calls to it share the same comparison stack.
  2. Make checkTypeRelatedTo more like a class that has methods to do the comparison and locals to track the recursion.
  3. Make getUnionType (which does the subtype reduction) or getReducedTypeOfUnionType detect cycles and be resilient to reentry.

I am leaning towards option 3.

@Arnavion
Copy link
Contributor Author

Arnavion commented May 8, 2015

I confirmed that the original code also compiles with @JsonFreeman's fix. Thanks!

@evil-shrike
Copy link

I'm experiencing this error with a source file importing a d.ts file with module aliases (via "/// <reference"). Unfortunately it's hard for me to extract what exactly causes the compiler to crash.
So let me just ask is it supposed to fail on 1.5.0-beta compiler (I mean the latest published npm package)?

With TS 1.4.1 my code is being compiled just fine.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 9, 2015

@evil-shrike can you give the latest from branch release-1.5 a try. all you need is tsc.js and lib.d.ts from https://github.com/Microsoft/TypeScript/tree/release-1.5/bin

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jun 9, 2015
@evil-shrike
Copy link

@mhegazy I've tried the release-1.5 branch and got the error on it.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 9, 2015

can you share the code sample that causes the issue?

@mhegazy
Copy link
Contributor

mhegazy commented Jun 9, 2015

@evil-shrike, looks like this was never ported to branch release-1.5, my appologies. can you try the latest from master instead?

@mhegazy mhegazy reopened this Jun 9, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Jun 9, 2015

reopening to port to release-1.5

@JsonFreeman
Copy link
Contributor

#3071 was indeed ported to release-1.5, and that should be sufficient to fix the issue.

@JsonFreeman
Copy link
Contributor

@evil-shrike Can you try it on master and see if it works?

@evil-shrike
Copy link

I've tried master and still have "RangeError: Maximum call stack size exceeded":

D:\Work\R-n-D\XFW3_WebClient\Client\src>..\node_modules\.bin\tsc --module amd lib/core.events.ts
D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:0
(function (exports, require, module, __filename, __dirname) { /*! ************
^
RangeError: Maximum call stack size exceeded
    at createType (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js)
    at createObjectType (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:10187:24)
    at instantiateAnonymousType (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:12628:26)
    at instantiateType (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:12648:25)
    at instantiateList (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:12524:33)
    at instantiateType (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:12651:61)
    at getTypeOfInstantiatedSymbol (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:11323:30)
    at getTypeOfSymbol (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:11329:24)
    at inferFromProperties (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:13639:40)
    at inferFromTypes (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:13623:25)

D:\Work\R-n-D\XFW3_WebClient\Client\src>..\node_modules\.bin\tsc
Version 1.5.3
Syntax:   tsc [options] [file ...]

Unfortunately I can't share the reproducible code sample currently (it fails on whole project but stop failing as I start removing files). I'll try to create a reproducible sample...

@JsonFreeman
Copy link
Contributor

Thanks. Also, do you have a longer stack trace?

@evil-shrike
Copy link

This is all I have. Is there a way to tell compiler to output more details?

@JsonFreeman
Copy link
Contributor

I have a guess as to what's going on. I will create a branch for you to try.

@JsonFreeman
Copy link
Contributor

I've pushed a branch https://github.com/Microsoft/TypeScript/tree/deeplyNestedTypeArgumentInference. It is based on master, but I would do the same fix on release-1.5. Can you give it a try? In the meantime, I will try to come up with a sample for the problem I think you're having.

@JsonFreeman
Copy link
Contributor

Oh also, you need to build using "jake local", and grab the output from the folder built\local. If you want I can push a change to bin, so you can use that instead.

@evil-shrike
Copy link

I've built the branch and run but unfortunately it fails with the same error (I hope I didn't miss anything):

D:\Work\R-n-D\XFW3_WebClient\Client\src>node ..\node_modules\typescript\bin\tsc --module amd --sourceMap -t es5 lib/ui/Part.ts
D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:0
(function (exports, require, module, __filename, __dirname) { var ts;
^
RangeError: Maximum call stack size exceeded
    at instantiateSymbol (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js)
    at instantiateList (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:15475:33)
    at instantiateAnonymousType (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:15594:33)
    at instantiateType (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:15613:25)
    at instantiateList (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:15475:33)
    at instantiateType (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:15616:61)
    at getTypeOfInstantiatedSymbol (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:14147:30)
    at getTypeOfSymbol (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:14153:24)
    at inferFromProperties (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:16695:40)
    at inferFromTypes (D:\Work\R-n-D\XFW3_WebClient\Client\node_modules\typescript\bin\tsc.js:16679:25)

@JsonFreeman
Copy link
Contributor

In the typescript repo, you have to run node built\local\tsc.js

@JsonFreeman
Copy link
Contributor

Also, I think I have a sample, which is inspired by #3309:

function foo<T>() {
    var z = foo<typeof y>();
    var y: {
        y2: typeof z
    };
    return y;
}


function bar<T>() {
    var z = bar<typeof y>();
    var y: {
        y2: typeof z;
    }
    return y;
}

var a = foo<number>();
var b = bar<number>();

function test<T>(x: typeof a): void { }
test(b);

@JsonFreeman
Copy link
Contributor

I've opened #3451 for this.

@JsonFreeman
Copy link
Contributor

I have a pull request #3452 that addresses what I think your issue is. At least it fixes the same stack overflow you are getting, even if you are not getting it in the same way.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 9, 2015

closing this in favor of new issue #3452.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue High Priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants