Commit c802d9d0

c802d9d01d87Merged

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

Description

fix(codegen): include method context in unresolved-call/const warnings

Closes #490. A method whose params can't be constrained by any reachable caller defaulted to mrb_int, and any op invalid on Int (e.g. xs.length where xs was the unanchored param) then surfaced as

warning: cannot resolve call to 'length' on int (emitting 0)

— with no method name or location, so the user had to bisect defs to find the source. Reported against library entry points (tinynn embed_lookup / embed_back) where each build printed one opaque warning per dead-code-only branch.

Two changes in warn_unresolved_call (and the parallel warn_unresolved_const):

  1. Build a context string from @current_class_idx + @current_method_name. Instance method → Foo#bar, class method → Foo.bar, module class method (stored under @meth_names as Util_cls_size_of) → Util.size_of, no def scope → (top level).

  2. Include the context in both the printed line AND the dedupe key. The prior key collapsed "same mname + recv_tag" hits across distinct defs into one line, which was exactly the case that turned library-method-with-no-caller warnings opaque — one cryptic line covered every site that shared the shape.

The emitted C is unchanged; the warning is informational and codegen still emits 0 for the call's expression, matching the historical silent-no-op behaviour. Test asserts only program output (stderr is dropped by the harness) so it doubles as a "library method with no caller still compiles" regression guard.

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

Details

Commit
c802d9d01d87
Parent
bd6271237cae

spinel_codegen.rbmodifiedView file

@@ -4082,15 +4082,49 @@
40824082 0
40834083 end
40844084
4085+ # Identifies the method body we're currently emitting, formatted for
4086+ # diagnostic messages. Library-style methods with no concrete caller
4087+ # default their params to `mrb_int` and surface as a flood of
4088+ # "cannot resolve call to ... on int" lines; without context the
4089+ # user can't tell which def the warning came from. Returns
4090+ # "Foo#bar" for an instance method, "Foo.bar" for a class method
4091+ # (incl. module class methods stored as @meth_names with a
4092+ # `<Mod>_cls_<m>` form), or "(top level)" outside any def.
4093+ def current_method_context_str
4094+ if @current_class_idx >= 0 && @current_class_idx < @cls_names.length
4095+ sep = "."
4096+ if @current_method_has_self == 1
4097+ sep = "#"
4098+ end
4099+ return @cls_names[@current_class_idx] + sep + @current_method_name
4100+ end
4101+ if @current_method_name != ""
4102+ ix_cls = @current_method_name.index("_cls_")
4103+ # CRuby returns nil when not found; spinel runtime returns -1.
4104+ if ix_cls != nil && ix_cls > 0
4105+ return @current_method_name[0, ix_cls] + "." +
4106+ @current_method_name[ix_cls + 5, @current_method_name.length - (ix_cls + 5)]
4107+ end
4108+ return @current_method_name
4109+ end
4110+ "(top level)"
4111+ end
4112+
40854113 # Print a stderr warning the first time we see an unresolved call to
4086- # `mname` with the given receiver-type tag. Subsequent identical
4087- # warnings are suppressed so a silent-fallthrough call inside a hot
4088- # loop emits one line, not a torrent. The warning is informational
4089- # only — codegen continues and emits `0` for the call's C expression
4090- # (the historical silent-no-op behaviour) so existing tests/benches
4091- # whose outputs happen to coincide with `0` keep compiling.
4114+ # `mname` with the given receiver-type tag in the current method
4115+ # context. Subsequent identical warnings are suppressed so a
4116+ # silent-fallthrough call inside a hot loop emits one line, not a
4117+ # torrent. Dedupe key includes the method context so the same shape
4118+ # of unresolved call in two different defs each produces its own
4119+ # line — the prior key (`mname:recv_tag`) collapsed them and made
4120+ # library-method-with-no-caller diagnostics opaque. The warning is
4121+ # informational only — codegen continues and emits `0` for the
4122+ # call's C expression (the historical silent-no-op behaviour) so
4123+ # existing tests/benches whose outputs happen to coincide with `0`
4124+ # keep compiling.
40924125 def warn_unresolved_call(mname, recv_tag)
4093- key = mname + ":" + recv_tag
4126+ ctx = current_method_context_str
4127+ key = mname + ":" + recv_tag + ":" + ctx
40944128 i = 0
40954129 while i < @unresolved_call_warnings.length
40964130 if @unresolved_call_warnings[i] == key
@@ -4099,7 +4133,7 @@
40994133 i = i + 1
41004134 end
41014135 @unresolved_call_warnings.push(key)
4102- $stderr.puts "warning: cannot resolve call to '" + mname + "' on " + recv_tag + " (emitting 0)"
4136+ $stderr.puts "warning: in " + ctx + ": cannot resolve call to '" + mname + "' on " + recv_tag + " (emitting 0)"
41034137 end
41044138
41054139 # Same dedupe pattern as warn_unresolved_call but for unknown
@@ -4107,7 +4141,8 @@
41074141 # single program with both an undefined method and an undefined
41084142 # constant produces two distinct warnings, not interleaved noise.
41094143 def warn_unresolved_const(rname)
4110- key = "_const_:" + rname
4144+ ctx = current_method_context_str
4145+ key = "_const_:" + rname + ":" + ctx
41114146 i = 0
41124147 while i < @unresolved_call_warnings.length
41134148 if @unresolved_call_warnings[i] == key
@@ -4116,7 +4151,7 @@
41164151 i = i + 1
41174152 end
41184153 @unresolved_call_warnings.push(key)
4119- $stderr.puts "warning: uninitialized constant '" + rname + "' (emitting 0)"
4154+ $stderr.puts "warning: in " + ctx + ": uninitialized constant '" + rname + "' (emitting 0)"
41204155 end
41214156
41224157 # Walk every class's parent chain. A cycle anywhere on the chain is