Skip to content

Commit 1c5aff8

Browse files
committed
Fix downcast and upcast (were switched)
1 parent 270d00d commit 1c5aff8

File tree

4 files changed

+44
-44
lines changed

4 files changed

+44
-44
lines changed

src/compiler.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3469,12 +3469,12 @@ export class Compiler extends DiagnosticEmitter {
34693469
}
34703470
fromType = fromType.nonNullableType;
34713471
}
3472-
if (fromType.isAssignableTo(toType)) { // downcast or same
3472+
if (fromType.isAssignableTo(toType)) { // upcast or same
34733473
assert(toType.isExternalReference || fromType.kind == toType.kind);
34743474
this.currentType = toType;
34753475
return expr;
34763476
}
3477-
if (explicit && toType.nonNullableType.isAssignableTo(fromType)) { // upcast
3477+
if (explicit && toType.nonNullableType.isAssignableTo(fromType)) { // downcast
34783478
// <Cat | null>(<Animal>maybeCat)
34793479
if (toType.isExternalReference) {
34803480
this.error(
@@ -3487,7 +3487,7 @@ export class Compiler extends DiagnosticEmitter {
34873487
}
34883488
assert(fromType.kind == toType.kind);
34893489
if (!this.options.noAssert) {
3490-
expr = this.makeRuntimeUpcastCheck(expr, fromType, toType, reportNode);
3490+
expr = this.makeRuntimeDowncastCheck(expr, fromType, toType, reportNode);
34913491
}
34923492
this.currentType = toType;
34933493
return expr;
@@ -7858,7 +7858,7 @@ export class Compiler extends DiagnosticEmitter {
78587858
// <nullable> instanceof <nonNullable> - LHS must be != 0
78597859
if (actualType.isNullableReference && !expectedType.isNullableReference) {
78607860

7861-
// downcast - check statically
7861+
// upcast - check statically
78627862
if (actualType.nonNullableType.isAssignableTo(expectedType)) {
78637863
return module.binary(
78647864
sizeTypeRef == TypeRef.I64
@@ -7869,7 +7869,7 @@ export class Compiler extends DiagnosticEmitter {
78697869
);
78707870
}
78717871

7872-
// upcast - check dynamically
7872+
// downcast - check dynamically
78737873
if (expectedType.isAssignableTo(actualType)) {
78747874
let program = this.program;
78757875
if (!(actualType.isUnmanaged || expectedType.isUnmanaged)) {
@@ -7908,11 +7908,11 @@ export class Compiler extends DiagnosticEmitter {
79087908
// either none or both nullable
79097909
} else {
79107910

7911-
// downcast - check statically
7911+
// upcast - check statically
79127912
if (actualType.isAssignableTo(expectedType)) {
79137913
return module.maybeDropCondition(expr, module.i32(1));
79147914

7915-
// upcast - check dynamically
7915+
// downcast - check dynamically
79167916
} else if (expectedType.isAssignableTo(actualType)) {
79177917
let program = this.program;
79187918
if (!(actualType.isUnmanaged || expectedType.isUnmanaged)) {
@@ -10542,9 +10542,9 @@ export class Compiler extends DiagnosticEmitter {
1054210542
return expr;
1054310543
}
1054410544

10545-
/** Makes a runtime upcast check, e.g. on `<Child>parent`. */
10546-
makeRuntimeUpcastCheck(
10547-
/** Expression being upcast. */
10545+
/** Makes a runtime downcast check, e.g. on `<Child>parent`. */
10546+
makeRuntimeDowncastCheck(
10547+
/** Expression being downcast. */
1054810548
expr: ExpressionRef,
1054910549
/** Type of the expression. */
1055010550
type: Type,
@@ -10561,7 +10561,7 @@ export class Compiler extends DiagnosticEmitter {
1056110561
assert(this.compileFunction(instanceofInstance));
1056210562

1056310563
var staticAbortCallExpr = this.makeStaticAbort(
10564-
this.ensureStaticString("unexpected upcast"),
10564+
this.ensureStaticString("unexpected downcast"),
1056510565
reportNode
1056610566
); // TODO: throw
1056710567

tests/compiler/instanceof.ts

Lines changed: 1 addition & 1 deletion
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));

tests/compiler/managed-cast.debug.wat

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/compiler/managed-cast.ts

Lines changed: 16 additions & 16 deletions
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)