-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix name duplication in code generation for new definition typing
Please `git cherrypick` this to a separate PR.
- Loading branch information
Showing
26 changed files
with
177 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
:NewDefs | ||
|
||
:js | ||
fun foo: Int -> Int | ||
fun foo = x => x + 1 // <- This will be added as a value symbol before translating `Bar`. | ||
class Bar { | ||
fun calc(x) = foo(x) | ||
} | ||
//│ fun foo: Int -> Int | ||
//│ class Bar { | ||
//│ constructor() | ||
//│ fun calc: Int -> Int | ||
//│ } | ||
//│ fun foo: Int -> Int | ||
//│ // Prelude | ||
//│ let res; | ||
//│ class TypingUnit { | ||
//│ #Bar; | ||
//│ constructor() { | ||
//│ } | ||
//│ get Bar() { | ||
//│ const qualifier = this; | ||
//│ if (this.#Bar === undefined) { | ||
//│ class Bar { | ||
//│ constructor() { | ||
//│ } | ||
//│ calc(x) { | ||
//│ return foo(x); | ||
//│ } | ||
//│ }; | ||
//│ this.#Bar = Bar; | ||
//│ } | ||
//│ return this.#Bar; | ||
//│ } | ||
//│ } | ||
//│ const typing_unit = new TypingUnit; | ||
//│ globalThis.Bar = typing_unit.Bar; | ||
//│ // Query 1 is empty | ||
//│ // Query 2 | ||
//│ globalThis.foo = function foo(x) { | ||
//│ return x + 1; | ||
//│ }; | ||
//│ // End of generated code | ||
|
||
// Note: This test case looks trivial but it was like: | ||
// | ||
// ``` | ||
// :re | ||
// (new Bar()).calc(0) | ||
// //│ Int | ||
// //│ res | ||
// //│ Runtime error: | ||
// //│ ReferenceError: foo is not defined | ||
// ``` | ||
// | ||
// My fix is a little bit hacky. The root of the problem is: when generating | ||
// code within a class, we need all top-level bindings to be accessible. This | ||
// part of implementation of new-definition-typing chose to declare all term | ||
// `NuFunDef` as `ValueSymbol` in advance, but this can lead to the fact that | ||
// the same symbol is declared multiple times, thus wasting some runtime names. | ||
// Consequently, the code that references these wasted runtime names are invalid. | ||
// | ||
// Actually, I have a better solution, but it requires adjusting the order of | ||
// translation, and I don't have much time to spend on this at the moment. So, | ||
// my current fix is rather hacky. But I will complete this part after `PreTyper` | ||
// is finished, when I replacing the old Scope with the new Scope. | ||
// | ||
// Luyu Cheng on 2023/12/30 | ||
(new Bar()).calc(0) | ||
//│ Int | ||
//│ res | ||
//│ = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.