Commit a8d909e5
a8d909e5a878Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): #314 skip Integer#[] bit-extract when index is non-int
Integer#[idx] (bit indexing) fired unconditionally on any int-typed
receiver, regardless of the index's type. Common shape that tripped:
def self.from_raw(row)
instance.id = row[:id] || 0
...
end
row is an unpinned param whose type defaults to "int". row[:id]
then lowered to (row >> SPS_id) & 1 — but SPS_id is the symbol's
interned id (commonly >= 64), so the shift is undefined behavior and
gcc/clang flag it with -Wshift-count-overflow.
Gate the bit-extract on the index's inferred type. A non-int / non-poly index (typically a Symbol or String literal) is hash / array subscript, never integer bit indexing — fall through to the unresolved-call placeholder so downstream sees the right diagnostic instead of a warning + garbage value.
Surfaced via the Roundhouse archive: make spinel-test-build C
warnings drop from 159 to 54, of which the 24 shift-count-overflow
instances drop to 1 (the remaining one is a legitimate negative-shift
edge case in unrelated code). All 4 archive test binaries continue
to build cleanly with 0 errors. 340/340 spinel tests + bootstrap
stable.
Refs #314
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- a8d909e5a878
- Parent
- cf52df066aa5
spinel_codegen.rbmodifiedView file
if args_id >= 0 aargs = get_args(args_id) if aargs.length > 0 # Skip bit-extract when the index is a non-int — `row[:id]` # / `row["id"]` is hash subscript, not bit indexing on an # integer. Without this gate, a poly/hash-receiver param # that defaulted to "int" (no upstream pinning) routed # `row[:id]` to `(row >> SPS_id) & 1`, shifting an int by # the symbol's interned id (>= 64) which is undefined # behavior under -Wshift-count-overflow. Surfaces in # Roundhouse's `def self.from_raw(row); row[:id] || 0; # end` where `row` is unpinned. Issue #314 (Roundhouse # warnings family). idx_t = infer_type(aargs[0]) if idx_t != "int" && idx_t != "poly" return "" end idx = compile_expr(aargs[0]) return "(((" + rc + ") >> (" + idx + ")) & 1)" endtest/int_bracket_skip_sym_idx.rbaddedView file
Loading diff…
test/int_bracket_skip_sym_idx.rb.expectedaddedView file
Loading diff…