Repository navigation
Description
Problem
A widening cast from an unsigned constant sign-extends its value. Other numeric conversions accepted by check fail during constant lowering even though their local-variable equivalents work. llvm/src/codegen/consts.rs only receives LLVM integer types, limits widening through a host-sized signed value, and evaluates several cast operands using the destination type.
Reproduction
Tested at 5002a93a4a233032553aa6c6e3639d88e93c53bf on Fedora Linux 43 amd64 with LLVM 21.1.8. Build the current compiler with LLVM_SYS_211_PREFIX=/usr/lib64/llvm21 cargo build --locked --release --jobs 2.
Save as repro.wave:
const SMALL: u8 = 255;
const WIDE: u64 = SMALL as u64;
fun main() -> i32 {
if (WIDE != 255) { return 1; }
return 0;
}
target/release/wavec check repro.wave
target/release/wavec build repro.wave -O0 --emit=ir,obj,bin --out-dir /tmp/wave-audit-reproRepeat the build with -O2.
Run /tmp/wave-audit-repro/repro. Both O0/O2 return 1, rather than 0. Using equivalent local variables returns 0; widening a signed i8 = -1 also behaves correctly.
Additional executed checks at both optimization levels:
| Accepted global initializer | Build result |
|---|---|
(SMALL) as u64 |
E9002: identifier resolved to a different LLVM type |
(255 as i8) as u64 |
E9002: inner cast target does not match declaration type |
Widen a u128 constant holding 18446744073709551616 to u256 |
E9002: cannot sign-extend 128-bit constant to 256-bit |
Implicitly initialize u64 from const SMALL: u8 = 42 |
E9002: different LLVM type |
Initialize i32 from const F: f64 = 1.5; F as i32 |
E9002: cannot cast constant to integer |
Scope
Evaluate each operand with its resolved Wave type and apply the already validated explicit/implicit conversion. Cover const and static initializers, grouping, nested casts, and supported integer widths through 1024 bits. Preserve current runtime conversion policy and integer-division semantics. Character/string literal lowering is #728; the broader HIR conversion representation is #721. Difficulty: hard.
Completion criteria
- Unsigned widening preserves the value; signed widening preserves the sign.
- Accepted numeric conversions agree between global initializers and equivalent local expressions at O0/O2.
- Grouping and nested casts preserve every source/destination conversion rather than replacing inner types with the final declaration type.
- Regression coverage includes values above 64 bits and float/integer conversions without new arithmetic policy.
Activity