Skip to content

Commit 6a3e848

Browse files
authored
fix: Fix boolean oveflow casts for constants (AssemblyScript#2134)
1 parent 341cb9d commit 6a3e848

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

src/compiler.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3394,6 +3394,14 @@ export class Compiler extends DiagnosticEmitter {
33943394
var type = element.type;
33953395
this.currentType = type;
33963396
switch (type.kind) {
3397+
case TypeKind.BOOL: {
3398+
return this.module.i32(
3399+
element.constantValueKind == ConstantValueKind.INTEGER
3400+
// @ts-ignore
3401+
? <i32>i64_ne(element.constantIntegerValue, i64_zero)
3402+
: 0
3403+
);
3404+
}
33973405
case TypeKind.I8:
33983406
case TypeKind.I16: {
33993407
let shift = type.computeSmallIntegerShift(Type.i32);
@@ -3404,8 +3412,7 @@ export class Compiler extends DiagnosticEmitter {
34043412
); // recognized by canOverflow
34053413
}
34063414
case TypeKind.U8:
3407-
case TypeKind.U16:
3408-
case TypeKind.BOOL: {
3415+
case TypeKind.U16: {
34093416
let mask = element.type.computeSmallIntegerMask(Type.i32);
34103417
return this.module.i32(
34113418
element.constantValueKind == ConstantValueKind.INTEGER

tests/compiler/overflow.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,27 @@
129129
assert(val - 1 == 0xffff);
130130
}
131131

132+
// special cases
133+
{
134+
const b1 = <bool>2;
135+
assert(b1 == true);
136+
137+
const b2 = <bool>-1;
138+
assert(b2 == true);
139+
140+
const b3 = <bool>0;
141+
assert(b3 == false);
142+
143+
let b4 = <bool>2;
144+
assert(b4 == true);
145+
146+
let b5 = <bool>-1;
147+
assert(b5 == true);
148+
149+
let b6 = <bool>0;
150+
assert(b6 == false);
151+
}
152+
132153
{
133154
// regression #2131
134155
const a: u32 = 65;

tests/compiler/overflow.untouched.wat

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,64 @@
677677
call $~lib/builtins/abort
678678
unreachable
679679
end
680+
i32.const 1
681+
i32.const 1
682+
i32.eq
683+
drop
684+
i32.const 1
685+
i32.const 1
686+
i32.eq
687+
drop
688+
i32.const 0
689+
i32.const 0
690+
i32.eq
691+
drop
692+
i32.const 2
693+
local.set $0
694+
local.get $0
695+
i32.const 0
696+
i32.ne
697+
i32.const 1
698+
i32.eq
699+
i32.eqz
700+
if
701+
i32.const 0
702+
i32.const 32
703+
i32.const 144
704+
i32.const 3
705+
call $~lib/builtins/abort
706+
unreachable
707+
end
708+
i32.const -1
709+
local.set $1
710+
local.get $1
711+
i32.const 0
712+
i32.ne
713+
i32.const 1
714+
i32.eq
715+
i32.eqz
716+
if
717+
i32.const 0
718+
i32.const 32
719+
i32.const 147
720+
i32.const 3
721+
call $~lib/builtins/abort
722+
unreachable
723+
end
724+
i32.const 0
725+
local.set $2
726+
local.get $2
727+
i32.const 0
728+
i32.eq
729+
i32.eqz
730+
if
731+
i32.const 0
732+
i32.const 32
733+
i32.const 150
734+
i32.const 3
735+
call $~lib/builtins/abort
736+
unreachable
737+
end
680738
i32.const 65
681739
i32.const 63457
682740
i32.const 504
@@ -690,15 +748,15 @@
690748
i32.const 65535
691749
i32.and
692750
i32.add
693-
local.set $0
694-
local.get $0
751+
local.set $2
752+
local.get $2
695753
i32.const 65597
696754
i32.eq
697755
i32.eqz
698756
if
699757
i32.const 0
700758
i32.const 32
701-
i32.const 138
759+
i32.const 159
702760
i32.const 3
703761
call $~lib/builtins/abort
704762
unreachable

0 commit comments

Comments
 (0)