Commit 17cffcb5

17cffcb5507bMerged

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

Description

fix(codegen,analyze): primitive .class + respond_to? + middle splat param

Three independent issues with shared root cause: the codegen arms for .class, respond_to?, and rest-param handling only fired for obj-typed receivers (or the trivial rest-only case).

  • .class (#715): added a primitive arm in compile_call that emits (sp_Class){<builtin_id>} for int / bigint / float / string / symbol / nil / range / time / array / hash / proc / lambda. Bool emits a ternary so the runtime cls_id picks TrueClass vs FalseClass. analyze.rb mirrors with a "class" return type so callers can read the result as an sp_Class.

  • respond_to? (#716): primitive recv goes through a conservative allowlist (to_s/to_i/to_f/inspect/class/nil?/is_a?/kind_of?/ instance_of?/==/!=/hash/respond_to?/frozen?/freeze/dup/clone/ tap/then). Returns TRUE for known names. Compile-time can't introspect each primitive's method table fully, so the allowlist trades exhaustive accuracy for the common cases users actually probe.

  • middle splat param def f(a, *b, c) (#710): the rest-arg loop used to greedily consume positional_ids[k..end] into the splat tmp, then re-enter the outer loop at k+1 reading ptypes[k+1..] from the now-empty tail. Two changes:

    • leave the trailing positional_ids[splat_end..] for the fixed params after the rest slot (trailing_after_rest = pnames.length
      • 1 - rest_param_idx);
    • rebuild positional_ids with [pre-splat, placeholder, trailing] so the outer loop's k+1.. indexing keeps aligning with the remaining ptypes. Also fire treat_as_rest unconditionally when this is a middle rest slot (is_last_param == 0) so g(100, 200) with def g(a, *b, c) produces an empty *b instead of dropping 200 into b's slot.

Regression tests: test/class_of_primitive.rb - .class for 9 primitives test/respond_to_primitive.rb - respond_to? allowlist test/middle_splat_param.rb - 4 middle-splat shapes

454+2 -> 456 tests pass; optcarrot checksum 59662.

Closes #710 Closes #715 Closes #716

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
17cffcb5507b
Parent
c80632fb44fb

spinel_analyze.rbmodifiedView file

@@ -3903,6 +3903,17 @@
39033903 if is_obj_type(rt) == 1
39043904 return "class"
39053905 end
3906+ # Primitive .class -- Integer / Float / String / Symbol /
3907+ # NilClass / TrueClass / FalseClass / Range / Time / Array /
3908+ # Hash / Proc -- all return a `class` value. Issue #715.
3909+ bt_pc = base_type(rt)
3910+ if bt_pc == "int" || bt_pc == "bigint" || bt_pc == "float" ||
3911+ bt_pc == "string" || bt_pc == "mutable_str" || bt_pc == "symbol" ||
3912+ bt_pc == "bool" || bt_pc == "nil" || bt_pc == "range" || bt_pc == "time" ||
3913+ is_array_type(bt_pc) == 1 || is_hash_type(bt_pc) == 1 ||
3914+ bt_pc == "proc" || bt_pc == "lambda"
3915+ return "class"
3916+ end
39063917 end
39073918 end
39083919 # `<obj>.class.<cmeth>` chained dispatch. When the