Commit f12e02f9

f12e02f911efMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

codegen: don't raise NoMethodError for int/poly/nil recvs

d541ecb changed the unresolved-call fallback from emitting 0 to raising NoMethodError so genuine "x.unknown_method" sites surface the bug instead of silently producing 0. This broke optcarrot: patterns like

@hook = nil
...
if @hook
  @hook.fire(...)
end

infer @hook as int/poly when the assigned class isn't pinnable. The guarded call then raises at runtime even though the guard keeps it dynamically safe.

Gate the raise on a concrete recv type (string, array, hash, obj_*); fall back to silent 0 on int/poly/nil where the dispatch failure usually reflects unpinnable type inference rather than a real typo. The warning still fires so the diagnostic surface is unchanged.

Refs #727

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

Details

Commit
f12e02f911ef
Parent
636ac4de0853

spinel_codegen.rbmodifiedView file

@@ -16311,9 +16311,21 @@
1631116311 # discards the times call) keep working, while genuine
1631216312 # "x.unknown_method" sites surface the bug instead of returning
1631316313 # a silent zero. Issue #727.
16314+ #
16315+ # Gate the raise on a *concrete* receiver type. "int" and "poly"
16316+ # often arise when an ivar / param's type couldn't be inferred
16317+ # (e.g. `@x = nil` later assigned an obj at runtime); raising
16318+ # would break the silent-fallback pattern optcarrot etc. depend
16319+ # on (`@hook && @hook.fire(...)` where @hook starts nil and is
16320+ # later assigned). Keep the warn for visibility, fall back to
16321+ # silent 0 on int/poly recvs.
1631416322 warn_unresolved_call(mname, base_type(recv_type))
16323+ rb_unr = base_type(recv_type)
16324+ if rb_unr == "int" || rb_unr == "poly" || rb_unr == "nil"
16325+ return "0"
16326+ end
1631516327 @needs_exc_class_hierarchy = 1
16316- "({ sp_raise_cls(\"NoMethodError\", \"undefined method '" + mname + "' for " + base_type(recv_type) + "\"); (mrb_int)0; })"
16328+ "({ sp_raise_cls(\"NoMethodError\", \"undefined method '" + mname + "' for " + rb_unr + "\"); (mrb_int)0; })"
1631716329 end
1631816330
1631916331 def compile_block_given_expr