Commit 34ff4c89

34ff4c895448Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified

Description

fix(analyze,codegen,runtime): body-usage widen int|nil param to poly for .include?

Sam Ruby's #558. Sibling of #552 for the other ambiguous- receiver method that exists on String / Array / Hash / Range but NOT on Integer. Pre-fix cols.include?(:updated_at) on an int/nil-defaulted param produced "cannot resolve call to 'include?' on int (emitting 0)" and silently returned 0, breaking ActiveRecord::Base#fill_timestamps and view-helper opts.include?(:disabled) checks in roundhouse's framework runtime.

Three pieces:

  1. is_lengthlike_only_method (analyze) now matches include? alongside length / size / empty?. Same body proof: a param the body calls include? on is provably not an Integer, so widening to flat poly is sound.

  2. emit_poly_builtin_dispatch (codegen) gains include? arms keyed by the arg type. Array variants use the existing sp_*Array_include helpers (sym_array shares sp_IntArray_include since sym ids store as mrb_int); Hash variants alias include? -> has_key? per CRuby. SP_TAG_STR include? in compile_poly_method_call fires for string args (CRuby's String#include? takes a substring; non-string args TypeError at runtime, so the result temp stays at its default).

  3. New sp_PolyArray_include runtime helper. Iterates the sp_RbVal array and compares via sp_poly_eq, mirroring the *Array_include pattern but with poly element type.

  4. scan_for_dig_usage's pre-scan extended to also arm @needs_sym_int_hash / @needs_sym_str_hash / @needs_sym_poly_hash / @needs_str_poly_hash when an include? call appears with a symbol or string arg. Mirrors the dig-arm coverage from #555 case 07 since the typedef-emit pass runs before compile-time @needs_ flags would otherwise flip.

Verified: 527 tests pass (including a new discriminator that prints false/false/false/false/false pre-fix and false/true/false/true/false post-fix), optcarrot checksum 59662 preserved.

Closes #558

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
34ff4c895448
Parent
33a04d660349

lib/sp_runtime.hmodifiedView file

@@ -1452,6 +1452,13 @@
14521452 }
14531453 return TRUE;
14541454 }
1455+static mrb_bool sp_PolyArray_include(sp_PolyArray *a, sp_RbVal v) {
1456+ if (!a) return FALSE;
1457+ for (mrb_int i = 0; i < a->len; i++) {
1458+ if (sp_poly_eq(a->data[i], v)) return TRUE;
1459+ }
1460+ return FALSE;
1461+}
14551462
14561463 /* Mark the embedded GC reference inside an sp_RbVal (string or obj).
14571464 Used as the scan hook for containers that store polymorphic values. */