-
Notifications
You must be signed in to change notification settings - Fork 13k
Fixed a ghost error popping up after completions request caching a wrong contextual function type #58378
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
Fixed a ghost error popping up after completions request caching a wrong contextual function type #58378
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1923,32 +1923,42 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
} | ||
|
||
function runWithoutResolvedSignatureCaching<T>(node: Node | undefined, fn: () => T): T { | ||
const cachedResolvedSignatures = []; | ||
const cachedTypes: (readonly [SymbolLinks, Type | undefined])[] = []; | ||
node = findAncestor(node, isCallLikeOrFunctionLikeExpression); | ||
if (node) { | ||
const cachedResolvedSignatures = []; | ||
const cachedTypes = []; | ||
while (node) { | ||
const nodeLinks = getNodeLinks(node); | ||
cachedResolvedSignatures.push([nodeLinks, nodeLinks.resolvedSignature] as const); | ||
nodeLinks.resolvedSignature = undefined; | ||
if (isFunctionExpressionOrArrowFunction(node)) { | ||
const symbolLinks = getSymbolLinks(getSymbolOfDeclaration(node)); | ||
const type = symbolLinks.type; | ||
cachedTypes.push([symbolLinks, type] as const); | ||
symbolLinks.type = undefined; | ||
} | ||
node = findAncestor(node.parent, isCallLikeOrFunctionLikeExpression); | ||
} | ||
const result = fn(); | ||
for (const [nodeLinks, resolvedSignature] of cachedResolvedSignatures) { | ||
nodeLinks.resolvedSignature = resolvedSignature; | ||
} | ||
for (const [symbolLinks, type] of cachedTypes) { | ||
symbolLinks.type = type; | ||
if (!node) { | ||
return fn(); | ||
} | ||
while (node) { | ||
const nodeLinks = getNodeLinks(node); | ||
cachedResolvedSignatures.push([nodeLinks, nodeLinks.resolvedSignature, nodeLinks.flags] as const); | ||
nodeLinks.resolvedSignature = undefined; | ||
nodeLinks.flags = NodeCheckFlags.None; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The key here is to reset So I could just remove that bit and re-add it later if needed. I looked through all I just tried with a complete flags reset here and it does seem to work... but 🤷♂️ I'd probably be in favor of resetting solely what's really-really needed. |
||
if (isFunctionExpressionOrArrowFunction(node)) { | ||
resetCachedSymbolTypes(node); | ||
for (const parameter of node.parameters) { | ||
resetCachedSymbolTypes(parameter); | ||
} | ||
} | ||
return result; | ||
node = findAncestor(node.parent, isCallLikeOrFunctionLikeExpression); | ||
} | ||
const result = fn(); | ||
for (const [nodeLinks, resolvedSignature, nodeCheckFlags] of cachedResolvedSignatures) { | ||
nodeLinks.resolvedSignature = resolvedSignature; | ||
nodeLinks.flags = nodeCheckFlags; | ||
} | ||
for (const [symbolLinks, type] of cachedTypes) { | ||
symbolLinks.type = type; | ||
} | ||
return result; | ||
|
||
function resetCachedSymbolTypes(declaration: Declaration) { | ||
const symbolLinks = getSymbolLinks(getSymbolOfDeclaration(declaration)); | ||
const type = symbolLinks.type; | ||
|
||
cachedTypes.push([symbolLinks, type] as const); | ||
symbolLinks.type = undefined; | ||
} | ||
return fn(); | ||
} | ||
|
||
function runWithInferenceBlockedFromSourceNode<T>(node: Node | undefined, fn: () => T): T { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/// <reference path="fourslash.ts" /> | ||
|
||
// @strict: true | ||
// @target: esnext | ||
// @lib: esnext | ||
|
||
//// type ObjectFromEntries<T> = T extends readonly [ | ||
//// infer Key extends string | number | symbol, | ||
//// infer Value, | ||
//// ][] | ||
//// ? { [key in Key]: Value } | ||
//// : never; | ||
//// | ||
//// type KeyValuePairs<T> = { | ||
//// [K in keyof T]: [K, T[K]]; | ||
//// }[keyof T]; | ||
//// | ||
//// declare function mapObjectEntries< | ||
//// const T extends object, | ||
//// const TMapped extends [string | number | symbol, unknown], | ||
//// >( | ||
//// obj: T, | ||
//// mapper: ([a, b]: KeyValuePairs<T>) => TMapped, | ||
//// ): ObjectFromEntries<TMapped[]>; | ||
//// | ||
//// mapObjectEntries({ a: 1, b: 2 }, ([x, y]) => ["a/*1*/", y]); | ||
|
||
verify.completions({ | ||
marker: "1", | ||
exact: ["a"], | ||
}); | ||
verify.getSemanticDiagnostics([]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This becomes more and more complicated and error-prone. I'm definitely not a fan of this. It builds on top of the existing solution... so at least it shouldn't be significantly worse now. It's just that perhaps there is a completely better way to do it than the existing mechanism. I think that alternatives could be explored separately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about using infra introduced in #57421 but that PR is still in a draft. The problem space is also a little bit different - in this PR here we don't need to revert some recent work. In here, we need to temporarily evict some preexisting caches and restore them - we don't need to speculate per se. I assume that both are interested in the same lists of caches though.