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):
-
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 asUtil_cls_size_of) →Util.size_of, no def scope →(top level). -
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
0 end # Identifies the method body we're currently emitting, formatted for # diagnostic messages. Library-style methods with no concrete caller # default their params to `mrb_int` and surface as a flood of # "cannot resolve call to ... on int" lines; without context the # user can't tell which def the warning came from. Returns # "Foo#bar" for an instance method, "Foo.bar" for a class method # (incl. module class methods stored as @meth_names with a # `<Mod>_cls_<m>` form), or "(top level)" outside any def. def current_method_context_str if @current_class_idx >= 0 && @current_class_idx < @cls_names.length sep = "." if @current_method_has_self == 1 sep = "#" end return @cls_names[@current_class_idx] + sep + @current_method_name end if @current_method_name != "" ix_cls = @current_method_name.index("_cls_") # CRuby returns nil when not found; spinel runtime returns -1. if ix_cls != nil && ix_cls > 0 return @current_method_name[0, ix_cls] + "." + @current_method_name[ix_cls + 5, @current_method_name.length - (ix_cls + 5)] end return @current_method_name end "(top level)" end # Print a stderr warning the first time we see an unresolved call to # `mname` with the given receiver-type tag. Subsequent identical # warnings are suppressed so a silent-fallthrough call inside a hot # loop emits one line, not a torrent. The warning is informational # only — codegen continues and emits `0` for the call's C expression # (the historical silent-no-op behaviour) so existing tests/benches # whose outputs happen to coincide with `0` keep compiling. # `mname` with the given receiver-type tag in the current method # context. Subsequent identical warnings are suppressed so a # silent-fallthrough call inside a hot loop emits one line, not a # torrent. Dedupe key includes the method context so the same shape # of unresolved call in two different defs each produces its own # line — the prior key (`mname:recv_tag`) collapsed them and made # library-method-with-no-caller diagnostics opaque. The warning is # informational only — codegen continues and emits `0` for the # call's C expression (the historical silent-no-op behaviour) so # existing tests/benches whose outputs happen to coincide with `0` # keep compiling. def warn_unresolved_call(mname, recv_tag) key = mname + ":" + recv_tag ctx = current_method_context_str key = mname + ":" + recv_tag + ":" + ctx i = 0 while i < @unresolved_call_warnings.length if @unresolved_call_warnings[i] == key i = i + 1 end @unresolved_call_warnings.push(key) $stderr.puts "warning: cannot resolve call to '" + mname + "' on " + recv_tag + " (emitting 0)" $stderr.puts "warning: in " + ctx + ": cannot resolve call to '" + mname + "' on " + recv_tag + " (emitting 0)" end # Same dedupe pattern as warn_unresolved_call but for unknown # single program with both an undefined method and an undefined # constant produces two distinct warnings, not interleaved noise. def warn_unresolved_const(rname) key = "_const_:" + rname ctx = current_method_context_str key = "_const_:" + rname + ":" + ctx i = 0 while i < @unresolved_call_warnings.length if @unresolved_call_warnings[i] == key i = i + 1 end @unresolved_call_warnings.push(key) $stderr.puts "warning: uninitialized constant '" + rname + "' (emitting 0)" $stderr.puts "warning: in " + ctx + ": uninitialized constant '" + rname + "' (emitting 0)" end # Walk every class's parent chain. A cycle anywhere on the chain istest/warn_unresolved_call_context.rbaddedView file
Loading diff…
test/warn_unresolved_call_context.rb.expectedaddedView file
Loading diff…