Skip to content

Commit

Permalink
Fixed bug that leads to a hang under certain circumstances involving …
Browse files Browse the repository at this point in the history
…deeply-nested higher-order functions. This addresses #8723.
  • Loading branch information
erictraut committed Aug 13, 2024
1 parent c9a4eb2 commit a2718f7
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ import {
isCodeFlowSupportedForReference,
wildcardImportReferenceKey,
} from './codeFlowTypes';
import { ConstraintSolution } from './constraintSolution';
import {
addConstraintsForExpectedType,
applySourceSolutionToConstraints,
Expand Down Expand Up @@ -11658,7 +11659,7 @@ export function createTypeEvaluator(
}

const liveTypeVarScopes = ParseTreeUtils.getTypeVarScopesForNode(errorNode);
specializedReturnType = adjustCallableReturnType(type, specializedReturnType, liveTypeVarScopes);
specializedReturnType = adjustCallableReturnType(errorNode, specializedReturnType, liveTypeVarScopes);

if (specializedInitSelfType) {
specializedInitSelfType = solveAndApplyConstraints(specializedInitSelfType, constraints);
Expand Down Expand Up @@ -11720,33 +11721,50 @@ export function createTypeEvaluator(
// or unions of callables. It's intended for a specific use case. We may
// need to make this more sophisticated in the future.
function adjustCallableReturnType(
callableType: FunctionType,
callNode: ExpressionNode,
returnType: Type,
liveTypeVarScopes: TypeVarScopeId[]
): Type {
if (!isFunction(returnType) || returnType.shared.name || !callableType.shared.typeVarScopeId) {
if (!isFunction(returnType)) {
return returnType;
}

// What type variables are referenced in the callable return type? Do not include any live type variables.
const newTypeParams = getTypeVarArgsRecursive(returnType).filter(
const typeParams = getTypeVarArgsRecursive(returnType).filter(
(t) => !liveTypeVarScopes.some((scopeId) => t.priv.scopeId === scopeId)
);

// If there are no unsolved type variables, we're done. If there are
// unsolved type variables, treat them as though they are rescoped
// to the callable.
if (newTypeParams.length === 0) {
// unsolved type variables, rescope them to the callable.
if (typeParams.length === 0) {
return returnType;
}

const newScopeId = newTypeParams[0].priv.freeTypeVar?.priv.scopeId ?? newTypeParams[0].priv.scopeId;
// Create a new scope ID based on the caller's position. This
// will guarantee uniqueness. If another caller uses the same
// call and arguments, the type vars will not conflict.
const newScopeId = ParseTreeUtils.getScopeIdForNode(callNode);
const solution = new ConstraintSolution();

return FunctionType.cloneWithNewTypeVarScopeId(
returnType,
newScopeId,
callableType.priv.constructorTypeVarScopeId,
newTypeParams
const newTypeParams = typeParams.map((typeVar) => {
const newTypeParam = TypeVarType.cloneForScopeId(
typeVar,
newScopeId,
typeVar.priv.scopeName,
TypeVarScopeType.Function
);
solution.setType(typeVar, newTypeParam);
return newTypeParam;
});

return applySolvedTypeVars(
FunctionType.cloneWithNewTypeVarScopeId(
returnType,
newScopeId,
/* constructorTypeVarScopeId */ undefined,
newTypeParams
),
solution
);
}

Expand Down

0 comments on commit a2718f7

Please sign in to comment.