Skip to content

Commit

Permalink
(compiler) Detect method being redefined
Browse files Browse the repository at this point in the history
This has been an oversight while working on the experimental Perlang
compiler (#406). The bug
was discovered when implementing the changes in
#463; when we started running
one of those tests, no error was emitted even though the code was
redefining a top-level function. It turned out that the compiler would
silently overwrite a function if you defined it twice.
  • Loading branch information
perlun committed Apr 23, 2024
1 parent aa6ed64 commit f00db47
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions release-notes/v0.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Add `-c` flag for "compile and assemble only" [[#455][455]]
- Remove `using namespace` in generated C++ code [[#458][458]]
- Add partial inline C++ support [[#462][462]]
- Detect method being redefined [[#464][464]]

### Changed
#### Data types
Expand Down Expand Up @@ -45,3 +46,4 @@
[458]: https://github.com/perlang-org/perlang/pull/458
[459]: https://github.com/perlang-org/perlang/pull/459
[462]: https://github.com/perlang-org/perlang/pull/462
[464]: https://github.com/perlang-org/perlang/pull/464
5 changes: 5 additions & 0 deletions src/Perlang.Interpreter/Compiler/PerlangCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,11 @@ public VoidObject VisitFunctionStmt(Stmt.Function functionStmt)

currentMethod = new StringBuilder();

if (methods.ContainsKey(functionStmt.Name.Lexeme))
{
throw new PerlangCompilerException($"Function '{functionStmt.Name.Lexeme}' is already defined");
}

methods[functionStmt.Name.Lexeme] = new Method(
functionStmt.Name.Lexeme,
functionStmt.Parameters,
Expand Down

0 comments on commit f00db47

Please sign in to comment.