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

Fix contextual typing for post-spread tuple elements #52769

Merged
merged 7 commits into from
Feb 18, 2023
Merged

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Feb 15, 2023

Fixes #52684.
Fixes #52760.

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Feb 15, 2023
# Conflicts:
#	src/compiler/types.ts
@ahejlsberg
Copy link
Member Author

@typescript-bot test this
@typescript-bot user test this inline
@typescript-bot run dt
@typescript-bot perf test faster
@typescript-bot test top100

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 15, 2023

Heya @ahejlsberg, I've started to run the extended test suite on this PR at 54f49e9. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 15, 2023

Heya @ahejlsberg, I've started to run the diff-based top-repos suite on this PR at 54f49e9. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 15, 2023

Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at 54f49e9. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 15, 2023

Heya @ahejlsberg, I've started to run the diff-based user code test suite on this PR at 54f49e9. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 15, 2023

Heya @ahejlsberg, I've started to run the abridged perf test suite on this PR at 54f49e9. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

Heya @ahejlsberg, I've run the RWC suite on this PR - assuming you're on the TS core team, you can view the resulting diff here.


const d1 = foo([...staticPath1Level, randomID, 'folder', 'subfolder', 'another-subfolder', 'doc.pdf']);
const d2 = foo([...staticPath2Level, randomID, 'folder', 'subfolder', 'another-subfolder', 'doc.pdf']);
const d3 = foo([...staticPath3Level, randomID, 'folder', 'subfolder', 'another-subfolder', 'doc.pdf']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that this PR might also fix this issue but it doesn't. Perhaps it's something to work looking into here? The problem is similar but it's kinda a reverse one. This PR fixes contextual types for post-spread elements at the expression/runtime level and this other case is about post-spread elements at the type level.

The problem is that getTypeOfPropertyOfContextualType is still used here without any modifications as the primary source for the contextual type. The logic was only adjusted in the "else" branch here. The contextual type for this case is successfully retrieved through those lines and since this doesn't take the position in the array expression into account - it's just string.

Suggested change
const d3 = foo([...staticPath3Level, randomID, 'folder', 'subfolder', 'another-subfolder', 'doc.pdf']);
const d3 = foo([...staticPath3Level, randomID, 'folder', 'subfolder', 'another-subfolder', 'doc.pdf']);
// repro from #52760
const e1: [...string[], '!'] = ['a', '!'];

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we need to fix that too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When mentioning this case I was also thinking beyond union reductions, it would make sense for this to work too:

declare function test(
  ...args: [...((arg: number) => void)[], (arg: string) => void]
): void;

test(
  (a) => {
    a
    // ^? actual: any; expected: number
  },
  (b) => {
    b
    // ^? actual: any; expected: number
  },
  (c) => {
    c
    // ^? actual: any; expected: string
  },
);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would require us to reason about elements at a fixed offset from the end of a tuple. We currently only reason about elements at a fixed offset from the start. It adds some more complexity, but it is certainly possible to do.

|| mapType(
arrayContextualType,
t => getIteratedTypeOrElementType(IterationUse.Element, t, undefinedType, /*errorNode*/ undefined, /*checkAssignability*/ false),
index >= 0 && getTypeOfPropertyOfContextualType(arrayContextualType, "" + index as __String) ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that it's not exactly 100% related to this case - but I was touching this function just recently here. It would be cool if that improvement (if correct) could also go into 5.0 - both would make for a nice "batch" of improvements for contextual types for element expressions. 😉

@jakebailey
Copy link
Member

@typescript-bot test this
@typescript-bot user test this inline
@typescript-bot run dt
@typescript-bot perf test faster
@typescript-bot test top100

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 16, 2023

Heya @jakebailey, I've started to run the extended test suite on this PR at 2631e2f. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 16, 2023

Heya @jakebailey, I've started to run the diff-based top-repos suite on this PR at 2631e2f. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 16, 2023

Heya @jakebailey, I've started to run the diff-based user code test suite on this PR at 2631e2f. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 16, 2023

Heya @jakebailey, I've started to run the abridged perf test suite on this PR at 2631e2f. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 16, 2023

Heya @jakebailey, I've started to run the parallelized Definitely Typed test suite on this PR at 2631e2f. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the user test suite comparing main and refs/pull/52769/merge:

Everything looks good!

@typescript-bot
Copy link
Collaborator

Heya @jakebailey, I've run the RWC suite on this PR - assuming you're on the TS core team, you can view the resulting diff here.

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the top-repos suite comparing main and refs/pull/52769/merge:

Everything looks good!

@typescript-bot
Copy link
Collaborator

@jakebailey
The results of the perf run you requested are in!

Here they are:

Comparison Report - main..52769

Metric main 52769 Delta Best Worst p-value
Angular - node (v16.17.1, x64)
Memory used 359,068k (± 0.00%) 359,345k (± 0.01%) +277k (+ 0.08%) 359,321k 359,372k p=0.005 n=6
Parse Time 4.18s (± 0.93%) 4.16s (± 0.40%) ~ 4.13s 4.17s p=0.517 n=6
Bind Time 1.24s (± 0.00%) 1.24s (± 0.99%) ~ 1.22s 1.25s p=0.598 n=6
Check Time 9.51s (± 0.35%) 9.53s (± 0.38%) ~ 9.50s 9.58s p=0.464 n=6
Emit Time 8.11s (± 0.49%) 8.11s (± 0.92%) ~ 8.00s 8.19s p=0.936 n=6
Total Time 23.04s (± 0.39%) 23.03s (± 0.45%) ~ 22.89s 23.16s p=1.000 n=6
Compiler-Unions - node (v16.17.1, x64)
Memory used 191,709k (± 0.02%) 191,745k (± 0.04%) ~ 191,645k 191,834k p=0.470 n=6
Parse Time 1.81s (± 0.57%) 1.81s (± 0.67%) ~ 1.79s 1.82s p=1.000 n=6
Bind Time 0.85s (± 0.61%) 0.84s (± 0.00%) -0.01s (- 0.79%) 0.84s 0.84s p=0.025 n=6
Check Time 10.19s (± 0.68%) 10.20s (± 0.31%) ~ 10.16s 10.24s p=1.000 n=6
Emit Time 3.05s (± 0.48%) 3.07s (± 0.91%) ~ 3.04s 3.12s p=0.250 n=6
Total Time 15.89s (± 0.49%) 15.91s (± 0.18%) ~ 15.87s 15.95s p=0.936 n=6
Monaco - node (v16.17.1, x64)
Memory used 343,504k (± 0.01%) 343,617k (± 0.01%) +113k (+ 0.03%) 343,572k 343,657k p=0.005 n=6
Parse Time 3.12s (± 0.90%) 3.12s (± 0.98%) ~ 3.08s 3.16s p=0.809 n=6
Bind Time 1.11s (± 0.57%) 1.12s (± 0.75%) ~ 1.10s 1.12s p=0.226 n=6
Check Time 7.79s (± 0.34%) 7.80s (± 0.39%) ~ 7.77s 7.84s p=0.871 n=6
Emit Time 4.52s (± 0.86%) 4.51s (± 0.73%) ~ 4.48s 4.57s p=0.808 n=6
Total Time 16.53s (± 0.44%) 16.55s (± 0.35%) ~ 16.48s 16.61s p=0.520 n=6
TFS - node (v16.17.1, x64)
Memory used 299,638k (± 0.01%) 299,679k (± 0.00%) +41k (+ 0.01%) 299,665k 299,705k p=0.005 n=6
Parse Time 2.46s (± 0.60%) 2.44s (± 0.60%) -0.02s (- 0.95%) 2.42s 2.46s p=0.036 n=6
Bind Time 1.26s (± 0.60%) 1.25s (± 0.41%) ~ 1.25s 1.26s p=0.247 n=6
Check Time 7.24s (± 0.35%) 7.23s (± 0.54%) ~ 7.19s 7.29s p=0.747 n=6
Emit Time 4.25s (± 0.61%) 4.24s (± 0.50%) ~ 4.21s 4.27s p=0.808 n=6
Total Time 15.20s (± 0.25%) 15.16s (± 0.37%) ~ 15.06s 15.23s p=0.227 n=6
material-ui - node (v16.17.1, x64)
Memory used 476,013k (± 0.01%) 475,960k (± 0.01%) ~ 475,918k 475,990k p=0.093 n=6
Parse Time 3.70s (± 0.50%) 3.68s (± 0.53%) ~ 3.64s 3.69s p=0.183 n=6
Bind Time 1.02s (± 1.07%) 1.02s (± 0.00%) ~ 1.02s 1.02s p=0.598 n=6
Check Time 18.29s (± 0.97%) 18.18s (± 0.30%) ~ 18.13s 18.26s p=0.173 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 23.01s (± 0.81%) 22.88s (± 0.23%) ~ 22.83s 22.97s p=0.149 n=6
xstate - node (v16.17.1, x64)
Memory used 546,915k (± 0.02%) 546,855k (± 0.03%) ~ 546,746k 547,153k p=0.471 n=6
Parse Time 4.80s (± 0.57%) 4.79s (± 0.45%) ~ 4.76s 4.82s p=1.000 n=6
Bind Time 1.86s (± 0.59%) 1.84s (± 0.30%) -0.02s (- 0.81%) 1.84s 1.85s p=0.031 n=6
Check Time 3.09s (± 0.93%) 3.08s (± 0.66%) ~ 3.06s 3.11s p=0.746 n=6
Emit Time 0.09s (± 0.00%) 0.09s (± 0.00%) ~ 0.09s 0.09s p=1.000 n=6
Total Time 9.83s (± 0.58%) 9.81s (± 0.45%) ~ 9.75s 9.88s p=0.419 n=6
System
Machine Namets-ci-ubuntu
Platformlinux 5.4.0-135-generic
Architecturex64
Available Memory16 GB
Available Memory15 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v16.17.1, x64)
Scenarios
  • Angular - node (v16.17.1, x64)
  • Compiler-Unions - node (v16.17.1, x64)
  • Monaco - node (v16.17.1, x64)
  • TFS - node (v16.17.1, x64)
  • material-ui - node (v16.17.1, x64)
  • xstate - node (v16.17.1, x64)
Benchmark Name Iterations
Current 52769 6
Baseline main 6

Developer Information:

Download Benchmark

@ahejlsberg
Copy link
Member Author

Tests and performance all look good.

@@ -465,7 +465,7 @@ fn1([1, 'abc']); // [number, string]
fn1([1, 'abc', true]); // [string, boolean]
>fn1([1, 'abc', true]) : [string, boolean]
>fn1 : <T, U>(t: [...unknown[], T, U]) => [T, U]
>[1, 'abc', true] : [number, string, boolean]
>[1, 'abc', true] : [number, string, true]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it doesn't seem to have any observable effect here, but, why is the last thing here true while the first two are widened?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following inference and instantiation, the contextual type for the array literal is [...unknown[], string, boolean]. The contextual type for the true literal is the is computed from unknown | string | boolean which previously would reduce to just unknown. With this PR we don't perform subtype reduction, so boolean survives in the contextual union type and causes the true literal to stay true. But the change doesn't really matter since we've already inferred boolean for U.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
4 participants