Commit 333ea257

333ea25766f2Merged

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

Description

fix(analyze): bare method-name arg in module body resolves to module cmeth

Closes #512. M.use(h) inside module M body — where the inner h is a bare reference to M.h — used to infer h as int (the default for unresolved bare calls). The wrong type propagated onto M.use's opts parameter and the downstream opts.length then emitted 0 with a "cannot resolve call to 'length' on int" warning.

Fix in infer_call_type's bare-call branch: when @current_lexical_scope names a module (and we're not inside a class scope -- @current_class_idx < 0), look up <scope>_cls_<mname> in @meth_names. If present, return its recorded return type. Sibling to the existing @current_class_idx >= 0 branch above; the cmeth is registered under that prefixed form by collect_module_with_prefix.

use(h) (where the outer call is also bare) still warns: the bare-call argument-propagation pass needs a parallel module-scope lookup to widen the callee's params from the call site's arg types. Documented inline in the test fixture.

Local: 481 tests pass.

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

Details

Commit
333ea25766f2
Parent
3bae3b7b87c2

spinel_analyze.rbmodifiedView file

@@ -2923,6 +2923,19 @@
29232923 mr = cls_method_return(@current_class_idx, mname)
29242924 return mr
29252925 end
2926+ # Bare call inside a `module M` body — implicit self is the
2927+ # module, so `h` resolves to `M.h`. The cmeth is registered as
2928+ # `<scope>_cls_<name>` in @meth_names by collect_module_with_prefix.
2929+ # Issue #512: without this, the bare call's return type fell to
2930+ # `int`, propagated onto the outer call's param, and downstream
2931+ # `opts.length` failed dispatch on the wrong (int) receiver type.
2932+ if recv < 0 && @current_lexical_scope != "" && @current_class_idx < 0
2933+ full_name = @current_lexical_scope + "_cls_" + mname
2934+ mi_module = find_method_idx(full_name)
2935+ if mi_module >= 0
2936+ return @meth_return_types[mi_module]
2937+ end
2938+ end
29262939
29272940 # proc / Proc.new
29282941 if mname == "proc"