fix(gen-zig): narrowing unsigned cast lowers to @truncate, not @intCast - #1999
Merged
Conversation
t27 'as' truncates on a narrowing integer cast (Rust semantics), but the Zig backend always emitted the CHECKED @intcast, which panics in safe builds when the value does not fit. tri_settle::reward_weighted extracts the u32 halves of a u64 (weighted_total as u32, (weighted_total >> 32) as u32); on a 2^32-multiple input @intcast aborts instead of truncating. Codegen(Zig) now carries a param/typed-local type map, scoped per fn (populated from the fn signature and typed 'let' locals). ExprCast emits @truncate when the source integer is provably a WIDER UNSIGNED int than the unsigned target; signed narrowing and widening/unknown stay on @intcast (unchanged). The unsigned-only restriction is deliberate: Zig's @truncate rejects a signed source (e.g. 'i16 as u8'), which a broader rule broke -- caught by ast-check on link_quality_monitor. tri-net corpus: 16 gen/zig regenerate (narrowing sites flip to @truncate); all pass zig ast-check + zig test. icarus / C / Rust are untouched (gen-zig only). t27 unit suite 1537 passed / 0 failed. FROZEN_HASH resealed. Refs #1948 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
|
📓 NotebookLM Notebook linked to this PR
This notebook contains session context, decisions, and artifacts for this work. |
Contributor
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.
t27
astruncates on a narrowing integer cast (Rust semantics), but the Zig backend always emitted the checked@intCast, which panics in safe builds when the value doesn't fit.tri_settle::reward_weightedextracts the u32 halves of a u64 (weighted_total as u32,(weighted_total >> 32) as u32); on a 2³²-multiple input@intCastaborts instead of truncating.Fix
Codegen(Zig) now carries a param/typed-local type map, scoped per fn (populated from the fn signature and typedletlocals).ExprCastemits@truncatewhen the source integer is provably a wider unsigned int than the (unsigned) target; signed narrowing and widening/unknown stay on@intCast(unchanged).The unsigned-only restriction is deliberate: Zig's
@truncaterejects a signed source (e.g.i16 as u8), which a broader rule broke — caught by ast-check onlink_quality_monitor.Validation
@truncate); all passzig ast-check+zig testA companion tri-net PR regenerates the 16 affected gen/zig.
Refs #1948
🤖 Generated with Claude Code