diff --git a/NOTICE b/NOTICE index 6371a08222..bf6d984510 100644 --- a/NOTICE +++ b/NOTICE @@ -50,6 +50,7 @@ under the licensing terms detailed in LICENSE: * ApsarasX * Adrien Zinger * Ruixiang Chen +* Daniel Salvadori Portions of this software are derived from third-party works licensed under the following terms: diff --git a/src/compiler.ts b/src/compiler.ts index c5741d2436..11a8ff7103 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -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 // (maybeCat) if (toType.isExternalReference) { this.error( @@ -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; @@ -7858,7 +7858,7 @@ export class Compiler extends DiagnosticEmitter { // instanceof - 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 @@ -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)) { @@ -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)) { @@ -10542,9 +10542,9 @@ export class Compiler extends DiagnosticEmitter { return expr; } - /** Makes a runtime upcast check, e.g. on `parent`. */ - makeRuntimeUpcastCheck( - /** Expression being upcast. */ + /** Makes a runtime downcast check, e.g. on `parent`. */ + makeRuntimeDowncastCheck( + /** Expression being downcast. */ expr: ExpressionRef, /** Type of the expression. */ type: Type, @@ -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 diff --git a/tests/compiler/instanceof.ts b/tests/compiler/instanceof.ts index ff793c2b3d..137f072f0d 100644 --- a/tests/compiler/instanceof.ts +++ b/tests/compiler/instanceof.ts @@ -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)); diff --git a/tests/compiler/managed-cast.debug.wat b/tests/compiler/managed-cast.debug.wat index eda60f08b0..51de7f43b6 100644 --- a/tests/compiler/managed-cast.debug.wat +++ b/tests/compiler/managed-cast.debug.wat @@ -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)) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -2533,7 +2533,7 @@ 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 @@ -2541,7 +2541,7 @@ 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 @@ -2549,7 +2549,7 @@ 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 @@ -2557,7 +2557,7 @@ 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 @@ -2565,7 +2565,7 @@ 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 @@ -2573,7 +2573,7 @@ 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 @@ -2581,7 +2581,7 @@ 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 @@ -2589,7 +2589,7 @@ 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 diff --git a/tests/compiler/managed-cast.release.wat b/tests/compiler/managed-cast.release.wat index 7ab03bc3d4..132edfb312 100644 --- a/tests/compiler/managed-cast.release.wat +++ b/tests/compiler/managed-cast.release.wat @@ -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)) diff --git a/tests/compiler/managed-cast.ts b/tests/compiler/managed-cast.ts index 1286778662..454dd834f1 100644 --- a/tests/compiler/managed-cast.ts +++ b/tests/compiler/managed-cast.ts @@ -5,49 +5,49 @@ class Cat extends Animal { meow(): void {} } -function testDowncast(cat: Cat): void { +function testUpcast(cat: Cat): void { (cat).tame(); } -testDowncast(new Cat()); +testUpcast(new Cat()); -function testDowncastFromNullable(cat: Cat | null): void { +function testUpcastFromNullable(cat: Cat | null): void { (cat).tame(); } -testDowncastFromNullable(new Cat()); +testUpcastFromNullable(new Cat()); -function testDowncastToNullable(cat: Cat): void { +function testUpcastToNullable(cat: Cat): void { var maybeAnimal = 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 = cat; if (maybeAnimal) maybeAnimal.tame(); } -testDowncastFromToNullable(new Cat()); +testUpcastFromToNullable(new Cat()); -function testUpcast(animal: Animal): void { +function testDowncast(animal: Animal): void { (animal).meow(); } -testUpcast(new Cat()); +testDowncast(new Cat()); -function testUpcastFromNullable(animal: Animal | null): void { +function testDowncastFromNullable(animal: Animal | null): void { (animal).meow(); } -testUpcastFromNullable(new Cat()); +testDowncastFromNullable(new Cat()); -function testUpcastToNullable(animal: Animal): void { +function testDowncastToNullable(animal: Animal): void { var maybeCat = 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 = animal; if (maybeCat) maybeCat.meow(); } -testUpcastFromToNullable(new Cat()); +testDowncastFromToNullable(new Cat()); __stack_pointer = __heap_base; __collect();