Skip to content

fix(hy-check): reject typed constant arithmetic and shift overflow with HYB-E6002 (fixes #725) - #737

Open
aazminu wants to merge 2 commits into
limitlessprojects:mainfrom
aazminu:fix-725-typed-const-overflow
Open

aazminu wants to merge 2 commits into
limitlessprojects:mainfrom
aazminu:fix-725-typed-const-overflow

Conversation

@aazminu

@aazminu aazminu commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Fixes #725

Summary

Previously, HYB-E6002 constant-overflow checks only validated untyped constants against a target's declared type or the default int/float64 type, and const_int_fold_with did not fold fixed-width integer conversion calls (u8(255), i64(1)) or check the folded result of typed constant expressions (top + 1 where const top u8 = 255, u8(255) + 1, -u8(1), u8(1) << 9, i64(1) << 63, -(i64(1) << 63)). Accepted programs therefore emitted Go that failed go build with constant ... overflows ....

Changes

  • compiler/crates/hy-check/src/const_overflow.rs:
    • Add Checker::const_int_fold_expr to fold integer constant expressions including local/file const bindings and 1-arg fixed-width integer conversions (u8(...), i64(...), etc., preserving SPEC X4 two's-complement wrapping on sub-64-bit conversions).
    • Add Checker::typed_const_expr_type to determine when a constant expression carries an explicit fixed-width integer type in Go (through typed const declarations, integer conversion calls, unary -, shifts <</>>, and binary arithmetic/bitwise operations).
    • Skip the untyped default-int check in check_default_int_overflow when an initializer is already a typed constant (so u64(1) << 63 stays accepted as u64), and deduplicate HYB-E6002 diagnostics per expression span.
  • compiler/crates/hy-check/src/emit_guard.rs:
    • Check typed constant binary expressions (including << and >>) and unary negation (-u8(1)) against their constant type range via check_const_overflow_init, suppressing cascading outer diagnostics when an inner subexpression already failed.
  • compiler/crates/hy-check/src/shape.rs:
    • Propagate operand types through Expr::Unary { op: UnOp::Neg } and Expr::Binary { op: BinOp::Shl | BinOp::Shr } in ty_of_expr.
  • compiler/crates/hy-check/tests/typed_const_arith_overflow_issue_725.rs:

Verification

  • cargo test -p hy-diag -p hy-syntax -p hy-emit -p hy-check

…th HYB-E6002 (fixes limitlessprojects#725)

- Extend constant integer folding (`const_int_fold_expr`) to resolve local
  and file-level `const` bindings (respecting declared fixed-width ranges)
  and fixed-width integer conversion calls (`u8(255)`, `i64(1)`, etc.,
  applying SPEC X4 two's-complement wrap on sub-64-bit conversions).
- Track the explicit integer type of typed constant expressions
  (`typed_const_expr_type`) across `const` references, conversion calls,
  unary negation (`-u8(1)`), shifts (`i64(1) << 63`, `u8(1) << 9`), and
  binary arithmetic/bitwise operations (`top + 1`, `u8(255) + 1`,
  `i8(-128) / -1`).
- Validate typed constant unary and binary expressions in `emit_guard.rs`
  against their constant type range (`HYB-E6002`), while skipping the
  default-`int` untyped overflow check when an initializer is already a
  typed constant (e.g. `u64(1) << 63`).
- Propagate operand types through `Unary::Neg` and `BinOp::Shl`/`Shr` in
  `ty_of_expr`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant