Commit 2ba0b9be
2ba0b9be1b67Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): infer_type tracks compile_int_class_fallback_expr's class pick
#700 shape A residual: self.commenter = p.commenter in
Comment#update(p) where p is typed mrb_int (fanout-widened)
and the codegen's compile_int_class_fallback_expr emits
((sp_CommentRow *)lv_p)->iv_commenter -- correctly reading the
const char* attr -- but the surrounding attr_writer's boxer
dispatch saw arg_t = "int" (analyze's fallback for unresolved
poly-recv method calls) and picked sp_box_int on the const
char* value.
The codegen and analyze disagreed: codegen picked the first user class with the attr_reader and emitted a typed field read; analyze gave up and returned the int default.
Mirror the codegen's pick at infer_type: when scope says LV is
int and the call is lv.attr_reader_name, iterate @cls_names in
declaration order and return the first matching class's
@<attr_reader> ivar type. Gated by primitive_method_shared_with_user_class
so real int LVs calling shared ops (s.index(...)) keep the
primitive dispatch.
Restrict to scope == "int" only -- poly recv goes through the
cls_id-switch dispatch which boxes per-arm to sp_RbVal, so the
"poly" fallback path (already cached by analyze) is correct.
First attempt also handled poly; reverted after the
poly_gsub_str_str_hash test failed (the test class set has A.body
String and B.body Integer; picking the first class for a poly recv
would mis-narrow to String).
Test: test/poly_attr_reader_box_dispatch.rb. 447 -> 448 tests pass.
Refs #700
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 2ba0b9be1b67
- Parent
- 76229ef590fd
spinel_codegen.rbmodifiedView file
end end end # Int LV recv with user-class attr_reader: codegen's # compile_int_class_fallback_expr picks the FIRST class in # @cls_names that defines the attr_reader and emits # `((sp_C *)lv)->iv_X` directly. Mirror that pick here so the # boxer dispatch at the attr_writer call site (line 22838) # gets the matching ivar type instead of the int fallback. # Without this, `self.attr = lv.attr` where lv is mrb_int and # the user class has attr_reader emits `sp_box_int(<const char *>)` # -- the residual #694 / #700 shape A. # # Restrict to scope == "int" (matches compile_int_class_fallback_expr's # gate). Poly LV recv goes through the cls_id-switch dispatch, # which box-per-arm produces sp_RbVal -- infer_type stays at the # cached "poly" / "int" default and the boxer at the writer # call site noop's (box_value_to_poly("poly", val) = val). # Filter out primitive-shared method names so a real int LV # calling a shared op (`s.index(...)`) doesn't get rerouted. if cmt_scope_t == "int" && primitive_method_shared_with_user_class(@nd_name[nid]) == 0 cmt_mname2 = @nd_name[nid] ci2 = 0 while ci2 < @cls_names.length if cls_has_attr_reader(ci2, cmt_mname2) == 1 return cls_ivar_type(ci2, "@" + cmt_mname2) end ci2 = ci2 + 1 end end end end # Cache lookup. analyze.rb's annotate_all_node_types fillstest/poly_attr_reader_box_dispatch.expectedaddedView file
Loading diff…
test/poly_attr_reader_box_dispatch.rbaddedView file
Loading diff…