Skip to content

Fixed single signature type parameter leak #58008

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

Merged

Conversation

Andarist
Copy link
Contributor

fixes #43961

cc @weswigham

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Mar 30, 2024
@@ -19867,6 +19867,25 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
}
if (type.objectFlags & ObjectFlags.SingleSignatureType) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be honest - this is very much "I don't know what I'm doing here". I "brute forced" this solution based on the changes from #57403 and the investigation I was doing regarding this issue from before this PR was created. I kinda knew where a fix potentially could be applied and that's it.

I can move this code to a helper function etc. I just didn't bother because I'm not sure yet how good this fix is.

I'm not sure if this should be somehow cached (instantiations are cached in this function). And the type parameter that is now mapped (P) is still not in clone.outerTypeParameters - I have no idea if that's a problem or not.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if this should be somehow cached

100%. Should definitely be cached. And you should be able to get that by adding the missing bits of this into instantiateAnonymousType, rather than here. It already does the clone.outerTypeParameters = singleSignatureType.outerTypeParameters; bit - what it's missing is the "apply mapper to signature inside the type" bit. Ish. That's supposed to be implicit in the result.mapper = mapper; line already there... you'd assume, but alas - it looks like that only actually does something for types with a .target member. That may be the quickest fix - just set the .target of the type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I pushed out changes based on the stuff that you have mentioned here. No extra work had to be done in instantiateAnonymousType though. That already works (it sets the mapper and the target)... it just was never used in this case.

The problem was that the computed id for this instantiation was the same as the id computed for the target in the branch that initializes target.instantiations. In other words, target was returned as that was grabbed from the target.instantiations and assigned as the existing result.

@Andarist Andarist requested a review from weswigham April 7, 2024 20:50
@weswigham weswigham merged commit e0755dc into microsoft:main Apr 19, 2024
25 checks passed
@Andarist Andarist deleted the fix-single-signature-type-parameter-leak branch July 1, 2024 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type parameter can leak out of a function instead of being bound to specific type
3 participants