Skip to content
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
15 changes: 15 additions & 0 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7186,6 +7186,21 @@ export class Compiler extends DiagnosticEmitter {
let sourceFunction = flow.sourceFunction;
let isNamed = declaration.name.text.length > 0;
let isSemanticallyAnonymous = !isNamed || contextualType != Type.void;

// Check for duplicate named function declarations before creating the
// prototype, since registering a concrete element with a duplicate
// internalName would trigger an assertion error.
if (!isSemanticallyAnonymous) {
let existingLocal = flow.getScopedLocal(declaration.name.text);
if (existingLocal) {
this.error(
DiagnosticCode.Duplicate_identifier_0,
declaration.name.range, declaration.name.text
);
return this.module.unreachable();
}
}

let prototype = new FunctionPrototype(
isSemanticallyAnonymous
? `${isNamed ? declaration.name.text : "anonymous"}|${sourceFunction.nextAnonymousId++}`
Expand Down
7 changes: 7 additions & 0 deletions tests/compiler/duplicate-function-in-scope.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"asc_flags": [],
"stderr": [
"EOF",
"TS2300: Duplicate identifier 'inner'."
]
}
16 changes: 16 additions & 0 deletions tests/compiler/duplicate-function-in-scope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Duplicate named function declarations in the same scope should
// produce a diagnostic instead of crashing the compiler.

export function test(): void {
function inner(): i32 {
let x: i32 = 0;
return x;
}
function inner(): i32 {
let x: i32 = 0;
return x + 1;
}
inner();
}

ERROR("EOF");
Loading