Skip to content

Reject or correctly widen oversized literals in comparison arithmetic #702

Closed

Description

@LunaStev

A literal-only arithmetic expression used in a wide comparison is lowered at the default i32 width and then widened. A literal that does not fit i32 is silently truncated, which can turn an upper-bound check into an almost-unbounded check.

Reproduced on Linux amd64 at b71aaf20 (PR #701), at both O0 and O2:

fun main() -> i32 {
    var value: u64 = 4294967265;
    if (value <= 4294967296 - 32) {
        return 1;
    }
    return 0;
}

The executable returns 1. The generated O0 IR compares against -32 as an unsigned i64:

%cmptmp = icmp ule i64 %load_value, -32

A typed u64 constant for the bound, or a u64 variable initialized from the same subtraction, preserves 4294967264. The discrepancy was found while implementing WASM allocation bounds; that allocator uses an explicitly typed bound and is not affected by this reproducer.

Please decide and enforce the intended contextual integer arithmetic rule: either evaluate this expression in a permitted wide type or reject an out-of-range default-width literal with a source diagnostic. Silently truncating the literal is not acceptable. Preserve the existing rule that integer division does not become floating division merely because its result is converted to a float.

Acceptance:

  • Wide comparison bounds containing literal-only arithmetic do not silently truncate oversized literals.
  • Type checking and codegen agree on arithmetic widths, signedness and conversions.
  • Regression cases cover comparison operands, grouped arithmetic, calls/returns, signed and unsigned types, and O0/O2.
  • Existing contextual conversion and integer-division tests remain green.

Related to #681's contextual conversion area, but this is integer operand typing rather than float-to-integer destination signedness. It requires frontend/codegen reasoning and is not labeled good first issue.

Activity

  1. added
    bug
    help wanted
    needs testing
    on Sep 23, 2026
  2. self-assigned this
    on Sep 24, 2026
  3. added a commit that references this issue on Sep 24, 2026
    01955d7
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