Repository navigation
Description
Problem
Semantic validation permits an already typed floating-point value to be assigned implicitly to an Int or Uint destination. The shared coerce_basic_value function nevertheless lowers every float-to-integer conversion with build_float_to_signed_int.
LLVM integer types do not encode signedness, and this helper currently receives only source signedness. A valid value in the upper half of an unsigned destination's range is therefore converted with fptosi rather than fptoui. The explicit as path already distinguishes an unsigned destination, so implicit and explicit conversions to the same Wave type disagree.
Code references:
- front/parser/src/verification.rs — implicit assignability rules
- llvm/src/statement/variable.rs — coerce_basic_value and initializer coercion
- llvm/src/expression/rvalue/cast.rs — signed/unsigned explicit float conversion
- LLVM reference — fptosi and fptoui
Source-derived regression
Save as implicit_float_unsigned.wave:
fun main() -> i32 {
var source: f64 = 3000000000.0;
var implicit_value: u32 = source;
var explicit_value: u32 = source as u32;
var expected: u32 = 3000000000;
if (explicit_value != expected) {
return 1;
}
if (implicit_value != expected) {
return 2;
}
return 0;
}
Expected: both conversions produce 3000000000 and the program exits 0. The source is a typed variable, avoiding a separate question about implicit assignment of an unsuffixed float literal.
The current helper selects fptosi double %source to i32, while the explicit cast selects fptoui. The example is within the u32 range but outside the signed i32 range; LLVM defines the out-of-range signed conversion as producing poison. It is not valid to depend on a particular machine instruction's observed result.
The instruction-selection mismatch is derived from the Wave source. A separate hand-written LLVM reduction folded the signed constant conversion to poison and preserved the unsigned conversion, but it was not Wave-generated IR. The Wave regression still requires an actual compiler run.
Expected behavior and scope
Preserve destination signedness through the shared coercion API and its callers. Use the corresponding unsigned conversion for unsigned Wave destinations and retain the signed conversion for signed destinations.
Review initialization, assignment, return, argument, and aggregate-element paths that reuse the helper, supplying destination information consistently where the frontend already permits the conversion. Do not infer destination signedness from the source or from an LLVM iN type.
This issue does not broaden implicit conversions, introduce saturating casts, or define new behavior for NaN, infinities, or values outside the destination's actual range. Keep explicit casts and existing integer widening/narrowing policies unchanged.
Completion criteria
-
wavec check implicit_float_unsigned.waveaccepts the regression, and actual generated IR uses unsigned conversion for its implicitu32destination. - The regression exits 0 at unoptimized and optimized settings with the supported LLVM toolchain.
- Test typed
f32/f64inputs and supported unsigned widths, including exactly representable values above each corresponding signed maximum but within the unsigned range. - Include zero, ordinary positive fractions with the existing truncation-toward-zero behavior, and in-range negative values for signed destinations.
- Exercise initialization, assignment, return, argument, and aggregate-element contexts wherever the current frontend permits them.
- Verify nonconstant conversion functions so constant folding cannot hide incorrect instruction selection.
- Existing explicit casts, integer conversions, and disallowed implicit-conversion diagnostics remain covered.
- Add compiler IR regressions and runnable Wave cases rather than relying only on a hand-written LLVM model.
Difficulty: medium.
Activity
Fix typed codegen and expand portable std capabilities (#701)