Skip to content

fix: Disallow "any to v128" or "v128 to any" conversions #2442

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 7 commits into from
Aug 21, 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
22 changes: 22 additions & 0 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3495,6 +3495,28 @@ 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
//
// 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)
) {
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(
Expand Down
10 changes: 10 additions & 0 deletions tests/compiler/simd-errors.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
15 changes: 15 additions & 0 deletions tests/compiler/simd-errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// f32
{
let a = f32x4.splat(0);
let b: f32 = 0;
v128.add<f32>(a, b);
}

// i32
{
let a: i32 = 0;
let b = i32x4.splat(0);
v128.sub<i32>(a, b);
}

ERROR("EOF");