Skip to content

Define boolean casts independently from integer truncation #748

Closed

Description

@LunaStev

Problem

Boolean casts currently reuse integer narrowing: integer 2 becomes false, while 2.0 as bool lowers to fptoui double ... to i1 and produces poison after optimization. Boolean conversion needs an explicit semantic contract.

Starting points: front/parser/src/hir/conversions.rs, llvm/src/codegen/conversions.rs.

Reproduction

Tested at 6257ba3eb4b9f1ab6c247a920c6e0c4b5d699331 on Linux amd64 with LLVM 21.1.8 and a compiler built from that checkout. Build it with LLVM_SYS_211_PREFIX=/usr/lib64/llvm21 cargo build --locked --jobs 2.

Save as repro.wave:

fun main() -> i32 { var n: f64 = 2.0; if (n as bool) { return 0; } return 1; }
target/debug/wavec build repro.wave -O0 --emit=ir,bin --out-dir /tmp/wave-issue-O0
/tmp/wave-issue-O0/repro
target/debug/wavec build repro.wave -O2 --emit=ir,bin --out-dir /tmp/wave-issue-O2
/tmp/wave-issue-O2/repro

Observed O0=1, O2=40. O0 contains fptoui double ... to i1; O2 contains ret i32 poison. A corresponding i32 value of 2 casts to false at both levels. NaN also produced differing results. The observed optimized exit code is not stable behavior.

Scope

Define casts to bool independently from ordinary integer-width conversions. Preserve ordered ConversionInfo and share the chosen behavior between constants and runtime. This is separate from bool equality (#714) and input parsing (#727).

Difficulty: hard.

Completion criteria

  • Cover integer 0/1/2/-1, floating ±0, fractions, NaN and infinities.
  • Specify permitted source categories, including whether pointer-to-bool is allowed.
  • Represent the chosen semantics in HIR and verify equivalent O0/O2 results for defined inputs.

Design decision needed: Decide whether bool casts mean nonzero testing, low-bit extraction or only accepting 0/1, and decide floating NaN/infinity behavior.

Please mention @LunaStev to discuss the design before starting implementation.

Activity

  1. added
    bug
    help wanted
    discussion
    needs testing
    on Sep 25, 2026
  2. self-assigned this
    on Sep 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugdiscussionhelp wantedneeds testing

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions