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

@@ -2841,6 +2841,33 @@
28412841 end
28422842 end
28432843 end
2844+ # Int LV recv with user-class attr_reader: codegen's
2845+ # compile_int_class_fallback_expr picks the FIRST class in
2846+ # @cls_names that defines the attr_reader and emits
2847+ # `((sp_C *)lv)->iv_X` directly. Mirror that pick here so the
2848+ # boxer dispatch at the attr_writer call site (line 22838)
2849+ # gets the matching ivar type instead of the int fallback.
2850+ # Without this, `self.attr = lv.attr` where lv is mrb_int and
2851+ # the user class has attr_reader emits `sp_box_int(<const char *>)`
2852+ # -- the residual #694 / #700 shape A.
2853+ #
2854+ # Restrict to scope == "int" (matches compile_int_class_fallback_expr's
2855+ # gate). Poly LV recv goes through the cls_id-switch dispatch,
2856+ # which box-per-arm produces sp_RbVal -- infer_type stays at the
2857+ # cached "poly" / "int" default and the boxer at the writer
2858+ # call site noop's (box_value_to_poly("poly", val) = val).
2859+ # Filter out primitive-shared method names so a real int LV
2860+ # calling a shared op (`s.index(...)`) doesn't get rerouted.
2861+ if cmt_scope_t == "int" && primitive_method_shared_with_user_class(@nd_name[nid]) == 0
2862+ cmt_mname2 = @nd_name[nid]
2863+ ci2 = 0
2864+ while ci2 < @cls_names.length
2865+ if cls_has_attr_reader(ci2, cmt_mname2) == 1
2866+ return cls_ivar_type(ci2, "@" + cmt_mname2)
2867+ end
2868+ ci2 = ci2 + 1
2869+ end
2870+ end
28442871 end
28452872 end
28462873 # Cache lookup. analyze.rb's annotate_all_node_types fills