diff --git a/src/compiler.ts b/src/compiler.ts index 36f09ab8aa..759eb32c0b 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -1574,7 +1574,8 @@ export class Compiler extends DiagnosticEmitter { /** Force compilation of stdlib alternative if a builtin. */ forceStdAlternative: bool = false ): bool { - if (instance.is(CommonFlags.Compiled)) return !instance.is(CommonFlags.Errored); + if (instance.is(CommonFlags.Errored)) return false; + if (instance.is(CommonFlags.Compiled)) return true; if (!forceStdAlternative) { if (instance.hasDecorator(DecoratorFlags.Builtin)) return true; diff --git a/src/program.ts b/src/program.ts index 8bcf1bfa19..e54826b19d 100644 --- a/src/program.ts +++ b/src/program.ts @@ -3850,7 +3850,15 @@ export class Function extends TypedElement { flow.setLocalFlag(local.index, LocalFlags.Initialized); } } - registerConcreteElement(program, this); + if (program.instancesByName.has(this.internalName)) { + program.error( + DiagnosticCode.Duplicate_function_implementation, + prototype.declaration.name.range + ); + this.set(CommonFlags.Errored); + } else { + registerConcreteElement(program, this); + } } /** Gets the types of additional locals that are not parameters. */ diff --git a/tests/compiler/duplicate-function-in-scope.json b/tests/compiler/duplicate-function-in-scope.json new file mode 100644 index 0000000000..be8607a6b1 --- /dev/null +++ b/tests/compiler/duplicate-function-in-scope.json @@ -0,0 +1,8 @@ +{ + "asc_flags": [], + "stderr": [ + "EOF", + "TS2300: Duplicate identifier 'inner'.", + "TS2393: Duplicate function implementation." + ] +} diff --git a/tests/compiler/duplicate-function-in-scope.ts b/tests/compiler/duplicate-function-in-scope.ts new file mode 100644 index 0000000000..7d91e2abfc --- /dev/null +++ b/tests/compiler/duplicate-function-in-scope.ts @@ -0,0 +1,16 @@ +// Duplicate named function declarations in the same scope should +// produce a diagnostic instead of crashing the compiler. + +export function testMixed1(): void { + const inner = function (): void {}; + function inner(): void {} + inner(); +} + +export function test(): void { + function inner(): void {} + function inner(): void {} + inner(); +} + +ERROR("EOF");