Commit d36385d3

d36385d39337Merged

Ryan Seys <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified

Description

feat(analyze,codegen): instance_eval returns last-expression value

The existing lift returned the receiver via a comma-expression hack so n = obj.instance_eval { @count } yielded the Counter, not the int. Make the lifted sp_ieval_ carry the inferred return type from the block body and emit a direct call at the expression site; fall back to the comma-expression form when the body is assignment-only so if obj.instance_eval { @flag = 1 } still reads truthy. This pass restricts the cached return type to "simple" shapes (int / bool / float / string / mutable_str / nil / obj_); array / hash / poly bodies stay void and take the receiver-fallback path -- broader coverage belongs in a follow-up.

  • analyze: infer_ieval_body_return_types walks each lifted body after the main return-type fixpoint converges and caches the last-expression ctype per ieval id (gated by ieval_return_type_is_simple). infer_call_type honours the cache for __sp_ieval_<N> lookups, falling back to obj_.
  • codegen: emit_ieval_func and the forward-prototype emit use the cached ctype for the function signature. compile_ieval_call_expr returns a direct call when the cached ctype is non-void, comma-expr otherwise. ieval_sig_ret inlines the C-type mapping; routing through c_type would cascade-widen unrelated parameters via the @out_lines push chain (analyzer flow-typing fragility).
  • test/instance_eval_return_value.rb: int / bool / arith / string return bodies plus the void-fallback truthy case.
  • test/bm_instance_eval.rb: sections 9 and 11 (tail-position ivar / param receiver) gain explicit receiver returns so the enclosing method's signature stays Routes-typed -- the lift no longer flows the entries array out as the tail value.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
d36385d39337
Parent
397889d88953

spinel_analyze.rbmodifiedView file

@@ -578,9 +578,14 @@
578578 # instance_eval block hoisting: parallel arrays indexed by synthetic
579579 # function id N. Each lifted block becomes a file-scope static
580580 # function `sp_ieval_<N>` that takes a typed `self` parameter.
581+ # @ieval_return_types is the inferred ctype of the lifted body's last
582+ # expression ("void" when assignment-only); codegen reads it to pick
583+ # the lifted function's signature and whether the call site emits a
584+ # direct call or the receiver-comma-expr fallback.
581585 @ieval_counter = 0
582586 @ieval_class_idxs = []
583587 @ieval_body_ids = []
588+ @ieval_return_types = "".split(",")
584589
585590 # RBS-derived seed lines. Populated by load_rbs_seeds before
586591 # analyze_phase; consumed by apply_rbs_seeds after collect_all
@@ -2967,19 +2972,25 @@
29672972 # `recv.__sp_ieval_<N>(...)`: the rewritten form of an
29682973 # `recv.instance_eval { ... }` call. v1 only fired on top-level call
29692974 # sites, where the call's value was always discarded — so its return
2970- # type didn't matter and the warn-fallback "int" was harmless. Now
2971- # that the rewrite can land inside a class method body whose tail
2972- # expression IS the instance_eval call, the enclosing method's
2973- # signature has to match the value the lift actually emits:
2974- # `compile_ieval_call_expr` returns the receiver via a comma
2975- # expression, so the type is recv's class. Read the synthetic id's
2976- # registered class directly from `@ieval_class_idxs` rather than
2977- # re-inferring recv — by the time this runs (compile-side type
2978- # iteration), recv's type may have been refined and the registered
2979- # class is the canonical answer.
2975+ # `recv.__sp_ieval_<N>(...)` is the rewritten form of an
2976+ # `recv.instance_eval { ... }` call. compile_ieval_call_expr emits
2977+ # a direct call to sp_ieval_<N> when the body's last expression has
2978+ # a non-void inferred type, or the comma-expression form yielding
2979+ # the receiver when void. Mirror that here: non-void return type
2980+ # from @ieval_return_types when known, else fall through to the
2981+ # receiver's registered class (obj_<C>).
29802982 if is_ieval_call_name(mname) == 1
29812983 suffix = mname[11, mname.length - 11]
29822984 n = suffix.to_i
2985+ # Non-void lifted body: the call's value is the block's last
2986+ # expression. Void body: the comma-expr fallback yields the
2987+ # receiver, so the call types as obj_<C>.
2988+ if n >= 0 && n < @ieval_return_types.length
2989+ rt = @ieval_return_types[n]
2990+ if rt != "" && rt != "void"
2991+ return rt
2992+ end
2993+ end
29832994 if n >= 0 && n < @ieval_class_idxs.length
29842995 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 the
68806892 # 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 + 1
71557167 @ieval_class_idxs.push(ci)
71567168 @ieval_body_ids.push(body_id)
7169+ # Seed return type as "void"; infer_ieval_body_return_types runs
7170+ # after the main return-type fixpoint converges and overwrites with
7171+ # 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 and
71597175 # emit a direct C call to `sp_ieval_<N>`.
@@ -7185,6 +7201,45 @@
71857201 end
71867202 end
71877203
7204+ # Last-expression return-type inference for each lifted ieval body.
7205+ # Runs after the main `infer_all_returns` fixpoint converges so
7206+ # receiverless calls inside the body see their final return types.
7207+ # When the body's last expression is assignment-only the result stays
7208+ # "void" -- compile_ieval_call_expr falls back to the comma-expr form
7209+ # yielding the receiver so truthy contexts still type-check. Only
7210+ # caches "simple" ctypes (scalars + obj_<C>); array / hash / poly
7211+ # bodies stay "void" so they take the comma-expr fallback, matching
7212+ # the pre-existing receiver-typed signature. Broader coverage belongs
7213+ # in a follow-up.
7214+ def infer_ieval_body_return_types
7215+ n = 0
7216+ while n < @ieval_class_idxs.length
7217+ ci = @ieval_class_idxs[n]
7218+ bid = @ieval_body_ids[n]
7219+ if bid >= 0
7220+ @current_class_idx = ci
7221+ push_scope
7222+ rt = infer_body_return(bid)
7223+ if ieval_return_type_is_simple(rt) == 1
7224+ @ieval_return_types[n] = rt
7225+ end
7226+ pop_scope
7227+ @current_class_idx = -1
7228+ end
7229+ n = n + 1
7230+ end
7231+ end
7232+
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 1
7236+ end
7237+ if rt.length > 4 && rt[0, 4] == "obj_"
7238+ return 1
7239+ end
7240+ 0
7241+ end
7242+
71887243 def is_ieval_call_name(mname)
71897244 if mname.length <= 11
71907245 return 0
@@ -19871,6 +19926,10 @@
1987119926 unify_imeth_override_returns
1987219927 infer_all_returns
1987319928 unify_imeth_override_returns
19929+ # Lifted ieval bodies: now that all method return types are stable,
19930+ # walk each block body once and cache the last-expression ctype per
19931+ # synthetic id. compile_ieval_call_expr consults the cache.
19932+ infer_ieval_body_return_types
1987419933 # Widen function return types to poly when explicit `return X`
1987519934 # and the implicit final-expression return disagree on the
1987619935 # value-vs-pointer axis. Runs post-fixpoint so the iterative
@@ -25190,6 +25249,7 @@
2519025249 buf = ir_emit_sa(buf, "@poly_param_types", @poly_param_types)
2519125250 buf = ir_emit_ia(buf, "@ieval_class_idxs", @ieval_class_idxs)
2519225251 buf = ir_emit_ia(buf, "@ieval_body_ids", @ieval_body_ids)
25252+ buf = ir_emit_sa(buf, "@ieval_return_types", @ieval_return_types)
2519325253 buf = ir_emit_ia(buf, "@pre_execution_blocks", @pre_execution_blocks)
2519425254 buf = ir_emit_ia(buf, "@post_execution_blocks", @post_execution_blocks)
2519525255 buf = ir_emit_sa(buf, "@toplevel_ivar_names", @toplevel_ivar_names)