Repository navigation
Conversation
…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`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #725
Summary
Previously,
HYB-E6002constant-overflow checks only validated untyped constants against a target's declared type or the defaultint/float64type, andconst_int_fold_withdid not fold fixed-width integer conversion calls (u8(255),i64(1)) or check the folded result of typed constant expressions (top + 1whereconst top u8 = 255,u8(255) + 1,-u8(1),u8(1) << 9,i64(1) << 63,-(i64(1) << 63)). Accepted programs therefore emitted Go that failedgo buildwithconstant ... overflows ....Changes
compiler/crates/hy-check/src/const_overflow.rs:Checker::const_int_fold_exprto fold integer constant expressions including local/fileconstbindings and 1-arg fixed-width integer conversions (u8(...),i64(...), etc., preserving SPEC X4 two's-complement wrapping on sub-64-bit conversions).Checker::typed_const_expr_typeto determine when a constant expression carries an explicit fixed-width integer type in Go (through typedconstdeclarations, integer conversion calls, unary-, shifts<</>>, and binary arithmetic/bitwise operations).intcheck incheck_default_int_overflowwhen an initializer is already a typed constant (sou64(1) << 63stays accepted asu64), and deduplicateHYB-E6002diagnostics per expression span.compiler/crates/hy-check/src/emit_guard.rs:<<and>>) and unary negation (-u8(1)) against their constant type range viacheck_const_overflow_init, suppressing cascading outer diagnostics when an inner subexpression already failed.compiler/crates/hy-check/src/shape.rs:Expr::Unary { op: UnOp::Neg }andExpr::Binary { op: BinOp::Shl | BinOp::Shr }inty_of_expr.compiler/crates/hy-check/tests/typed_const_arith_overflow_issue_725.rs:const top u8 = 255; top + 1,i64(1) << 63,u8(0) - 1) passes hyb check, then fails go build (X1/X2) #725, and valid typed/untyped controls (u64(1) << 63,(-i64(1)) << 63,(1 << 63) - 1,-(1 << 63),u8(100 + 200), runtime variable wrapping).Verification
cargo test -p hy-diag -p hy-syntax -p hy-emit -p hy-check