Skip to content

fix: Fix boolean oveflow casts for constants #2134

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 1 commit into from
Nov 11, 2021
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
11 changes: 9 additions & 2 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3394,6 +3394,14 @@ export class Compiler extends DiagnosticEmitter {
var type = element.type;
this.currentType = type;
switch (type.kind) {
case TypeKind.BOOL: {
return this.module.i32(
element.constantValueKind == ConstantValueKind.INTEGER
// @ts-ignore
? <i32>i64_ne(element.constantIntegerValue, i64_zero)
: 0
);
}
case TypeKind.I8:
case TypeKind.I16: {
let shift = type.computeSmallIntegerShift(Type.i32);
Expand All @@ -3404,8 +3412,7 @@ export class Compiler extends DiagnosticEmitter {
); // recognized by canOverflow
}
case TypeKind.U8:
case TypeKind.U16:
case TypeKind.BOOL: {
case TypeKind.U16: {
let mask = element.type.computeSmallIntegerMask(Type.i32);
return this.module.i32(
element.constantValueKind == ConstantValueKind.INTEGER
Expand Down
21 changes: 21 additions & 0 deletions tests/compiler/overflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,27 @@
assert(val - 1 == 0xffff);
}

// special cases
{
const b1 = <bool>2;
assert(b1 == true);

const b2 = <bool>-1;
assert(b2 == true);

const b3 = <bool>0;
assert(b3 == false);

let b4 = <bool>2;
assert(b4 == true);

let b5 = <bool>-1;
assert(b5 == true);

let b6 = <bool>0;
assert(b6 == false);
}

{
// regression #2131
const a: u32 = 65;
Expand Down
64 changes: 61 additions & 3 deletions tests/compiler/overflow.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,64 @@
call $~lib/builtins/abort
unreachable
end
i32.const 1
i32.const 1
i32.eq
drop
i32.const 1
i32.const 1
i32.eq
drop
i32.const 0
i32.const 0
i32.eq
drop
i32.const 2
local.set $0
local.get $0
i32.const 0
i32.ne
i32.const 1
i32.eq
i32.eqz
if
i32.const 0
i32.const 32
i32.const 144
i32.const 3
call $~lib/builtins/abort
unreachable
end
i32.const -1
local.set $1
local.get $1
i32.const 0
i32.ne
i32.const 1
i32.eq
i32.eqz
if
i32.const 0
i32.const 32
i32.const 147
i32.const 3
call $~lib/builtins/abort
unreachable
end
i32.const 0
local.set $2
local.get $2
i32.const 0
i32.eq
i32.eqz
if
i32.const 0
i32.const 32
i32.const 150
i32.const 3
call $~lib/builtins/abort
unreachable
end
i32.const 65
i32.const 63457
i32.const 504
Expand All @@ -690,15 +748,15 @@
i32.const 65535
i32.and
i32.add
local.set $0
local.get $0
local.set $2
local.get $2
i32.const 65597
i32.eq
i32.eqz
if
i32.const 0
i32.const 32
i32.const 138
i32.const 159
i32.const 3
call $~lib/builtins/abort
unreachable
Expand Down