Commit 70e0dcbe
70e0dcbed3dfMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
Merge pull request #605 from ryanseys/rs-ieval-return-value
feat(analyze,codegen): instance_eval returns last-expression value
Details
- Commit
- 70e0dcbed3df
- Parent
- 30e7207f703f
- Merge parent
- d36385d39337
spinel_analyze.rbmodifiedView file
@@ -578,9 +578,14 @@
578578
# instance_eval block hoisting: parallel arrays indexed by synthetic579579
# function id N. Each lifted block becomes a file-scope static580580
# function `sp_ieval_<N>` that takes a typed `self` parameter.581+
# @ieval_return_types is the inferred ctype of the lifted body's last582+
# expression ("void" when assignment-only); codegen reads it to pick583+
# the lifted function's signature and whether the call site emits a584+
# direct call or the receiver-comma-expr fallback.581585
@ieval_counter = 0582586
@ieval_class_idxs = []583587
@ieval_body_ids = []588+
@ieval_return_types = "".split(",")584589
585590
# RBS-derived seed lines. Populated by load_rbs_seeds before586591
# analyze_phase; consumed by apply_rbs_seeds after collect_all@@ -2967,19 +2972,25 @@
29672972
# `recv.__sp_ieval_<N>(...)`: the rewritten form of an29682973
# `recv.instance_eval { ... }` call. v1 only fired on top-level call29692974
# sites, where the call's value was always discarded — so its return2970-
# type didn't matter and the warn-fallback "int" was harmless. Now2971-
# that the rewrite can land inside a class method body whose tail2972-
# expression IS the instance_eval call, the enclosing method's2973-
# signature has to match the value the lift actually emits:2974-
# `compile_ieval_call_expr` returns the receiver via a comma2975-
# expression, so the type is recv's class. Read the synthetic id's2976-
# registered class directly from `@ieval_class_idxs` rather than2977-
# re-inferring recv — by the time this runs (compile-side type2978-
# iteration), recv's type may have been refined and the registered2979-
# class is the canonical answer.2975+
# `recv.__sp_ieval_<N>(...)` is the rewritten form of an2976+
# `recv.instance_eval { ... }` call. compile_ieval_call_expr emits2977+
# a direct call to sp_ieval_<N> when the body's last expression has2978+
# a non-void inferred type, or the comma-expression form yielding2979+
# the receiver when void. Mirror that here: non-void return type2980+
# from @ieval_return_types when known, else fall through to the2981+
# receiver's registered class (obj_<C>).29802982
if is_ieval_call_name(mname) == 129812983
suffix = mname[11, mname.length - 11]29822984
n = suffix.to_i2985+
# Non-void lifted body: the call's value is the block's last2986+
# expression. Void body: the comma-expr fallback yields the2987+
# receiver, so the call types as obj_<C>.2988+
if n >= 0 && n < @ieval_return_types.length2989+
rt = @ieval_return_types[n]2990+
if rt != "" && rt != "void"2991+
return rt2992+
end2993+
end29832994
if n >= 0 && n < @ieval_class_idxs.length29842995
return "obj_" + @cls_names[@ieval_class_idxs[n]]29852996
end@@ -6876,6 +6887,7 @@
68766887
# from analyze get appended to instead of replaced.68776888
@ieval_class_idxs = []68786889
@ieval_body_ids = []6890+
@ieval_return_types = "".split(",")68796891
# Widen class-method ptypes through obj-typed receivers before the68806892
# walk so a method-param receiver (e.g. `def configure(app);68816893
# app.instance_eval { } end` invoked as `cfg.configure(routes)`)@@ -7154,6 +7166,10 @@
71547166
@ieval_counter = @ieval_counter + 171557167
@ieval_class_idxs.push(ci)71567168
@ieval_body_ids.push(body_id)7169+
# Seed return type as "void"; infer_ieval_body_return_types runs7170+
# after the main return-type fixpoint converges and overwrites with7171+
# the body's last-expression ctype when non-void.7172+
@ieval_return_types.push("void")71577173
# Mark the call site: the function name doubles as the synthetic id.71587174
# compile_call_expr / compile_call_stmt recognise the prefix and71597175
# emit a direct C call to `sp_ieval_<N>`.@@ -7185,6 +7201,45 @@
71857201
end71867202
end71877203
7204+
# Last-expression return-type inference for each lifted ieval body.7205+
# Runs after the main `infer_all_returns` fixpoint converges so7206+
# receiverless calls inside the body see their final return types.7207+
# When the body's last expression is assignment-only the result stays7208+
# "void" -- compile_ieval_call_expr falls back to the comma-expr form7209+
# yielding the receiver so truthy contexts still type-check. Only7210+
# caches "simple" ctypes (scalars + obj_<C>); array / hash / poly7211+
# bodies stay "void" so they take the comma-expr fallback, matching7212+
# the pre-existing receiver-typed signature. Broader coverage belongs7213+
# in a follow-up.7214+
def infer_ieval_body_return_types7215+
n = 07216+
while n < @ieval_class_idxs.length7217+
ci = @ieval_class_idxs[n]7218+
bid = @ieval_body_ids[n]7219+
if bid >= 07220+
@current_class_idx = ci7221+
push_scope7222+
rt = infer_body_return(bid)7223+
if ieval_return_type_is_simple(rt) == 17224+
@ieval_return_types[n] = rt7225+
end7226+
pop_scope7227+
@current_class_idx = -17228+
end7229+
n = n + 17230+
end7231+
end7232+
7233+
def ieval_return_type_is_simple(rt)7234+
if rt == "int" || rt == "bool" || rt == "float" || rt == "string" || rt == "mutable_str" || rt == "nil"7235+
return 17236+
end7237+
if rt.length > 4 && rt[0, 4] == "obj_"7238+
return 17239+
end7240+
07241+
end7242+
71887243
def is_ieval_call_name(mname)71897244
if mname.length <= 1171907245
return 0@@ -19891,6 +19946,10 @@
1989119946
unify_imeth_override_returns1989219947
infer_all_returns1989319948
unify_imeth_override_returns19949+
# Lifted ieval bodies: now that all method return types are stable,19950+
# walk each block body once and cache the last-expression ctype per19951+
# synthetic id. compile_ieval_call_expr consults the cache.19952+
infer_ieval_body_return_types1989419953
# Widen function return types to poly when explicit `return X`1989519954
# and the implicit final-expression return disagree on the1989619955
# value-vs-pointer axis. Runs post-fixpoint so the iterative@@ -25210,6 +25269,7 @@
2521025269
buf = ir_emit_sa(buf, "@poly_param_types", @poly_param_types)2521125270
buf = ir_emit_ia(buf, "@ieval_class_idxs", @ieval_class_idxs)2521225271
buf = ir_emit_ia(buf, "@ieval_body_ids", @ieval_body_ids)25272+
buf = ir_emit_sa(buf, "@ieval_return_types", @ieval_return_types)2521325273
buf = ir_emit_ia(buf, "@pre_execution_blocks", @pre_execution_blocks)2521425274
buf = ir_emit_ia(buf, "@post_execution_blocks", @post_execution_blocks)2521525275
buf = ir_emit_sa(buf, "@toplevel_ivar_names", @toplevel_ivar_names)spinel_codegen.rbmodifiedView file
Loading diff…
test/bm_instance_eval.rbmodifiedView file
Loading diff…
test/instance_eval_return_value.rbaddedView file
Loading diff…
test/instance_eval_return_value.rb.expectedaddedView file
Loading diff…