From 8b5d29120d39d72aeb1b18ef7b3aba6822256cf2 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 18 Aug 2022 06:20:28 +0300 Subject: [PATCH 1/6] diallow any -> v128 or v128 -> any conversion --- src/compiler.ts | 9 +++++++++ tests/compiler/simd-errors.json | 10 ++++++++++ tests/compiler/simd-errors.ts | 15 +++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 tests/compiler/simd-errors.json create mode 100644 tests/compiler/simd-errors.ts diff --git a/src/compiler.ts b/src/compiler.ts index 16454f998c..9c597aabcd 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -3451,6 +3451,15 @@ export class Compiler extends DiagnosticEmitter { // any to void if (toType.kind == TypeKind.VOID) return module.drop(expr); + // v128 to any / any to v128 + if (toType.kind == TypeKind.V128 || fromType.kind == TypeKind.V128) { + this.error( + DiagnosticCode.Type_0_is_not_assignable_to_type_1, + reportNode.range, fromType.toString(), toType.toString() + ); + return module.unreachable(); + } + // reference involved if (fromType.isReference || toType.isReference) { if (this.currentFlow.isNonnull(expr, fromType)) { diff --git a/tests/compiler/simd-errors.json b/tests/compiler/simd-errors.json new file mode 100644 index 0000000000..9a0b2ef259 --- /dev/null +++ b/tests/compiler/simd-errors.json @@ -0,0 +1,10 @@ +{ + "asc_flags": [ + "--enable", "simd" + ], + "stderr": [ + "TS2322: Type 'f32' is not assignable to type 'v128'.", + "TS2322: Type 'i32' is not assignable to type 'v128'.", + "EOF" + ] +} diff --git a/tests/compiler/simd-errors.ts b/tests/compiler/simd-errors.ts new file mode 100644 index 0000000000..c2de705258 --- /dev/null +++ b/tests/compiler/simd-errors.ts @@ -0,0 +1,15 @@ +// f32 +{ + let a = f32x4.splat(0); + let b: f32 = 0; + v128.add(a, b); +} + +// i32 +{ + let a: i32 = 0; + let b = i32x4.splat(0); + v128.sub(a, b); +} + +ERROR("EOF"); From 769c78e553b14fcfe42d33abe7d26ffed0d4fa25 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 18 Aug 2022 06:30:09 +0300 Subject: [PATCH 2/6] fix --- src/compiler.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/compiler.ts b/src/compiler.ts index 9c597aabcd..b76890224f 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -3452,7 +3452,11 @@ export class Compiler extends DiagnosticEmitter { if (toType.kind == TypeKind.VOID) return module.drop(expr); // v128 to any / any to v128 - if (toType.kind == TypeKind.V128 || fromType.kind == TypeKind.V128) { + // except v128 to bool + if ( + (toType.kind != fromType.kind && toType.kind != TypeKind.BOOL) && + (toType.kind == TypeKind.V128 || fromType.kind == TypeKind.V128) + ) { this.error( DiagnosticCode.Type_0_is_not_assignable_to_type_1, reportNode.range, fromType.toString(), toType.toString() From b9251579ea36caa72ac738f1cce919289294eb2c Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 18 Aug 2022 06:41:55 +0300 Subject: [PATCH 3/6] better --- src/compiler.ts | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index b76890224f..53bb67444a 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -3451,19 +3451,6 @@ export class Compiler extends DiagnosticEmitter { // any to void if (toType.kind == TypeKind.VOID) return module.drop(expr); - // v128 to any / any to v128 - // except v128 to bool - if ( - (toType.kind != fromType.kind && toType.kind != TypeKind.BOOL) && - (toType.kind == TypeKind.V128 || fromType.kind == TypeKind.V128) - ) { - this.error( - DiagnosticCode.Type_0_is_not_assignable_to_type_1, - reportNode.range, fromType.toString(), toType.toString() - ); - return module.unreachable(); - } - // reference involved if (fromType.isReference || toType.isReference) { if (this.currentFlow.isNonnull(expr, fromType)) { @@ -3510,6 +3497,25 @@ export class Compiler extends DiagnosticEmitter { // not dealing with references from here on assert(!fromType.isReference && !toType.isReference); + // Early return if we have same types + if (toType.kind == fromType.kind) { + this.currentType = toType; + return expr; + } + + // v128 to any / any to v128 + // except v128 to bool + if ( + toType.kind != TypeKind.BOOL && + (toType.kind == TypeKind.V128 || fromType.kind == TypeKind.V128) + ) { + this.error( + DiagnosticCode.Type_0_is_not_assignable_to_type_1, + reportNode.range, fromType.toString(), toType.toString() + ); + return module.unreachable(); + } + if (!fromType.isAssignableTo(toType)) { if (!explicit) { this.error( From 2c40c927fb673ca9047e6ec40739003eb1be6a16 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 20 Aug 2022 18:43:20 +0300 Subject: [PATCH 4/6] use isVectorValue --- src/compiler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler.ts b/src/compiler.ts index 53bb67444a..d26cd42479 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -3507,7 +3507,7 @@ export class Compiler extends DiagnosticEmitter { // except v128 to bool if ( toType.kind != TypeKind.BOOL && - (toType.kind == TypeKind.V128 || fromType.kind == TypeKind.V128) + (toType.isVectorValue || fromType.isVectorValue) ) { this.error( DiagnosticCode.Type_0_is_not_assignable_to_type_1, From 35615c81c23eb0b555cdad2db455b5afeb1ac955 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 20 Aug 2022 18:47:14 +0300 Subject: [PATCH 5/6] also use isBooleanValue --- src/compiler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler.ts b/src/compiler.ts index d26cd42479..3fe2bba772 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -3506,7 +3506,7 @@ export class Compiler extends DiagnosticEmitter { // v128 to any / any to v128 // except v128 to bool if ( - toType.kind != TypeKind.BOOL && + !toType.isBooleanValue && (toType.isVectorValue || fromType.isVectorValue) ) { this.error( From 8903e36e95cb9458c6af1fbae10ad3f9d4e3f43c Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sun, 21 Aug 2022 04:25:22 +0300 Subject: [PATCH 6/6] add note --- src/compiler.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/compiler.ts b/src/compiler.ts index 891ec7699f..22246dde13 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -3503,6 +3503,9 @@ export class Compiler extends DiagnosticEmitter { // v128 to any / any to v128 // except v128 to bool + // + // NOTE:In case we would have more conversions to and from v128 type it's better + // to make these checks more individual and integrate in below flow. if ( !toType.isBooleanValue && (toType.isVectorValue || fromType.isVectorValue)