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

When was constraintDepth added? #33541

Closed
AnyhowStep opened this issue Sep 22, 2019 · 6 comments
Closed

When was constraintDepth added? #33541

AnyhowStep opened this issue Sep 22, 2019 · 6 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@AnyhowStep
Copy link
Contributor

I was testing some of my code in TS 3.6 and TS 3.7

Code that worked fine in TS 3.5.1 now got the Type_instantiation_is_excessively_deep_and_possibly_infinite error instead.

I decided to do a little investigation and noticed that there was this thing called constraintDepth with a limit of 50. This was what was triggering the error.

I tried to disable the check for this constraintDepth and, predictably, got the error,

                return mapper(type);
                       ^
RangeError: Maximum call stack size exceeded
    at instantiateTypeWorker (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42582:24)
    at instantiateType (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42575:26)
    at /home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42274:42
    at Object.map (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:507:29)
    at getConditionalTypeInstantiation (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42537:40)
    at instantiateTypeWorker (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42620:24)
    at instantiateType (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42575:26)
    at /home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42274:42
    at Object.map (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:507:29)
    at getConditionalTypeInstantiation (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42537:40)
    at instantiateTypeWorker (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42620:24)
    at instantiateType (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42575:26)
    at /home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42274:42
    at Object.map (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:507:29)
    at getConditionalTypeInstantiation (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42537:40)
    at instantiateTypeWorker (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42620:24)

However, if the code I wrote really was the problem, it shouldn't have run fine in 3.5.1. It should have also exceeded the max call stack size.


I don't have a minimal repro but I noticed something weird.
The following exceeds the constraint depth on 3.6 and 3.7,
image

However, the following does not,
image

Trying to nest function calls directly triggers the constraint depth error. But assigning it to a temporary variable is OK.


I'm a bit at a loss here and out of my depth (haha, depth).

  • When was constraintDepth added?
  • Why was it added?
  • What problem was it trying to solve?

Code that worked fine in TS 3.5.1 now hits this constraintDepth error.
Removing the limit causes the max call stack size to be exceeded.

So, it looks to me like there's a bug with the code surrounding the constraintDepth implementation. Hopefully, the answers to my questions can help me refactor my code to not hit the depth limit. Or maybe it'll expose a bug that needs fixing in the TS code base.

AnyhowStep added a commit to AnyhowStep/tsql that referenced this issue Sep 22, 2019
@AnyhowStep
Copy link
Contributor Author

The file with the problem may be found here,
AnyhowStep/tsql@c413c11#diff-dfb1d0c2f356726e8bd4741b3ba718daR12-R29

  1. Clone https://github.com/AnyhowStep/tsql
  2. cd tsql
  3. Checkout c413c1152ae7b65eb2e2a18ee985d6dbb8b31295
  4. npm install
  5. npm run test ./test/run-time/input/expr-library/double/acos/double.ts

I'd like to again stress that this runs fine on TS 3.5.1 =(

@jack-williams
Copy link
Collaborator

jack-williams commented Sep 22, 2019

#26558

44ada08

@AnyhowStep
Copy link
Contributor Author

AnyhowStep commented Sep 22, 2019

Also, further proof the functions in question are fine on TS 3.5.1,

AnyhowStep/tsql@f9b73b2#diff-39b2092b0dd378cba12e94f7af61aba7R11-R573
In the above linked file, I've nested acos() and coalesce() calls 100+ times (500+ lines).

The emit of that giant file is here (only 30+ lines),
AnyhowStep/tsql@f9b73b2#diff-925d183e2fe29d7d151e0add12f6e4eeR2-R36

That file and 500+ others only take 27s to compile with TS 3.5.1,
image

A screenshot of the file with 100+ nested function calls,
image

@AnyhowStep
Copy link
Contributor Author

Thanks, @jack-williams , I'll start looking from there.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Sep 23, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

@AnyhowStep
Copy link
Contributor Author

AnyhowStep commented Dec 15, 2019

Instead of trying to figure out which commit broke my TypeOfCoalesce<> recursive type, I decided to just convert it into a trampoline.

The PR on my project,
AnyhowStep/tsql#65

The file that has the new TypeOfColaesce<> type,
https://github.com/AnyhowStep/tsql/pull/65/files#diff-0214b2fefabaf1617e690660683db775R226-R237

If anyone else is running into max depth errors with recursive types, trampolines may be a viable solution.

Here's another example of trampolines being used,
https://github.com/AnyhowStep/tsql/blob/master/src/tuple-util/reverse.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants