Skip to content

Commit d09b19b

Browse files
fix: compiler crashed when duplicate inner function declared in function body
1 parent 52b9533 commit d09b19b

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/compiler.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7186,6 +7186,21 @@ export class Compiler extends DiagnosticEmitter {
71867186
let sourceFunction = flow.sourceFunction;
71877187
let isNamed = declaration.name.text.length > 0;
71887188
let isSemanticallyAnonymous = !isNamed || contextualType != Type.void;
7189+
7190+
// Check for duplicate named function declarations before creating the
7191+
// prototype, since registering a concrete element with a duplicate
7192+
// internalName would trigger an assertion error.
7193+
if (!isSemanticallyAnonymous) {
7194+
let existingLocal = flow.getScopedLocal(declaration.name.text);
7195+
if (existingLocal) {
7196+
this.error(
7197+
DiagnosticCode.Duplicate_identifier_0,
7198+
declaration.name.range, declaration.name.text
7199+
);
7200+
return this.module.unreachable();
7201+
}
7202+
}
7203+
71897204
let prototype = new FunctionPrototype(
71907205
isSemanticallyAnonymous
71917206
? `${isNamed ? declaration.name.text : "anonymous"}|${sourceFunction.nextAnonymousId++}`
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"asc_flags": [],
3+
"stderr": [
4+
"TS2300: Duplicate identifier 'inner'.",
5+
"EOF"
6+
]
7+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Duplicate named function declarations in the same scope should
2+
// produce a diagnostic instead of crashing the compiler.
3+
4+
export function test(): void {
5+
function inner(): i32 {
6+
let x: i32 = 0;
7+
return x;
8+
}
9+
function inner(): i32 {
10+
let x: i32 = 0;
11+
return x + 1;
12+
}
13+
inner();
14+
}
15+
16+
ERROR("EOF");

0 commit comments

Comments
 (0)