Skip to content

Commit 5faef3a

Browse files
authored
fix: Fix mixed up naming "upcast" <-> "downcast" for runtime checks (#2423)
1 parent 90bd9e8 commit 5faef3a

File tree

6 files changed

+47
-46
lines changed

6 files changed

+47
-46
lines changed

Diff for: NOTICE

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ under the licensing terms detailed in LICENSE:
5050
* ApsarasX <[email protected]>
5151
* Adrien Zinger <[email protected]>
5252
* Ruixiang Chen <[email protected]>
53+
* Daniel Salvadori <[email protected]>
5354

5455
Portions of this software are derived from third-party works licensed under
5556
the following terms:

Diff for: src/compiler.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -3435,12 +3435,12 @@ export class Compiler extends DiagnosticEmitter {
34353435
}
34363436
fromType = fromType.nonNullableType;
34373437
}
3438-
if (fromType.isAssignableTo(toType)) { // downcast or same
3438+
if (fromType.isAssignableTo(toType)) { // upcast or same
34393439
assert(toType.isExternalReference || fromType.kind == toType.kind);
34403440
this.currentType = toType;
34413441
return expr;
34423442
}
3443-
if (explicit && toType.nonNullableType.isAssignableTo(fromType)) { // upcast
3443+
if (explicit && toType.nonNullableType.isAssignableTo(fromType)) { // downcast
34443444
// <Cat | null>(<Animal>maybeCat)
34453445
if (toType.isExternalReference) {
34463446
this.error(
@@ -3453,7 +3453,7 @@ export class Compiler extends DiagnosticEmitter {
34533453
}
34543454
assert(fromType.kind == toType.kind);
34553455
if (!this.options.noAssert) {
3456-
expr = this.makeRuntimeUpcastCheck(expr, fromType, toType, reportNode);
3456+
expr = this.makeRuntimeDowncastCheck(expr, fromType, toType, reportNode);
34573457
}
34583458
this.currentType = toType;
34593459
return expr;
@@ -7511,7 +7511,7 @@ export class Compiler extends DiagnosticEmitter {
75117511
// <nullable> instanceof <nonNullable> - LHS must be != 0
75127512
if (actualType.isNullableReference && !expectedType.isNullableReference) {
75137513

7514-
// downcast - check statically
7514+
// upcast - check statically
75157515
if (actualType.nonNullableType.isAssignableTo(expectedType)) {
75167516
return module.binary(
75177517
sizeTypeRef == TypeRef.I64
@@ -7522,7 +7522,7 @@ export class Compiler extends DiagnosticEmitter {
75227522
);
75237523
}
75247524

7525-
// upcast - check dynamically
7525+
// downcast - check dynamically
75267526
if (expectedType.isAssignableTo(actualType)) {
75277527
let program = this.program;
75287528
if (!(actualType.isUnmanaged || expectedType.isUnmanaged)) {
@@ -7561,11 +7561,11 @@ export class Compiler extends DiagnosticEmitter {
75617561
// either none or both nullable
75627562
} else {
75637563

7564-
// downcast - check statically
7564+
// upcast - check statically
75657565
if (actualType.isAssignableTo(expectedType)) {
75667566
return module.maybeDropCondition(expr, module.i32(1));
75677567

7568-
// upcast - check dynamically
7568+
// downcast - check dynamically
75697569
} else if (expectedType.isAssignableTo(actualType)) {
75707570
let program = this.program;
75717571
if (!(actualType.isUnmanaged || expectedType.isUnmanaged)) {
@@ -10179,9 +10179,9 @@ export class Compiler extends DiagnosticEmitter {
1017910179
return expr;
1018010180
}
1018110181

10182-
/** Makes a runtime upcast check, e.g. on `<Child>parent`. */
10183-
makeRuntimeUpcastCheck(
10184-
/** Expression being upcast. */
10182+
/** Makes a runtime downcast check, e.g. on `<Child>parent`. */
10183+
makeRuntimeDowncastCheck(
10184+
/** Expression being downcast. */
1018510185
expr: ExpressionRef,
1018610186
/** Type of the expression. */
1018710187
type: Type,
@@ -10198,7 +10198,7 @@ export class Compiler extends DiagnosticEmitter {
1019810198
assert(this.compileFunction(instanceofInstance));
1019910199

1020010200
var staticAbortCallExpr = this.makeStaticAbort(
10201-
this.ensureStaticString("unexpected upcast"),
10201+
this.ensureStaticString("unexpected downcast"),
1020210202
reportNode
1020310203
); // TODO: throw
1020410204

Diff for: tests/compiler/instanceof.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ assert(!(I instanceof A));
1515
assert(!(f instanceof A));
1616
assert(!(F instanceof A));
1717

18-
// assert(!(a instanceof B)); // dynamic upcast, checked in rt/instanceof
18+
// assert(!(a instanceof B)); // dynamic downcast, checked in rt/instanceof
1919
assert( b instanceof B );
2020
assert(!(i instanceof B));
2121
assert(!(I instanceof B));

Diff for: tests/compiler/managed-cast.debug.wat

+17-17
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
(data (i32.const 348) "<\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00t\00l\00s\00f\00.\00t\00s\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
3939
(data (i32.const 412) "<\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00\1e\00\00\00u\00n\00e\00x\00p\00e\00c\00t\00e\00d\00 \00n\00u\00l\00l\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
4040
(data (i32.const 476) "<\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00\1e\00\00\00m\00a\00n\00a\00g\00e\00d\00-\00c\00a\00s\00t\00.\00t\00s\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
41-
(data (i32.const 540) "<\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00\"\00\00\00u\00n\00e\00x\00p\00e\00c\00t\00e\00d\00 \00u\00p\00c\00a\00s\00t\00\00\00\00\00\00\00\00\00\00\00")
41+
(data (i32.const 540) "<\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00&\00\00\00u\00n\00e\00x\00p\00e\00c\00t\00e\00d\00 \00d\00o\00w\00n\00c\00a\00s\00t\00\00\00\00\00\00\00")
4242
(data (i32.const 608) "\05\00\00\00 \00\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\00\00\00 \00\00\00\00\00\00\00")
4343
(table $0 1 1 funcref)
4444
(elem $0 (i32.const 1))
@@ -2119,11 +2119,11 @@
21192119
(func $managed-cast/Animal#tame (param $0 i32)
21202120
nop
21212121
)
2122-
(func $managed-cast/testDowncast (param $0 i32)
2122+
(func $managed-cast/testUpcast (param $0 i32)
21232123
local.get $0
21242124
call $managed-cast/Animal#tame
21252125
)
2126-
(func $managed-cast/testDowncastToNullable (param $0 i32)
2126+
(func $managed-cast/testUpcastToNullable (param $0 i32)
21272127
(local $1 i32)
21282128
local.get $0
21292129
local.set $1
@@ -2133,7 +2133,7 @@
21332133
call $managed-cast/Animal#tame
21342134
end
21352135
)
2136-
(func $managed-cast/testDowncastFromToNullable (param $0 i32)
2136+
(func $managed-cast/testUpcastFromToNullable (param $0 i32)
21372137
(local $1 i32)
21382138
local.get $0
21392139
local.set $1
@@ -2183,7 +2183,7 @@
21832183
(func $managed-cast/Cat#meow (param $0 i32)
21842184
nop
21852185
)
2186-
(func $managed-cast/testUpcastToNullable (param $0 i32)
2186+
(func $managed-cast/testDowncastToNullable (param $0 i32)
21872187
(local $1 i32)
21882188
(local $2 i32)
21892189
global.get $~lib/memory/__stack_pointer
@@ -2221,7 +2221,7 @@
22212221
i32.add
22222222
global.set $~lib/memory/__stack_pointer
22232223
)
2224-
(func $managed-cast/testUpcastFromToNullable (param $0 i32)
2224+
(func $managed-cast/testDowncastFromToNullable (param $0 i32)
22252225
(local $1 i32)
22262226
(local $2 i32)
22272227
global.get $~lib/memory/__stack_pointer
@@ -2379,7 +2379,7 @@
23792379
unreachable
23802380
end
23812381
)
2382-
(func $managed-cast/testDowncastFromNullable (param $0 i32)
2382+
(func $managed-cast/testUpcastFromNullable (param $0 i32)
23832383
(local $1 i32)
23842384
(local $2 i32)
23852385
global.get $~lib/memory/__stack_pointer
@@ -2413,7 +2413,7 @@
24132413
i32.add
24142414
global.set $~lib/memory/__stack_pointer
24152415
)
2416-
(func $managed-cast/testUpcast (param $0 i32)
2416+
(func $managed-cast/testDowncast (param $0 i32)
24172417
(local $1 i32)
24182418
(local $2 i32)
24192419
global.get $~lib/memory/__stack_pointer
@@ -2449,7 +2449,7 @@
24492449
i32.add
24502450
global.set $~lib/memory/__stack_pointer
24512451
)
2452-
(func $managed-cast/testUpcastFromNullable (param $0 i32)
2452+
(func $managed-cast/testDowncastFromNullable (param $0 i32)
24532453
(local $1 i32)
24542454
(local $2 i32)
24552455
global.get $~lib/memory/__stack_pointer
@@ -2533,63 +2533,63 @@
25332533
local.get $0
25342534
i32.store
25352535
local.get $0
2536-
call $managed-cast/testDowncast
2536+
call $managed-cast/testUpcast
25372537
i32.const 0
25382538
call $managed-cast/Cat#constructor
25392539
local.set $0
25402540
global.get $~lib/memory/__stack_pointer
25412541
local.get $0
25422542
i32.store
25432543
local.get $0
2544-
call $managed-cast/testDowncastFromNullable
2544+
call $managed-cast/testUpcastFromNullable
25452545
i32.const 0
25462546
call $managed-cast/Cat#constructor
25472547
local.set $0
25482548
global.get $~lib/memory/__stack_pointer
25492549
local.get $0
25502550
i32.store
25512551
local.get $0
2552-
call $managed-cast/testDowncastToNullable
2552+
call $managed-cast/testUpcastToNullable
25532553
i32.const 0
25542554
call $managed-cast/Cat#constructor
25552555
local.set $0
25562556
global.get $~lib/memory/__stack_pointer
25572557
local.get $0
25582558
i32.store
25592559
local.get $0
2560-
call $managed-cast/testDowncastFromToNullable
2560+
call $managed-cast/testUpcastFromToNullable
25612561
i32.const 0
25622562
call $managed-cast/Cat#constructor
25632563
local.set $0
25642564
global.get $~lib/memory/__stack_pointer
25652565
local.get $0
25662566
i32.store
25672567
local.get $0
2568-
call $managed-cast/testUpcast
2568+
call $managed-cast/testDowncast
25692569
i32.const 0
25702570
call $managed-cast/Cat#constructor
25712571
local.set $0
25722572
global.get $~lib/memory/__stack_pointer
25732573
local.get $0
25742574
i32.store
25752575
local.get $0
2576-
call $managed-cast/testUpcastFromNullable
2576+
call $managed-cast/testDowncastFromNullable
25772577
i32.const 0
25782578
call $managed-cast/Cat#constructor
25792579
local.set $0
25802580
global.get $~lib/memory/__stack_pointer
25812581
local.get $0
25822582
i32.store
25832583
local.get $0
2584-
call $managed-cast/testUpcastToNullable
2584+
call $managed-cast/testDowncastToNullable
25852585
i32.const 0
25862586
call $managed-cast/Cat#constructor
25872587
local.set $0
25882588
global.get $~lib/memory/__stack_pointer
25892589
local.get $0
25902590
i32.store
25912591
local.get $0
2592-
call $managed-cast/testUpcastFromToNullable
2592+
call $managed-cast/testDowncastFromToNullable
25932593
global.get $~lib/memory/__heap_base
25942594
global.set $~lib/memory/__stack_pointer
25952595
call $~lib/rt/itcms/__collect

Diff for: tests/compiler/managed-cast.release.wat

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
(data (i32.const 1500) "<")
3535
(data (i32.const 1512) "\01\00\00\00\1e\00\00\00m\00a\00n\00a\00g\00e\00d\00-\00c\00a\00s\00t\00.\00t\00s")
3636
(data (i32.const 1564) "<")
37-
(data (i32.const 1576) "\01\00\00\00\"\00\00\00u\00n\00e\00x\00p\00e\00c\00t\00e\00d\00 \00u\00p\00c\00a\00s\00t")
37+
(data (i32.const 1576) "\01\00\00\00&\00\00\00u\00n\00e\00x\00p\00e\00c\00t\00e\00d\00 \00d\00o\00w\00n\00c\00a\00s\00t")
3838
(data (i32.const 1632) "\05\00\00\00 \00\00\00\00\00\00\00 ")
3939
(data (i32.const 1660) " \00\00\00\04\00\00\00 ")
4040
(export "memory" (memory $0))

Diff for: tests/compiler/managed-cast.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,49 @@ class Cat extends Animal {
55
meow(): void {}
66
}
77

8-
function testDowncast(cat: Cat): void {
8+
function testUpcast(cat: Cat): void {
99
(<Animal>cat).tame();
1010
}
11-
testDowncast(new Cat());
11+
testUpcast(new Cat());
1212

13-
function testDowncastFromNullable(cat: Cat | null): void {
13+
function testUpcastFromNullable(cat: Cat | null): void {
1414
(<Animal>cat).tame();
1515
}
16-
testDowncastFromNullable(new Cat());
16+
testUpcastFromNullable(new Cat());
1717

18-
function testDowncastToNullable(cat: Cat): void {
18+
function testUpcastToNullable(cat: Cat): void {
1919
var maybeAnimal = <Animal | null>cat;
2020
if (maybeAnimal) maybeAnimal.tame();
2121
}
22-
testDowncastToNullable(new Cat());
22+
testUpcastToNullable(new Cat());
2323

24-
function testDowncastFromToNullable(cat: Cat | null): void {
24+
function testUpcastFromToNullable(cat: Cat | null): void {
2525
var maybeAnimal = <Animal | null>cat;
2626
if (maybeAnimal) maybeAnimal.tame();
2727
}
28-
testDowncastFromToNullable(new Cat());
28+
testUpcastFromToNullable(new Cat());
2929

30-
function testUpcast(animal: Animal): void {
30+
function testDowncast(animal: Animal): void {
3131
(<Cat>animal).meow();
3232
}
33-
testUpcast(new Cat());
33+
testDowncast(new Cat());
3434

35-
function testUpcastFromNullable(animal: Animal | null): void {
35+
function testDowncastFromNullable(animal: Animal | null): void {
3636
(<Cat>animal).meow();
3737
}
38-
testUpcastFromNullable(new Cat());
38+
testDowncastFromNullable(new Cat());
3939

40-
function testUpcastToNullable(animal: Animal): void {
40+
function testDowncastToNullable(animal: Animal): void {
4141
var maybeCat = <Cat | null>animal;
4242
if (maybeCat) maybeCat.meow();
4343
}
44-
testUpcastToNullable(new Cat());
44+
testDowncastToNullable(new Cat());
4545

46-
function testUpcastFromToNullable(animal: Animal | null): void {
46+
function testDowncastFromToNullable(animal: Animal | null): void {
4747
var maybeCat = <Cat | null>animal;
4848
if (maybeCat) maybeCat.meow();
4949
}
50-
testUpcastFromToNullable(new Cat());
50+
testDowncastFromToNullable(new Cat());
5151

5252
__stack_pointer = __heap_base;
5353
__collect();

0 commit comments

Comments
 (0)