Skip to content
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
12 changes: 12 additions & 0 deletions assemblyscript/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7820,6 +7820,18 @@ export class Compiler extends DiagnosticEmitter {
let sourceFunction = flow.targetFunction;
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 closureInfo = this.program.closureScanner.getClosureFunctionInfo(declaration);
let prototype = new FunctionPrototype(
isSemanticallyAnonymous
Expand Down
3 changes: 3 additions & 0 deletions tests/frontend/compiler/duplicate-function-in-scope.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"stderr": ["TS2300: Duplicate identifier 'inner'."]
}
14 changes: 14 additions & 0 deletions tests/frontend/compiler/duplicate-function-in-scope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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();
}
Loading