Skip to content

Commit 40037f5

Browse files
fix: compiler crashed when duplicate inner function declared in function body (#3000)
1 parent 04eba49 commit 40037f5

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src/compiler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,8 @@ export class Compiler extends DiagnosticEmitter {
15741574
/** Force compilation of stdlib alternative if a builtin. */
15751575
forceStdAlternative: bool = false
15761576
): bool {
1577-
if (instance.is(CommonFlags.Compiled)) return !instance.is(CommonFlags.Errored);
1577+
if (instance.is(CommonFlags.Errored)) return false;
1578+
if (instance.is(CommonFlags.Compiled)) return true;
15781579

15791580
if (!forceStdAlternative) {
15801581
if (instance.hasDecorator(DecoratorFlags.Builtin)) return true;

src/program.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3850,7 +3850,15 @@ export class Function extends TypedElement {
38503850
flow.setLocalFlag(local.index, LocalFlags.Initialized);
38513851
}
38523852
}
3853-
registerConcreteElement(program, this);
3853+
if (program.instancesByName.has(this.internalName)) {
3854+
program.error(
3855+
DiagnosticCode.Duplicate_function_implementation,
3856+
prototype.declaration.name.range
3857+
);
3858+
this.set(CommonFlags.Errored);
3859+
} else {
3860+
registerConcreteElement(program, this);
3861+
}
38543862
}
38553863

38563864
/** Gets the types of additional locals that are not parameters. */
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"asc_flags": [],
3+
"stderr": [
4+
"EOF",
5+
"TS2300: Duplicate identifier 'inner'.",
6+
"TS2393: Duplicate function implementation."
7+
]
8+
}
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 testMixed1(): void {
5+
const inner = function (): void {};
6+
function inner(): void {}
7+
inner();
8+
}
9+
10+
export function test(): void {
11+
function inner(): void {}
12+
function inner(): void {}
13+
inner();
14+
}
15+
16+
ERROR("EOF");

0 commit comments

Comments
 (0)