Commit d55c86b4
d55c86b47ed4Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
codegen,analyze: NilClass#to_i / to_f / to_a coercions
Three NilClass coercion methods that fell through to the unresolved-call fallback. Lowered to constants:
nil.to_i -> 0 nil.to_f -> 0.0 nil.to_a -> empty int_array
Deferred (need separate work): nil.to_c (Complex literal not supported) nil.to_r (Rational literal not supported) nil.to_h (typed-Hash empty needs element-type judgement)
Refs #871
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- d55c86b47ed4
- Parent
- 24a87506af5d
spinel_analyze.rbmodifiedView file
@@ -3893,6 +3893,30 @@
38933893
if mname == "inspect"38943894
return "string"38953895
end3896+
# NilClass coercion methods. Issue #871. to_c / to_r / to_h3897+
# skipped: Complex / Rational unsupported, Hash needs typed3898+
# element judgement.3899+
if recv >= 03900+
recv_t_nil = infer_type(recv)3901+
if recv_t_nil == "nil"3902+
if mname == "to_i"3903+
return "int"3904+
end3905+
if mname == "to_f"3906+
return "float"3907+
end3908+
if mname == "to_a"3909+
@needs_int_array = 13910+
return "int_array"3911+
end3912+
if mname == "&"3913+
return "bool"3914+
end3915+
if mname == "|" || mname == "^"3916+
return "bool"3917+
end3918+
end3919+
end38963920
# Class#name -- the class's source name as38973921
# a string. Aliases `.to_s` at the runtime helper level38983922
# (sp_class_to_s), so the return type is the same.spinel_codegen.rbmodifiedView file
Loading diff…
test/nil_coercion.rbaddedView file
Loading diff…
test/nil_coercion.rb.expectedaddedView file
Loading diff…