Skip to content

fix: Fix mixed up naming "upcast" <-> "downcast" for runtime checks #2423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ under the licensing terms detailed in LICENSE:
* ApsarasX <apsarax@outlook.com>
* Adrien Zinger <zinger.ad@gmail.com>
* Ruixiang Chen <xiang19890319@gmail.com>
* Daniel Salvadori <danaugrs@gmail.com>

Portions of this software are derived from third-party works licensed under
the following terms:
Expand Down
22 changes: 11 additions & 11 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3469,12 +3469,12 @@ export class Compiler extends DiagnosticEmitter {
}
fromType = fromType.nonNullableType;
}
if (fromType.isAssignableTo(toType)) { // downcast or same
if (fromType.isAssignableTo(toType)) { // upcast or same
assert(toType.isExternalReference || fromType.kind == toType.kind);
this.currentType = toType;
return expr;
}
if (explicit && toType.nonNullableType.isAssignableTo(fromType)) { // upcast
if (explicit && toType.nonNullableType.isAssignableTo(fromType)) { // downcast
// <Cat | null>(<Animal>maybeCat)
if (toType.isExternalReference) {
this.error(
Expand All @@ -3487,7 +3487,7 @@ export class Compiler extends DiagnosticEmitter {
}
assert(fromType.kind == toType.kind);
if (!this.options.noAssert) {
expr = this.makeRuntimeUpcastCheck(expr, fromType, toType, reportNode);
expr = this.makeRuntimeDowncastCheck(expr, fromType, toType, reportNode);
}
this.currentType = toType;
return expr;
Expand Down Expand Up @@ -7858,7 +7858,7 @@ export class Compiler extends DiagnosticEmitter {
// <nullable> instanceof <nonNullable> - LHS must be != 0
if (actualType.isNullableReference && !expectedType.isNullableReference) {

// downcast - check statically
// upcast - check statically
if (actualType.nonNullableType.isAssignableTo(expectedType)) {
return module.binary(
sizeTypeRef == TypeRef.I64
Expand All @@ -7869,7 +7869,7 @@ export class Compiler extends DiagnosticEmitter {
);
}

// upcast - check dynamically
// downcast - check dynamically
if (expectedType.isAssignableTo(actualType)) {
let program = this.program;
if (!(actualType.isUnmanaged || expectedType.isUnmanaged)) {
Expand Down Expand Up @@ -7908,11 +7908,11 @@ export class Compiler extends DiagnosticEmitter {
// either none or both nullable
} else {

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

// upcast - check dynamically
// downcast - check dynamically
} else if (expectedType.isAssignableTo(actualType)) {
let program = this.program;
if (!(actualType.isUnmanaged || expectedType.isUnmanaged)) {
Expand Down Expand Up @@ -10542,9 +10542,9 @@ export class Compiler extends DiagnosticEmitter {
return expr;
}

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

var staticAbortCallExpr = this.makeStaticAbort(
this.ensureStaticString("unexpected upcast"),
this.ensureStaticString("unexpected downcast"),
reportNode
); // TODO: throw

Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/instanceof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ assert(!(I instanceof A));
assert(!(f instanceof A));
assert(!(F instanceof A));

// assert(!(a instanceof B)); // dynamic upcast, checked in rt/instanceof
// assert(!(a instanceof B)); // dynamic downcast, checked in rt/instanceof
assert( b instanceof B );
assert(!(i instanceof B));
assert(!(I instanceof B));
Expand Down
34 changes: 17 additions & 17 deletions tests/compiler/managed-cast.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
(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")
(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")
(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")
(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")
(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")
(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")
(table $0 1 1 funcref)
(elem $0 (i32.const 1))
Expand Down Expand Up @@ -2119,11 +2119,11 @@
(func $managed-cast/Animal#tame (param $0 i32)
nop
)
(func $managed-cast/testDowncast (param $0 i32)
(func $managed-cast/testUpcast (param $0 i32)
local.get $0
call $managed-cast/Animal#tame
)
(func $managed-cast/testDowncastToNullable (param $0 i32)
(func $managed-cast/testUpcastToNullable (param $0 i32)
(local $1 i32)
local.get $0
local.set $1
Expand All @@ -2133,7 +2133,7 @@
call $managed-cast/Animal#tame
end
)
(func $managed-cast/testDowncastFromToNullable (param $0 i32)
(func $managed-cast/testUpcastFromToNullable (param $0 i32)
(local $1 i32)
local.get $0
local.set $1
Expand Down Expand Up @@ -2183,7 +2183,7 @@
(func $managed-cast/Cat#meow (param $0 i32)
nop
)
(func $managed-cast/testUpcastToNullable (param $0 i32)
(func $managed-cast/testDowncastToNullable (param $0 i32)
(local $1 i32)
(local $2 i32)
global.get $~lib/memory/__stack_pointer
Expand Down Expand Up @@ -2221,7 +2221,7 @@
i32.add
global.set $~lib/memory/__stack_pointer
)
(func $managed-cast/testUpcastFromToNullable (param $0 i32)
(func $managed-cast/testDowncastFromToNullable (param $0 i32)
(local $1 i32)
(local $2 i32)
global.get $~lib/memory/__stack_pointer
Expand Down Expand Up @@ -2379,7 +2379,7 @@
unreachable
end
)
(func $managed-cast/testDowncastFromNullable (param $0 i32)
(func $managed-cast/testUpcastFromNullable (param $0 i32)
(local $1 i32)
(local $2 i32)
global.get $~lib/memory/__stack_pointer
Expand Down Expand Up @@ -2413,7 +2413,7 @@
i32.add
global.set $~lib/memory/__stack_pointer
)
(func $managed-cast/testUpcast (param $0 i32)
(func $managed-cast/testDowncast (param $0 i32)
(local $1 i32)
(local $2 i32)
global.get $~lib/memory/__stack_pointer
Expand Down Expand Up @@ -2449,7 +2449,7 @@
i32.add
global.set $~lib/memory/__stack_pointer
)
(func $managed-cast/testUpcastFromNullable (param $0 i32)
(func $managed-cast/testDowncastFromNullable (param $0 i32)
(local $1 i32)
(local $2 i32)
global.get $~lib/memory/__stack_pointer
Expand Down Expand Up @@ -2533,63 +2533,63 @@
local.get $0
i32.store
local.get $0
call $managed-cast/testDowncast
call $managed-cast/testUpcast
i32.const 0
call $managed-cast/Cat#constructor
local.set $0
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store
local.get $0
call $managed-cast/testDowncastFromNullable
call $managed-cast/testUpcastFromNullable
i32.const 0
call $managed-cast/Cat#constructor
local.set $0
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store
local.get $0
call $managed-cast/testDowncastToNullable
call $managed-cast/testUpcastToNullable
i32.const 0
call $managed-cast/Cat#constructor
local.set $0
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store
local.get $0
call $managed-cast/testDowncastFromToNullable
call $managed-cast/testUpcastFromToNullable
i32.const 0
call $managed-cast/Cat#constructor
local.set $0
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store
local.get $0
call $managed-cast/testUpcast
call $managed-cast/testDowncast
i32.const 0
call $managed-cast/Cat#constructor
local.set $0
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store
local.get $0
call $managed-cast/testUpcastFromNullable
call $managed-cast/testDowncastFromNullable
i32.const 0
call $managed-cast/Cat#constructor
local.set $0
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store
local.get $0
call $managed-cast/testUpcastToNullable
call $managed-cast/testDowncastToNullable
i32.const 0
call $managed-cast/Cat#constructor
local.set $0
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store
local.get $0
call $managed-cast/testUpcastFromToNullable
call $managed-cast/testDowncastFromToNullable
global.get $~lib/memory/__heap_base
global.set $~lib/memory/__stack_pointer
call $~lib/rt/itcms/__collect
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/managed-cast.release.wat
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
(data (i32.const 1500) "<")
(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")
(data (i32.const 1564) "<")
(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")
(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")
(data (i32.const 1632) "\05\00\00\00 \00\00\00\00\00\00\00 ")
(data (i32.const 1660) " \00\00\00\04\00\00\00 ")
(export "memory" (memory $0))
Expand Down
32 changes: 16 additions & 16 deletions tests/compiler/managed-cast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,49 @@ class Cat extends Animal {
meow(): void {}
}

function testDowncast(cat: Cat): void {
function testUpcast(cat: Cat): void {
(<Animal>cat).tame();
}
testDowncast(new Cat());
testUpcast(new Cat());

function testDowncastFromNullable(cat: Cat | null): void {
function testUpcastFromNullable(cat: Cat | null): void {
(<Animal>cat).tame();
}
testDowncastFromNullable(new Cat());
testUpcastFromNullable(new Cat());

function testDowncastToNullable(cat: Cat): void {
function testUpcastToNullable(cat: Cat): void {
var maybeAnimal = <Animal | null>cat;
if (maybeAnimal) maybeAnimal.tame();
}
testDowncastToNullable(new Cat());
testUpcastToNullable(new Cat());

function testDowncastFromToNullable(cat: Cat | null): void {
function testUpcastFromToNullable(cat: Cat | null): void {
var maybeAnimal = <Animal | null>cat;
if (maybeAnimal) maybeAnimal.tame();
}
testDowncastFromToNullable(new Cat());
testUpcastFromToNullable(new Cat());

function testUpcast(animal: Animal): void {
function testDowncast(animal: Animal): void {
(<Cat>animal).meow();
}
testUpcast(new Cat());
testDowncast(new Cat());

function testUpcastFromNullable(animal: Animal | null): void {
function testDowncastFromNullable(animal: Animal | null): void {
(<Cat>animal).meow();
}
testUpcastFromNullable(new Cat());
testDowncastFromNullable(new Cat());

function testUpcastToNullable(animal: Animal): void {
function testDowncastToNullable(animal: Animal): void {
var maybeCat = <Cat | null>animal;
if (maybeCat) maybeCat.meow();
}
testUpcastToNullable(new Cat());
testDowncastToNullable(new Cat());

function testUpcastFromToNullable(animal: Animal | null): void {
function testDowncastFromToNullable(animal: Animal | null): void {
var maybeCat = <Cat | null>animal;
if (maybeCat) maybeCat.meow();
}
testUpcastFromToNullable(new Cat());
testDowncastFromToNullable(new Cat());

__stack_pointer = __heap_base;
__collect();