Skip to content

Preserve numeric conversion semantics in constant initializers #733

Closed

Description

@LunaStev

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-repro

Repeat 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

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

Metadata

Metadata

Assignees

Labels

bughelp wantedneeds testing

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions