Skip to content

fix: Disallow type recursion during type declaration #2331

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
merged 10 commits into from
Jun 23, 2022
Merged
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
1 change: 1 addition & 0 deletions src/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
"Variable '{0}' used before its declaration.": 2448,
"Cannot redeclare block-scoped variable '{0}'" : 2451,
"The type argument for type parameter '{0}' cannot be inferred from the usage. Consider specifying the type arguments explicitly.": 2453,
"Type alias '{0}' circularly references itself.": 2456,
"Type '{0}' has no property '{1}'.": 2460,
"The '{0}' operator cannot be applied to type '{1}'.": 2469,
"In 'const' enum declarations member initializer must be constant expression.": 2474,
Expand Down
49 changes: 49 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3478,6 +3478,40 @@ export class Parser extends DiagnosticEmitter {
return null;
}

private getRecursiveDepthForTypeDeclaration(
identifierName: string,
type: TypeNode,
depth: i32 = 0
): i32 {
switch (type.kind) {
case NodeKind.NAMEDTYPE: {
let typeArguments = (<NamedTypeNode>type).typeArguments;
if (typeArguments) {
for (let i = 0, k = typeArguments.length; i < k; i++) {
let res = this.getRecursiveDepthForTypeDeclaration(identifierName, typeArguments[i], depth + 1);
if (res != -1) return res;
}
}
if ((<NamedTypeNode>type).name.identifier.text == identifierName) {
return depth;
}
break;
}
case NodeKind.FUNCTIONTYPE: {
let fnType = <FunctionTypeNode>type;
let res = this.getRecursiveDepthForTypeDeclaration(identifierName, fnType.returnType, depth + 1);
if (res != -1) return res;
let params = fnType.parameters;
for (let i = 0, k = params.length; i < k; i++) {
res = this.getRecursiveDepthForTypeDeclaration(identifierName, params[i].type, depth + 1);
if (res != -1) return res;
}
break;
}
}
return -1;
}

parseTypeDeclaration(
tn: Tokenizer,
flags: CommonFlags,
Expand All @@ -3499,6 +3533,21 @@ export class Parser extends DiagnosticEmitter {
tn.skip(Token.BAR);
let type = this.parseType(tn);
if (!type) return null;
let depth = this.getRecursiveDepthForTypeDeclaration(name.text, type);
if (depth >= 0) {
if (depth == 0) {
this.error(
DiagnosticCode.Type_alias_0_circularly_references_itself,
tn.range(), name.text
);
} else {
this.error(
DiagnosticCode.Not_implemented_0,
tn.range(), "Recursion in type aliases"
);
}
return null;
}
let ret = Node.createTypeDeclaration(
name,
decorators,
Expand Down
10 changes: 10 additions & 0 deletions tests/parser/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ export type uint64_t = u64;
export type T1 = | int32_t;
export type T2 =
| int32_t;

// disallow type recursion
export type T3 = T3 | null;
export type T4 = (x: T4) => i32;
export type T5 = () => T5;
export type T6<T> = () => T6<T>;
export type T7 = Array<T7>;
export type T8 = Map<string, Array<T8>>;
export type T9 = Array<() => T9>;
export type T10 = T6<T10>;
8 changes: 8 additions & 0 deletions tests/parser/type.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ type int32_t = i32;
export type uint64_t = u64;
export type T1 = int32_t;
export type T2 = int32_t;
// ERROR 2456: "Type alias 'T3' circularly references itself." in type.ts(11,23+4)
// ERROR 100: "Not implemented: Recursion in type aliases" in type.ts(12,29+3)
// ERROR 100: "Not implemented: Recursion in type aliases" in type.ts(13,24+2)
// ERROR 100: "Not implemented: Recursion in type aliases" in type.ts(14,31+1)
// ERROR 100: "Not implemented: Recursion in type aliases" in type.ts(15,26+1)
// ERROR 100: "Not implemented: Recursion in type aliases" in type.ts(16,39+1)
// ERROR 100: "Not implemented: Recursion in type aliases" in type.ts(17,32+1)
// ERROR 100: "Not implemented: Recursion in type aliases" in type.ts(18,25+1)