Commit 82be610f

82be610f7cb7Merged

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

Description

fix(analyze,codegen): nil-default param widens to poly when typed callers exist (#634 shape A)

def f(x = nil) called both with f() and f(:foo) typed the slot as symbol because per-call-site unification picked the non-nil at over the nil default. The no-arg f() then defaulted the missing arg to sp_sym 0 rather than nil, and the body's x.nil? branch became dead code. The widening choice was deliberate at the unifier — flipping it broadly cascaded ivar backflow and ivar-from-param inferences to poly, breaking the self-host and several regression tests (notably nil_ivar_no_predicate_stays_scalar / #497).

Apply the widening only as a focused post-pass over param tables after all existing widening passes: any optional param whose default is literally NilNode and whose post-widening type is a non-nullable scalar (int / symbol / float / bool) escalates to poly. Local-var unification, ivar backflow, and ivars never written to nil stay on their current narrow paths.

Codegen's compile_call_args_with_defaults no-positional + has- default branch now routes through compile_expr_for_expected_type so a NilNode default flowing into a poly slot boxes to sp_box_nil() instead of emitting the bare 0.

compiler_helpers.rb's sole arg_types = nil default switches to "".split(",") so the new widening doesn't collapse it (both call sites always pass a concrete StrArray).

Shape B (attr_accessor without explicit init + typed write site) is a different code path; left for a follow-up.

Refs #634.

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

Details

Commit
82be610f7cb7
Parent
d56ba8b5dcc0

compiler_helpers.rbmodifiedView file

@@ -146,10 +146,12 @@
146146 # nid + arg_types let the helper apply the same arm-suppression
147147 # logic the emit loop uses (param-incompat + observed-class
148148 # narrow), so unreachable arms don't widen the result.
149- def poly_dispatch_return_type(mname, nid = -1, arg_types = nil)
150- if arg_types == nil
151- arg_types = "".split(",")
152- end
149+ def poly_dispatch_return_type(mname, nid = -1, arg_types = "".split(","))
150+ # arg_types defaults to an empty StrArray rather than nil so the
151+ # nil-default + typed-callsite widening that #634 enables doesn't
152+ # collapse this param's slot to poly. Both call sites always pass
153+ # a concrete StrArray, so the empty default is observationally
154+ # equivalent to the prior nil-then-replace.
153155 if mname == "[]"
154156 @needs_rb_value = 1
155157 return "poly"