Skip to content

Commit

Permalink
Changed binder to use Set rather than Map to track reference expressi…
Browse files Browse the repository at this point in the history
…ons.
  • Loading branch information
msfterictraut committed Aug 31, 2021
1 parent dabafb2 commit 6234fcd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
10 changes: 5 additions & 5 deletions packages/pyright-internal/src/analyzer/analyzerNodeInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ interface AnalyzerNodeInfo {
// Info about the source file, used only on module nodes.
fileInfo?: AnalyzerFileInfo;

// Map of expressions used within an execution scope (module,
// Set of expressions used within an execution scope (module,
// function or lambda) that requires code flow analysis.
codeFlowExpressions?: Map<string, string>;
codeFlowExpressions?: Set<string>;

// List of __all__ symbols in the module.
dunderAllInfo?: DunderAllInfo | undefined;
Expand Down Expand Up @@ -139,14 +139,14 @@ export function setFileInfo(node: ModuleNode, fileInfo: AnalyzerFileInfo) {
analyzerNode.fileInfo = fileInfo;
}

export function getCodeFlowExpressions(node: ExecutionScopeNode): Map<string, string> | undefined {
export function getCodeFlowExpressions(node: ExecutionScopeNode): Set<string> | undefined {
const analyzerNode = node as AnalyzerNodeInfo;
return analyzerNode.codeFlowExpressions;
}

export function setCodeFlowExpressions(node: ExecutionScopeNode, map: Map<string, string>) {
export function setCodeFlowExpressions(node: ExecutionScopeNode, expressions: Set<string>) {
const analyzerNode = node as AnalyzerNodeInfo;
analyzerNode.codeFlowExpressions = map;
analyzerNode.codeFlowExpressions = expressions;
}

export function getDunderAllInfo(node: ModuleNode): DunderAllInfo | undefined {
Expand Down
30 changes: 15 additions & 15 deletions packages/pyright-internal/src/analyzer/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ interface MemberAccessInfo {

interface DeferredBindingTask {
scope: Scope;
codeFlowExpressionMap: Map<string, string>;
codeFlowExpressions: Set<string>;
callback: () => void;
}

Expand Down Expand Up @@ -172,9 +172,9 @@ export class Binder extends ParseTreeWalker {
// Flow nodes used for return statements.
private _currentReturnTarget: FlowLabel | undefined;

// Map of symbols within the current execution scope
// Set of expressions within the current execution scope
// and require code flow analysis to resolve.
private _currentExecutionScopeReferenceMap: Map<string, string> | undefined;
private _currentScopeCodeFlowExpressions: Set<string> | undefined;

// Aliases of "typing" and "typing_extensions".
private _typingImportAliases: string[] = [];
Expand Down Expand Up @@ -239,7 +239,7 @@ export class Binder extends ParseTreeWalker {

this._walkStatementsAndReportUnreachable(node.statements);

AnalyzerNodeInfo.setCodeFlowExpressions(node, this._currentExecutionScopeReferenceMap!);
AnalyzerNodeInfo.setCodeFlowExpressions(node, this._currentScopeCodeFlowExpressions!);

// Associate the code flow node at the end of the module with the module.
AnalyzerNodeInfo.setAfterFlowNode(node, this._currentFlowNode);
Expand Down Expand Up @@ -516,7 +516,7 @@ export class Binder extends ParseTreeWalker {
AnalyzerNodeInfo.setAfterFlowNode(node, returnFlowNode);
});

AnalyzerNodeInfo.setCodeFlowExpressions(node, this._currentExecutionScopeReferenceMap!);
AnalyzerNodeInfo.setCodeFlowExpressions(node, this._currentScopeCodeFlowExpressions!);
});

this._createAssignmentTargetFlowNodes(node.name, /* walkTargets */ false, /* unbound */ false);
Expand Down Expand Up @@ -569,7 +569,7 @@ export class Binder extends ParseTreeWalker {
// Walk the expression that make up the lambda body.
this.walk(node.expression);

AnalyzerNodeInfo.setCodeFlowExpressions(node, this._currentExecutionScopeReferenceMap!);
AnalyzerNodeInfo.setCodeFlowExpressions(node, this._currentScopeCodeFlowExpressions!);
});
});

Expand Down Expand Up @@ -940,7 +940,7 @@ export class Binder extends ParseTreeWalker {
if (this._isNarrowingExpression(node.valueExpression, expressionList)) {
expressionList.forEach((expr) => {
const referenceKey = createKeyForReference(expr);
this._currentExecutionScopeReferenceMap!.set(referenceKey, referenceKey);
this._currentScopeCodeFlowExpressions!.add(referenceKey);
});
}

Expand Down Expand Up @@ -1890,7 +1890,7 @@ export class Binder extends ParseTreeWalker {
if (isSubjectNarrowable) {
expressionList.forEach((expr) => {
const referenceKey = createKeyForReference(expr);
this._currentExecutionScopeReferenceMap!.set(referenceKey, referenceKey);
this._currentScopeCodeFlowExpressions!.add(referenceKey);
});
}

Expand Down Expand Up @@ -2459,7 +2459,7 @@ export class Binder extends ParseTreeWalker {

expressionList.forEach((expr) => {
const referenceKey = createKeyForReference(expr);
this._currentExecutionScopeReferenceMap!.set(referenceKey, referenceKey);
this._currentScopeCodeFlowExpressions!.add(referenceKey);
});

// Select the first name expression.
Expand Down Expand Up @@ -2819,7 +2819,7 @@ export class Binder extends ParseTreeWalker {
};

const referenceKey = createKeyForReference(node);
this._currentExecutionScopeReferenceMap!.set(referenceKey, referenceKey);
this._currentScopeCodeFlowExpressions!.add(referenceKey);

if (unbound) {
flowNode.flags |= FlowFlags.Unbind;
Expand Down Expand Up @@ -3045,15 +3045,15 @@ export class Binder extends ParseTreeWalker {
// If this scope is an execution scope, allocate a new reference map.
const isExecutionScope =
scopeType === ScopeType.Builtin || scopeType === ScopeType.Module || scopeType === ScopeType.Function;
const prevReferenceMap = this._currentExecutionScopeReferenceMap;
const prevExpressions = this._currentScopeCodeFlowExpressions;

if (isExecutionScope) {
this._currentExecutionScopeReferenceMap = new Map<string, string>();
this._currentScopeCodeFlowExpressions = new Set<string>();
}

callback();

this._currentExecutionScopeReferenceMap = prevReferenceMap;
this._currentScopeCodeFlowExpressions = prevExpressions;
this._currentScope = prevScope;

return newScope;
Expand Down Expand Up @@ -3645,7 +3645,7 @@ export class Binder extends ParseTreeWalker {

this._deferredBindingTasks.push({
scope: this._currentScope,
codeFlowExpressionMap: this._currentExecutionScopeReferenceMap!,
codeFlowExpressions: this._currentScopeCodeFlowExpressions!,
callback,
});
}
Expand All @@ -3656,7 +3656,7 @@ export class Binder extends ParseTreeWalker {

// Reset the state
this._currentScope = nextItem.scope;
this._currentExecutionScopeReferenceMap = nextItem.codeFlowExpressionMap;
this._currentScopeCodeFlowExpressions = nextItem.codeFlowExpressions;

nextItem.callback();
}
Expand Down
17 changes: 9 additions & 8 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17172,7 +17172,7 @@ export function createTypeEvaluator(
const executionScope = ParseTreeUtils.getExecutionScopeNode(reference);
const codeFlowExpressions = AnalyzerNodeInfo.getCodeFlowExpressions(executionScope);

if (!codeFlowExpressions || !codeFlowExpressions!.has(referenceKey)) {
if (!codeFlowExpressions || !codeFlowExpressions.has(referenceKey)) {
return { type: undefined, usedOuterScopeAlias: false, isIncomplete: false };
}

Expand Down Expand Up @@ -17248,14 +17248,15 @@ export function createTypeEvaluator(
initialType: Type | undefined,
isInitialTypeIncomplete: boolean
): FlowNodeTypeResult {
const referenceKey =
reference !== undefined && targetSymbolId !== undefined
? createKeyForReference(reference) + `.${targetSymbolId.toString()}`
const referenceKey = reference !== undefined ? createKeyForReference(reference) : undefined;
const referenceKeyWithSymbolId =
referenceKey !== undefined && targetSymbolId !== undefined
? referenceKey + `.${targetSymbolId.toString()}`
: '.';
let flowNodeTypeCache = flowNodeTypeCacheSet.get(referenceKey);
let flowNodeTypeCache = flowNodeTypeCacheSet.get(referenceKeyWithSymbolId);
if (!flowNodeTypeCache) {
flowNodeTypeCache = new Map<number, CachedType | undefined>();
flowNodeTypeCacheSet.set(referenceKey, flowNodeTypeCache);
flowNodeTypeCacheSet.set(referenceKeyWithSymbolId, flowNodeTypeCache);
}

// Caches the type of the flow node in our local cache, keyed by the flow node ID.
Expand Down Expand Up @@ -17575,7 +17576,7 @@ export function createTypeEvaluator(
}

if (curFlowNode.flags & FlowFlags.LoopLabel) {
const labelNode = curFlowNode as FlowLabel;
const loopNode = curFlowNode as FlowLabel;

let firstWasIncomplete = false;
let isFirstTimeInLoop = false;
Expand All @@ -17594,7 +17595,7 @@ export function createTypeEvaluator(
);
}

labelNode.antecedents.forEach((antecedent, index) => {
loopNode.antecedents.forEach((antecedent, index) => {
// Have we already been here? If so, there will be an entry
// for this index, and we can use the type that was already
// computed.
Expand Down

0 comments on commit 6234fcd

Please sign in to comment.