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
043054305
end43064306
4307+
# Resolve the literal RangeNode behind a method receiver, peeking4308+
# through a single ParenthesesNode wrap. Returns -1 when the receiver4309+
# isn't a literal range.4310+
def resolve_literal_range_recv(nid)4311+
recv = @nd_receiver[nid]4312+
if recv < 04313+
return -14314+
end4315+
if @nd_type[recv] == "RangeNode"4316+
return recv4317+
end4318+
if @nd_type[recv] == "ParenthesesNode"4319+
pb = @nd_body[recv]4320+
if pb >= 04321+
ps = get_stmts(pb)4322+
if ps.length > 0 && @nd_type[ps.first] == "RangeNode"4323+
return ps.first4324+
end4325+
end4326+
end4327+
-14328+
end4329+
43074330
def infer_method_name_type(nid, mname, recv)43084331
# when recv is a class/module constant ref whose43094332
# class/module defines a class method of the given name, defer@@ -5356,6 +5379,13 @@
53565379
return "int"53575380
end53585381
if mname == "size"5382+
# A String range like ("a".."z") has no integer size; CRuby5383+
# Range#size returns nil. Type the result as a nullable int so5384+
# 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+
end53595389
if recv_could_be_obj(recv) == 1 && any_user_class_defines_imeth(mname) == 153605390
return ""53615391
endspinel_codegen.rbmodifiedView file
Loading diff…
test/range_size_string.rbaddedView file
Loading diff…
test/range_size_string.rb.expectedaddedView file
Loading diff…