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

Control flow analysis for element access with variable constant-like index in for statements #60715

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27135,7 +27135,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
if (isElementAccessExpression(node) && isIdentifier(node.argumentExpression)) {
const symbol = getResolvedSymbol(node.argumentExpression);
if (isConstantVariable(symbol) || isParameterOrMutableLocalVariable(symbol) && !isSymbolAssigned(symbol)) {
if (isConstantVariable(symbol) || isParameterOrMutableLocalVariable(symbol) && !isSymbolAssigned(symbol) || isBlockScopedForStatementVariable(symbol) && isUsedInForStatementBody(symbol, node.argumentExpression) && !isSymbolAssignedInForStatementBody(symbol)) {
const key = getFlowCacheKey((node as AccessExpression).expression, declaredType, initialType, flowContainer);
return key && `${key}.@${getSymbolId(symbol)}`;
}
Expand Down Expand Up @@ -27192,7 +27192,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
if (isElementAccessExpression(source) && isElementAccessExpression(target) && isIdentifier(source.argumentExpression) && isIdentifier(target.argumentExpression)) {
const symbol = getResolvedSymbol(source.argumentExpression);
if (symbol === getResolvedSymbol(target.argumentExpression) && (isConstantVariable(symbol) || isParameterOrMutableLocalVariable(symbol) && !isSymbolAssigned(symbol))) {
if (symbol === getResolvedSymbol(target.argumentExpression) && (isConstantVariable(symbol) || isParameterOrMutableLocalVariable(symbol) && !isSymbolAssigned(symbol) || isBlockScopedForStatementVariable(symbol) && isUsedInForStatementBody(symbol, source.argumentExpression) && !isSymbolAssignedInForStatementBody(symbol))) {
return isMatchingReference(source.expression, target.expression);
}
}
Expand Down Expand Up @@ -29632,6 +29632,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return isSomeSymbolAssignedWorker(rootDeclaration.name);
}

function isSymbolAssignedInForStatementBody(symbol: Symbol) {
const forStatement = getRootDeclaration(symbol.valueDeclaration!).parent.parent;
Debug.assert(isForStatement(forStatement));
return !isPastLastAssignment(symbol, forStatement.statement);
}

function isUsedInForStatementBody(symbol: Symbol, location: Node) {
const forStatement = getRootDeclaration(symbol.valueDeclaration!).parent.parent;
Debug.assert(isForStatement(forStatement));
return location.pos >= forStatement.statement.pos && location.end <= forStatement.statement.end;
}
Comment on lines +29635 to +29645
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'd love to combine those 2 but I've failed so far to figure out an elegant function name for a function that would do both 😅


function isSomeSymbolAssignedWorker(node: BindingName): boolean {
if (node.kind === SyntaxKind.Identifier) {
return isSymbolAssigned(getSymbolOfDeclaration(node.parent as Declaration));
Expand Down Expand Up @@ -29742,6 +29754,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
);
}

function isBlockScopedForStatementVariable(symbol: Symbol) {
if (!(symbol.flags & SymbolFlags.BlockScopedVariable)) {
return false;
}
const declaration = symbol.valueDeclaration && getRootDeclaration(symbol.valueDeclaration);
return !!declaration && isVariableDeclaration(declaration) && declaration.parent.parent.kind === SyntaxKind.ForStatement;
}

function parameterInitializerContainsUndefined(declaration: ParameterDeclaration): boolean {
const links = getNodeLinks(declaration);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
controlFlowComputedPropertyNames2.ts(20,49): error TS2339: Property 'type' does not exist on type 'Type'.
controlFlowComputedPropertyNames2.ts(28,39): error TS2339: Property 'type' does not exist on type 'Type'.
controlFlowComputedPropertyNames2.ts(35,39): error TS2339: Property 'type' does not exist on type 'Type'.


==== controlFlowComputedPropertyNames2.ts (3 errors) ====
interface Type {
kind: number;
isIndexType(): this is IndexType;
}

interface IndexType extends Type {
kind: 1;
type: Type;
}

function test1(types: Type[]) {
for (let i = 0; i < types.length; i++) {
const t = types[i].isIndexType() ? types[i].type : types[i]; // ok
}
}

function test2(types: Type[]) {
for (let i = 0; i < types.length; i++) {
i++;
const t = types[i].isIndexType() ? types[i].type : types[i]; // error
~~~~
!!! error TS2339: Property 'type' does not exist on type 'Type'.
}
}

function test3(types: Type[]) {
for (
let i = 0;
i < types.length;
types[i].isIndexType() ? types[i].type : types[i], i++ // error
~~~~
!!! error TS2339: Property 'type' does not exist on type 'Type'.
) {}
}

function test4(types: Type[]) {
for (
let i = 0;
types[i].isIndexType() ? types[i].type : types[i], i < types.length; // error
~~~~
!!! error TS2339: Property 'type' does not exist on type 'Type'.
i++
) {}
}

141 changes: 141 additions & 0 deletions tests/baselines/reference/controlFlowComputedPropertyNames2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//// [tests/cases/conformance/controlFlow/controlFlowComputedPropertyNames2.ts] ////

=== controlFlowComputedPropertyNames2.ts ===
interface Type {
>Type : Symbol(Type, Decl(controlFlowComputedPropertyNames2.ts, 0, 0))

kind: number;
>kind : Symbol(Type.kind, Decl(controlFlowComputedPropertyNames2.ts, 0, 16))

isIndexType(): this is IndexType;
>isIndexType : Symbol(Type.isIndexType, Decl(controlFlowComputedPropertyNames2.ts, 1, 15))
>IndexType : Symbol(IndexType, Decl(controlFlowComputedPropertyNames2.ts, 3, 1))
}

interface IndexType extends Type {
>IndexType : Symbol(IndexType, Decl(controlFlowComputedPropertyNames2.ts, 3, 1))
>Type : Symbol(Type, Decl(controlFlowComputedPropertyNames2.ts, 0, 0))

kind: 1;
>kind : Symbol(IndexType.kind, Decl(controlFlowComputedPropertyNames2.ts, 5, 34))

type: Type;
>type : Symbol(IndexType.type, Decl(controlFlowComputedPropertyNames2.ts, 6, 10))
>Type : Symbol(Type, Decl(controlFlowComputedPropertyNames2.ts, 0, 0))
}

function test1(types: Type[]) {
>test1 : Symbol(test1, Decl(controlFlowComputedPropertyNames2.ts, 8, 1))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 10, 15))
>Type : Symbol(Type, Decl(controlFlowComputedPropertyNames2.ts, 0, 0))

for (let i = 0; i < types.length; i++) {
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 11, 10))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 11, 10))
>types.length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 10, 15))
>length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 11, 10))

const t = types[i].isIndexType() ? types[i].type : types[i]; // ok
>t : Symbol(t, Decl(controlFlowComputedPropertyNames2.ts, 12, 9))
>types[i].isIndexType : Symbol(Type.isIndexType, Decl(controlFlowComputedPropertyNames2.ts, 1, 15))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 10, 15))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 11, 10))
>isIndexType : Symbol(Type.isIndexType, Decl(controlFlowComputedPropertyNames2.ts, 1, 15))
>types[i].type : Symbol(IndexType.type, Decl(controlFlowComputedPropertyNames2.ts, 6, 10))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 10, 15))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 11, 10))
>type : Symbol(IndexType.type, Decl(controlFlowComputedPropertyNames2.ts, 6, 10))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 10, 15))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 11, 10))
}
}

function test2(types: Type[]) {
>test2 : Symbol(test2, Decl(controlFlowComputedPropertyNames2.ts, 14, 1))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 16, 15))
>Type : Symbol(Type, Decl(controlFlowComputedPropertyNames2.ts, 0, 0))

for (let i = 0; i < types.length; i++) {
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 17, 10))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 17, 10))
>types.length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 16, 15))
>length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 17, 10))

i++;
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 17, 10))

const t = types[i].isIndexType() ? types[i].type : types[i]; // error
>t : Symbol(t, Decl(controlFlowComputedPropertyNames2.ts, 19, 9))
>types[i].isIndexType : Symbol(Type.isIndexType, Decl(controlFlowComputedPropertyNames2.ts, 1, 15))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 16, 15))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 17, 10))
>isIndexType : Symbol(Type.isIndexType, Decl(controlFlowComputedPropertyNames2.ts, 1, 15))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 16, 15))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 17, 10))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 16, 15))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 17, 10))
}
}

function test3(types: Type[]) {
>test3 : Symbol(test3, Decl(controlFlowComputedPropertyNames2.ts, 21, 1))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 23, 15))
>Type : Symbol(Type, Decl(controlFlowComputedPropertyNames2.ts, 0, 0))

for (
let i = 0;
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 25, 7))

i < types.length;
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 25, 7))
>types.length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 23, 15))
>length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))

types[i].isIndexType() ? types[i].type : types[i], i++ // error
>types[i].isIndexType : Symbol(Type.isIndexType, Decl(controlFlowComputedPropertyNames2.ts, 1, 15))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 23, 15))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 25, 7))
>isIndexType : Symbol(Type.isIndexType, Decl(controlFlowComputedPropertyNames2.ts, 1, 15))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 23, 15))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 25, 7))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 23, 15))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 25, 7))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 25, 7))

) {}
}

function test4(types: Type[]) {
>test4 : Symbol(test4, Decl(controlFlowComputedPropertyNames2.ts, 29, 1))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 31, 15))
>Type : Symbol(Type, Decl(controlFlowComputedPropertyNames2.ts, 0, 0))

for (
let i = 0;
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 33, 7))

types[i].isIndexType() ? types[i].type : types[i], i < types.length; // error
>types[i].isIndexType : Symbol(Type.isIndexType, Decl(controlFlowComputedPropertyNames2.ts, 1, 15))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 31, 15))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 33, 7))
>isIndexType : Symbol(Type.isIndexType, Decl(controlFlowComputedPropertyNames2.ts, 1, 15))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 31, 15))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 33, 7))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 31, 15))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 33, 7))
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 33, 7))
>types.length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
>types : Symbol(types, Decl(controlFlowComputedPropertyNames2.ts, 31, 15))
>length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))

i++
>i : Symbol(i, Decl(controlFlowComputedPropertyNames2.ts, 33, 7))

) {}
}

Loading
Loading