Skip to content

Commit ee1ab19

Browse files
authored
Merge branch 'new-definition-typing' into ucs-paper
2 parents 2ff4c7b + 88df416 commit ee1ab19

File tree

6 files changed

+145
-14
lines changed

6 files changed

+145
-14
lines changed

shared/src/main/scala/mlscript/codegen/Codegen.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -854,10 +854,10 @@ final case class JSClassNewDecl(
854854
if (s.isEmpty) s"${p._1}"
855855
else s"${p._1}, $s")
856856
nestedTypes.foreach(t => buffer += s" #$t;")
857-
if (!privateMem.isEmpty) {
858-
privateMem.foreach(f => buffer += s" #${f};")
859-
privateMem.foreach(f => buffer += s" get ${f}() { return this.#${f}; }")
860-
}
857+
privateMem.distinct.foreach(f => {
858+
buffer += s" #${f};"
859+
buffer += s" get ${f}() { return this.#${f}; }"
860+
})
861861
buffer += s" constructor($params) {"
862862
if (`extends`.isDefined) {
863863
val sf = superFields.iterator.zipWithIndex.foldLeft("")((res, p) =>

shared/src/test/diff/codegen/ConstructorStmt.mls

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ class Baz {
212212
//│ if (this.#Baz === undefined) {
213213
//│ class Baz {
214214
//│ #x;
215-
//│ #y;
216215
//│ get x() { return this.#x; }
216+
//│ #y;
217217
//│ get y() { return this.#y; }
218218
//│ constructor() {
219219
//│ this.#x = 123;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
:NewParser
2+
:NewDefs
3+
4+
:js
5+
class C(a: int) { let a = 1 }
6+
//│ class C(a: int) {
7+
//│ let a: 1
8+
//│ }
9+
//│ // Prelude
10+
//│ let res;
11+
//│ class TypingUnit {
12+
//│ #C;
13+
//│ constructor() {
14+
//│ }
15+
//│ get C() {
16+
//│ const outer = this;
17+
//│ if (this.#C === undefined) {
18+
//│ class C {
19+
//│ #a;
20+
//│ get a() { return this.#a; }
21+
//│ constructor(a) {
22+
//│ this.#a = a;
23+
//│ this.#a = 1;
24+
//│ const a1 = this.#a;
25+
//│ }
26+
//│ };
27+
//│ this.#C = ((a) => new C(a));
28+
//│ this.#C.class = C;
29+
//│ }
30+
//│ return this.#C;
31+
//│ }
32+
//│ }
33+
//│ const typing_unit = new TypingUnit;
34+
//│ globalThis.C = typing_unit.C;
35+
//│ // End of generated code
36+
37+
// should return 1
38+
let a = C(2)
39+
a.a
40+
//│ let a: C
41+
//│ 1
42+
//│ a
43+
//│ = C {}
44+
//│ res
45+
//│ = 1
46+
47+
:js
48+
class C2(a: int, b: int) {
49+
let a = b + 1
50+
let b = a + 1
51+
}
52+
//│ class C2(a: int, b: int) {
53+
//│ let a: int
54+
//│ let b: int
55+
//│ }
56+
//│ // Prelude
57+
//│ class TypingUnit2 {
58+
//│ #C2;
59+
//│ constructor() {
60+
//│ }
61+
//│ get C2() {
62+
//│ const outer = this;
63+
//│ if (this.#C2 === undefined) {
64+
//│ class C2 {
65+
//│ #a;
66+
//│ get a() { return this.#a; }
67+
//│ #b;
68+
//│ get b() { return this.#b; }
69+
//│ constructor(a, b) {
70+
//│ this.#a = a;
71+
//│ this.#b = b;
72+
//│ this.#a = b + 1;
73+
//│ const a1 = this.#a;
74+
//│ this.#b = a1 + 1;
75+
//│ const b1 = this.#b;
76+
//│ }
77+
//│ };
78+
//│ this.#C2 = ((a, b) => new C2(a, b));
79+
//│ this.#C2.class = C2;
80+
//│ }
81+
//│ return this.#C2;
82+
//│ }
83+
//│ }
84+
//│ const typing_unit2 = new TypingUnit2;
85+
//│ globalThis.C2 = typing_unit2.C2;
86+
//│ // End of generated code
87+
88+
let c2 = C2(1, 2)
89+
c2.a
90+
c2.b
91+
//│ let c2: C2
92+
//│ int
93+
//│ c2
94+
//│ = C2 {}
95+
//│ res
96+
//│ = 3
97+
//│ res
98+
//│ = 4
99+
100+
class C3(a: int) {
101+
let a = 42
102+
class C4(a: int) {
103+
let a = 44
104+
}
105+
}
106+
//│ class C3(a: int) {
107+
//│ class C4(a: int) {
108+
//│ let a: 44
109+
//│ }
110+
//│ let a: 42
111+
//│ }
112+
113+
:e
114+
let c3 = C3(1)
115+
let c4 = c3.C4(2)
116+
c3.a
117+
c4.a
118+
//│ ╔══[ERROR] access to class member not yet supported
119+
//│ ║ l.115: let c4 = c3.C4(2)
120+
//│ ╙── ^^^
121+
//│ let c3: C3
122+
//│ let c4: error
123+
//│ error
124+
//│ c3
125+
//│ = C3 {}
126+
//│ c4
127+
//│ = C4 {}
128+
//│ res
129+
//│ = 42
130+
//│ res
131+
//│ = 44

shared/src/test/diff/codegen/Mixin.mls

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class Lit(n: int)
1919
//│ if (this.#Add === undefined) {
2020
//│ class Add {
2121
//│ #lhs;
22-
//│ #rhs;
2322
//│ get lhs() { return this.#lhs; }
23+
//│ #rhs;
2424
//│ get rhs() { return this.#rhs; }
2525
//│ constructor(lhs, rhs) {
2626
//│ this.#lhs = lhs;
@@ -66,8 +66,8 @@ class Lit(n: int)
6666
//│ │ │ if (this.#Add === undefined) {
6767
//│ │ │ class Add {
6868
//│ │ │ #lhs;
69-
//│ │ │ #rhs;
7069
//│ │ │ get lhs() { return this.#lhs; }
70+
//│ │ │ #rhs;
7171
//│ │ │ get rhs() { return this.#rhs; }
7272
//│ │ │ constructor(lhs, rhs) {
7373
//│ │ │ this.#lhs = lhs;

shared/src/test/diff/codegen/Nested.mls

+6-6
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,8 @@ module H {
582582
//│ if (this.#J === undefined) {
583583
//│ class J {
584584
//│ #x;
585-
//│ #i;
586585
//│ get x() { return this.#x; }
586+
//│ #i;
587587
//│ get i() { return this.#i; }
588588
//│ constructor(x) {
589589
//│ this.#x = x;
@@ -666,8 +666,8 @@ ij.incY
666666
//│ class I {
667667
//│ #J;
668668
//│ #x;
669-
//│ #y;
670669
//│ get x() { return this.#x; }
670+
//│ #y;
671671
//│ get y() { return this.#y; }
672672
//│ constructor(x) {
673673
//│ this.#x = x;
@@ -679,8 +679,8 @@ ij.incY
679679
//│ if (this.#J === undefined) {
680680
//│ class J {
681681
//│ #x;
682-
//│ #y;
683682
//│ get x() { return this.#x; }
683+
//│ #y;
684684
//│ get y() { return this.#y; }
685685
//│ constructor(x) {
686686
//│ this.#x = x;
@@ -1125,8 +1125,8 @@ I(1).J(3).a
11251125
//│ class I {
11261126
//│ #J;
11271127
//│ #x;
1128-
//│ #y;
11291128
//│ get x() { return this.#x; }
1129+
//│ #y;
11301130
//│ get y() { return this.#y; }
11311131
//│ constructor(x) {
11321132
//│ this.#x = x;
@@ -1138,8 +1138,8 @@ I(1).J(3).a
11381138
//│ if (this.#J === undefined) {
11391139
//│ class J {
11401140
//│ #z;
1141-
//│ #a;
11421141
//│ get z() { return this.#z; }
1142+
//│ #a;
11431143
//│ get a() { return this.#a; }
11441144
//│ constructor(z) {
11451145
//│ this.#z = z;
@@ -1378,8 +1378,8 @@ class Outer1(outer: int) {
13781378
//│ if (this.#Outer2 === undefined) {
13791379
//│ class Outer2 {
13801380
//│ #x;
1381-
//│ #outer;
13821381
//│ get x() { return this.#x; }
1382+
//│ #outer;
13831383
//│ get outer() { return this.#outer; }
13841384
//│ constructor(x) {
13851385
//│ this.#x = x;

shared/src/test/diff/codegen/Super.mls

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ mixin Foo1 {
5050
//│ const outer = this;
5151
//│ return (class Foo1 extends base {
5252
//│ #foo0;
53-
//│ #foo1;
5453
//│ get foo0() { return this.#foo0; }
54+
//│ #foo1;
5555
//│ get foo1() { return this.#foo1; }
5656
//│ constructor(...rest) {
5757
//│ super(...rest);

0 commit comments

Comments
 (0)