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
# nid + arg_types let the helper apply the same arm-suppression # logic the emit loop uses (param-incompat + observed-class # narrow), so unreachable arms don't widen the result. def poly_dispatch_return_type(mname, nid = -1, arg_types = nil) if arg_types == nil arg_types = "".split(",") end def poly_dispatch_return_type(mname, nid = -1, arg_types = "".split(",")) # arg_types defaults to an empty StrArray rather than nil so the # nil-default + typed-callsite widening that #634 enables doesn't # collapse this param's slot to poly. Both call sites always pass # a concrete StrArray, so the empty default is observationally # equivalent to the prior nil-then-replace. if mname == "[]" @needs_rb_value = 1 return "poly"spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/nil_default_param_widens_to_poly.expectedaddedView file
Loading diff…
test/nil_default_param_widens_to_poly.rbaddedView file
Loading diff…