Skip to content

Commit b0ca388

Browse files
authored
fix: Disallow "any to v128" or "v128 to any" conversions (#2442)
1 parent f7beaac commit b0ca388

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/compiler.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3495,6 +3495,28 @@ export class Compiler extends DiagnosticEmitter {
34953495
// not dealing with references from here on
34963496
assert(!fromType.isReference && !toType.isReference);
34973497

3498+
// Early return if we have same types
3499+
if (toType.kind == fromType.kind) {
3500+
this.currentType = toType;
3501+
return expr;
3502+
}
3503+
3504+
// v128 to any / any to v128
3505+
// except v128 to bool
3506+
//
3507+
// NOTE:In case we would have more conversions to and from v128 type it's better
3508+
// to make these checks more individual and integrate in below flow.
3509+
if (
3510+
!toType.isBooleanValue &&
3511+
(toType.isVectorValue || fromType.isVectorValue)
3512+
) {
3513+
this.error(
3514+
DiagnosticCode.Type_0_is_not_assignable_to_type_1,
3515+
reportNode.range, fromType.toString(), toType.toString()
3516+
);
3517+
return module.unreachable();
3518+
}
3519+
34983520
if (!fromType.isAssignableTo(toType)) {
34993521
if (!explicit) {
35003522
this.error(

tests/compiler/simd-errors.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"asc_flags": [
3+
"--enable", "simd"
4+
],
5+
"stderr": [
6+
"TS2322: Type 'f32' is not assignable to type 'v128'.",
7+
"TS2322: Type 'i32' is not assignable to type 'v128'.",
8+
"EOF"
9+
]
10+
}

tests/compiler/simd-errors.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// f32
2+
{
3+
let a = f32x4.splat(0);
4+
let b: f32 = 0;
5+
v128.add<f32>(a, b);
6+
}
7+
8+
// i32
9+
{
10+
let a: i32 = 0;
11+
let b = i32x4.splat(0);
12+
v128.sub<i32>(a, b);
13+
}
14+
15+
ERROR("EOF");

0 commit comments

Comments
 (0)