Skip to content

fix: wrap small integers before casting them to bool - #3065

Open
SwastikTripathi wants to merge 1 commit into
AssemblyScript:mainfrom
SwastikTripathi:fix/small-int-to-bool-cast
Open

SwastikTripathi wants to merge 1 commit into
AssemblyScript:mainfrom
SwastikTripathi:fix/small-int-to-bool-cast

Conversation

@SwastikTripathi

Copy link
Copy Markdown

Fixes #3057.

Changes proposed in this pull request:
An explicit cast of a small integer (i8, u8, i16, u16) to bool (<bool>x, x as bool, bool(x)) now clears the bits above the small type before testing the value. Before, convertExpression only wrapped small integers when converting to a larger type, and bool (size 1) never qualified, so a value that overflowed to 0 in its own type still tested as true:

function f(a: u8): bool {
  let c: u8 = a + 1;
  return <bool>c;
}
f(255); // true before, false now (c is 0)

The conditional form (c ? true : false) already went through makeIsTrueish, which wraps, so only the explicit cast was affected.

Added cases to tests/compiler/bool.ts for u8, i8, u16 and i16 values that wrap to 0 (and some that don't). With the old compiler the module aborts on the first new assertion; with the fix it runs. Regenerated the bool and cast fixtures; cast.debug.wat changes because it contains such casts (an i32.and/sign-extension now precedes the i32.ne). npm run test:compiler passes.

  • I've read the contributing guidelines
  • I've added my name and email to the NOTICE file

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Explicit bool cast of u8/i8/u16/i16 does not truncate high bits

1 participant