Commit f5e38f7d

f5e38f7d8013Merged

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

Description

Return nil from Range#size on a String range

("a".."z").size raised a C compile error: the codegen built an sp_Range from the string bounds (sp_range_new on char* args). CRuby returns nil for a String range's size because the element count isn't a simple arithmetic span. Detect a literal String RangeNode receiver and emit nil (typed as a nullable int) for size; integer ranges keep their arithmetic element count. length/count on String ranges remain outside the supported subset.

Fixes #1236

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

Details

Commit
f5e38f7d8013
Parent
c4ca265a3fa8

spinel_analyze.rbmodifiedView file

@@ -4304,6 +4304,29 @@
43044304 0
43054305 end
43064306
4307+ # Resolve the literal RangeNode behind a method receiver, peeking
4308+ # through a single ParenthesesNode wrap. Returns -1 when the receiver
4309+ # isn't a literal range.
4310+ def resolve_literal_range_recv(nid)
4311+ recv = @nd_receiver[nid]
4312+ if recv < 0
4313+ return -1
4314+ end
4315+ if @nd_type[recv] == "RangeNode"
4316+ return recv
4317+ end
4318+ if @nd_type[recv] == "ParenthesesNode"
4319+ pb = @nd_body[recv]
4320+ if pb >= 0
4321+ ps = get_stmts(pb)
4322+ if ps.length > 0 && @nd_type[ps.first] == "RangeNode"
4323+ return ps.first
4324+ end
4325+ end
4326+ end
4327+ -1
4328+ end
4329+
43074330 def infer_method_name_type(nid, mname, recv)
43084331 # when recv is a class/module constant ref whose
43094332 # class/module defines a class method of the given name, defer
@@ -5356,6 +5379,13 @@
53565379 return "int"
53575380 end
53585381 if mname == "size"
5382+ # A String range like ("a".."z") has no integer size; CRuby
5383+ # Range#size returns nil. Type the result as a nullable int so
5384+ # the nil renders correctly. (Int ranges keep returning int.)
5385+ srr_sz = resolve_literal_range_recv(nid)
5386+ if srr_sz >= 0 && @nd_left[srr_sz] >= 0 && infer_type(@nd_left[srr_sz]) == "string"
5387+ return "int?"
5388+ end
53595389 if recv_could_be_obj(recv) == 1 && any_user_class_defines_imeth(mname) == 1
53605390 return ""
53615391 end