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

@@ -23264,6 +23264,20 @@
2326423264 if args_id >= 0
2326523265 aargs = get_args(args_id)
2326623266 if aargs.length > 0
23267+ # Skip bit-extract when the index is a non-int — `row[:id]`
23268+ # / `row["id"]` is hash subscript, not bit indexing on an
23269+ # integer. Without this gate, a poly/hash-receiver param
23270+ # that defaulted to "int" (no upstream pinning) routed
23271+ # `row[:id]` to `(row >> SPS_id) & 1`, shifting an int by
23272+ # the symbol's interned id (>= 64) which is undefined
23273+ # behavior under -Wshift-count-overflow. Surfaces in
23274+ # Roundhouse's `def self.from_raw(row); row[:id] || 0;
23275+ # end` where `row` is unpinned. Issue #314 (Roundhouse
23276+ # warnings family).
23277+ idx_t = infer_type(aargs[0])
23278+ if idx_t != "int" && idx_t != "poly"
23279+ return ""
23280+ end
2326723281 idx = compile_expr(aargs[0])
2326823282 return "(((" + rc + ") >> (" + idx + ")) & 1)"
2326923283 end