Skip to content

Commit 5981995

Browse files
authored
fix: Substitute type arguments in 'new' only if prototypes match (#1660)
1 parent cc37cc4 commit 5981995

File tree

4 files changed

+206
-52
lines changed

4 files changed

+206
-52
lines changed

src/compiler.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8442,8 +8442,10 @@ export class Compiler extends DiagnosticEmitter {
84428442
if (
84438443
!typeArguments &&
84448444
(classReference = contextualType.classReference) !== null &&
8445+
classReference.prototype == classPrototype &&
84458446
classReference.is(CommonFlags.GENERIC)
84468447
) {
8448+
// e.g. `arr: Array<T> = new Array()`
84478449
classInstance = this.resolver.resolveClass(
84488450
classPrototype,
84498451
classReference.typeArguments,

tests/compiler/new.optimized.wat

Lines changed: 101 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(module
2+
(type $i32_=>_i32 (func (param i32) (result i32)))
23
(type $none_=>_none (func))
34
(type $i32_i32_=>_none (func (param i32 i32)))
45
(type $none_=>_i32 (func (result i32)))
5-
(type $i32_=>_i32 (func (param i32) (result i32)))
66
(type $i32_=>_none (func (param i32)))
77
(type $i32_i32_i32_=>_none (func (param i32 i32 i32)))
88
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
@@ -18,8 +18,8 @@
1818
(data (i32.const 1304) "\01\00\00\00\14\00\00\00~\00l\00i\00b\00/\00r\00t\00.\00t\00s")
1919
(data (i32.const 1372) "<")
2020
(data (i32.const 1384) "\01\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00t\00l\00s\00f\00.\00t\00s")
21-
(data (i32.const 1440) "\06\00\00\00 \00\00\00\00\00\00\00 ")
22-
(data (i32.const 1468) " \00\00\00\00\00\00\00 \00\00\00\00\00\00\00 ")
21+
(data (i32.const 1440) "\07\00\00\00 \00\00\00\00\00\00\00 ")
22+
(data (i32.const 1468) " \00\00\00\00\00\00\00 \00\00\00\00\00\00\00 \00\00\00\00\00\00\00 \00\00\00\04")
2323
(global $new/ref (mut i32) (i32.const 0))
2424
(global $~lib/rt/itcms/total (mut i32) (i32.const 0))
2525
(global $~lib/rt/itcms/threshold (mut i32) (i32.const 1024))
@@ -33,7 +33,10 @@
3333
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
3434
(global $new/gen (mut i32) (i32.const 0))
3535
(global $new/ref2 (mut i32) (i32.const 0))
36-
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 17876))
36+
(global $new/genext (mut i32) (i32.const 0))
37+
(global $new/genext2 (mut i32) (i32.const 0))
38+
(global $new/genext3 (mut i32) (i32.const 0))
39+
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 17884))
3740
(export "memory" (memory $0))
3841
(start $~start)
3942
(func $~lib/rt/itcms/initLazy (param $0 i32) (result i32)
@@ -66,6 +69,24 @@
6669
local.get $0
6770
call $~lib/rt/itcms/__visit
6871
end
72+
global.get $new/genext
73+
local.tee $0
74+
if
75+
local.get $0
76+
call $~lib/rt/itcms/__visit
77+
end
78+
global.get $new/genext2
79+
local.tee $0
80+
if
81+
local.get $0
82+
call $~lib/rt/itcms/__visit
83+
end
84+
global.get $new/genext3
85+
local.tee $0
86+
if
87+
local.get $0
88+
call $~lib/rt/itcms/__visit
89+
end
6990
i32.const 1248
7091
call $~lib/rt/itcms/__visit
7192
i32.const 1056
@@ -180,7 +201,7 @@
180201
if
181202
i32.const 0
182203
local.get $0
183-
i32.const 17876
204+
i32.const 17884
184205
i32.lt_u
185206
local.get $0
186207
i32.load offset=8
@@ -980,7 +1001,7 @@
9801001
local.set $0
9811002
loop $while-continue|0
9821003
local.get $0
983-
i32.const 17876
1004+
i32.const 17884
9841005
i32.lt_u
9851006
if
9861007
local.get $0
@@ -1070,7 +1091,7 @@
10701091
unreachable
10711092
end
10721093
local.get $0
1073-
i32.const 17876
1094+
i32.const 17884
10741095
i32.lt_u
10751096
if
10761097
local.get $0
@@ -1093,7 +1114,7 @@
10931114
i32.const 4
10941115
i32.add
10951116
local.tee $1
1096-
i32.const 17876
1117+
i32.const 17884
10971118
i32.ge_u
10981119
if
10991120
global.get $~lib/rt/tlsf/ROOT
@@ -1435,28 +1456,31 @@
14351456
)
14361457
(func $~lib/rt/__visit_members (param $0 i32)
14371458
block $invalid
1438-
block $new/ns.Ref
1439-
block $new/Gen<i32>
1440-
block $new/Ref
1441-
block $~lib/arraybuffer/ArrayBufferView
1442-
block $~lib/string/String
1443-
block $~lib/arraybuffer/ArrayBuffer
1444-
local.get $0
1445-
i32.const 8
1446-
i32.sub
1447-
i32.load
1448-
br_table $~lib/arraybuffer/ArrayBuffer $~lib/string/String $~lib/arraybuffer/ArrayBufferView $new/Ref $new/Gen<i32> $new/ns.Ref $invalid
1459+
block $new/GenExt
1460+
block $new/ns.Ref
1461+
block $new/Gen<i32>
1462+
block $new/Ref
1463+
block $~lib/arraybuffer/ArrayBufferView
1464+
block $~lib/string/String
1465+
block $~lib/arraybuffer/ArrayBuffer
1466+
local.get $0
1467+
i32.const 8
1468+
i32.sub
1469+
i32.load
1470+
br_table $~lib/arraybuffer/ArrayBuffer $~lib/string/String $~lib/arraybuffer/ArrayBufferView $new/Ref $new/Gen<i32> $new/ns.Ref $new/GenExt $invalid
1471+
end
1472+
return
14491473
end
14501474
return
14511475
end
1452-
return
1453-
end
1454-
local.get $0
1455-
i32.load
1456-
local.tee $0
1457-
if
14581476
local.get $0
1459-
call $~lib/rt/itcms/__visit
1477+
i32.load
1478+
local.tee $0
1479+
if
1480+
local.get $0
1481+
call $~lib/rt/itcms/__visit
1482+
end
1483+
return
14601484
end
14611485
return
14621486
end
@@ -1498,8 +1522,13 @@
14981522
i32.store
14991523
local.get $0
15001524
global.set $new/ref
1525+
i32.const 0
15011526
call $new/Gen<i32>#constructor
15021527
global.set $new/gen
1528+
i32.const 0
1529+
call $new/Gen<i32>#constructor
1530+
global.set $new/gen
1531+
i32.const 0
15031532
call $new/Gen<i32>#constructor
15041533
local.set $0
15051534
global.get $~lib/memory/__stack_pointer
@@ -1518,14 +1547,20 @@
15181547
i32.store
15191548
local.get $0
15201549
global.set $new/ref2
1550+
call $new/GenExt#constructor
1551+
global.set $new/genext
1552+
call $new/GenExt#constructor
1553+
global.set $new/genext2
1554+
call $new/GenExt#constructor
1555+
global.set $new/genext3
15211556
global.get $~lib/memory/__stack_pointer
15221557
i32.const 4
15231558
i32.add
15241559
global.set $~lib/memory/__stack_pointer
15251560
)
15261561
(func $~stack_check
15271562
global.get $~lib/memory/__stack_pointer
1528-
i32.const 1492
1563+
i32.const 1500
15291564
i32.lt_s
15301565
if
15311566
i32.const 17904
@@ -1536,6 +1571,35 @@
15361571
unreachable
15371572
end
15381573
)
1574+
(func $new/GenExt#constructor (result i32)
1575+
(local $0 i32)
1576+
global.get $~lib/memory/__stack_pointer
1577+
i32.const 8
1578+
i32.sub
1579+
global.set $~lib/memory/__stack_pointer
1580+
call $~stack_check
1581+
global.get $~lib/memory/__stack_pointer
1582+
i64.const 0
1583+
i64.store
1584+
global.get $~lib/memory/__stack_pointer
1585+
i32.const 6
1586+
call $~lib/rt/itcms/__new
1587+
local.tee $0
1588+
i32.store
1589+
global.get $~lib/memory/__stack_pointer
1590+
local.get $0
1591+
i32.store offset=4
1592+
global.get $~lib/memory/__stack_pointer
1593+
local.get $0
1594+
call $new/Gen<i32>#constructor
1595+
local.tee $0
1596+
i32.store
1597+
global.get $~lib/memory/__stack_pointer
1598+
i32.const 8
1599+
i32.add
1600+
global.set $~lib/memory/__stack_pointer
1601+
local.get $0
1602+
)
15391603
(func $new/Ref#constructor (result i32)
15401604
(local $0 i32)
15411605
global.get $~lib/memory/__stack_pointer
@@ -1557,8 +1621,7 @@
15571621
global.set $~lib/memory/__stack_pointer
15581622
local.get $0
15591623
)
1560-
(func $new/Gen<i32>#constructor (result i32)
1561-
(local $0 i32)
1624+
(func $new/Gen<i32>#constructor (param $0 i32) (result i32)
15621625
global.get $~lib/memory/__stack_pointer
15631626
i32.const 4
15641627
i32.sub
@@ -1567,11 +1630,15 @@
15671630
global.get $~lib/memory/__stack_pointer
15681631
i32.const 0
15691632
i32.store
1570-
global.get $~lib/memory/__stack_pointer
1571-
i32.const 4
1572-
call $~lib/rt/itcms/__new
1573-
local.tee $0
1574-
i32.store
1633+
local.get $0
1634+
i32.eqz
1635+
if
1636+
global.get $~lib/memory/__stack_pointer
1637+
i32.const 4
1638+
call $~lib/rt/itcms/__new
1639+
local.tee $0
1640+
i32.store
1641+
end
15751642
global.get $~lib/memory/__stack_pointer
15761643
i32.const 4
15771644
i32.add

tests/compiler/new.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Gen<T> {
1212
}
1313

1414
var gen: Gen<i32>;
15+
gen = new Gen();
1516
gen = new Gen<i32>();
1617
gen = new Gen<i32>().gen;
1718

@@ -25,3 +26,10 @@ var ref2: ns.Ref;
2526
ref2 = new ns.Ref();
2627
ref2 = new ns.Ref;
2728
ref2 = new ns.Ref().ref;
29+
30+
class GenExt extends Gen<i32> {
31+
}
32+
33+
var genext = new GenExt();
34+
var genext2: GenExt = new GenExt();
35+
var genext3: Gen<i32> = new GenExt();

0 commit comments

Comments
 (0)