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 end
3896+ # NilClass coercion methods. Issue #871. to_c / to_r / to_h
3897+ # skipped: Complex / Rational unsupported, Hash needs typed
3898+ # element judgement.
3899+ if recv >= 0
3900+ recv_t_nil = infer_type(recv)
3901+ if recv_t_nil == "nil"
3902+ if mname == "to_i"
3903+ return "int"
3904+ end
3905+ if mname == "to_f"
3906+ return "float"
3907+ end
3908+ if mname == "to_a"
3909+ @needs_int_array = 1
3910+ return "int_array"
3911+ end
3912+ if mname == "&"
3913+ return "bool"
3914+ end
3915+ if mname == "|" || mname == "^"
3916+ return "bool"
3917+ end
3918+ end
3919+ end
38963920 # Class#name -- the class's source name as
38973921 # a string. Aliases `.to_s` at the runtime helper level
38983922 # (sp_class_to_s), so the return type is the same.