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 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
@@ -19891,6 +19946,10 @@
1989119946 unify_imeth_override_returns
1989219947 infer_all_returns
1989319948 unify_imeth_override_returns
19949+ # Lifted ieval bodies: now that all method return types are stable,
19950+ # walk each block body once and cache the last-expression ctype per
19951+ # synthetic id. compile_ieval_call_expr consults the cache.
19952+ infer_ieval_body_return_types
1989419953 # Widen function return types to poly when explicit `return X`
1989519954 # and the implicit final-expression return disagree on the
1989619955 # 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)