Commit 5097b39b

5097b39b887aMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · +2 added · ~3 modified

Description

Resolve alias sources that name an attr_reader-generated method

alias new old / alias_method :new, :old aborted analysis with "source method not found" when old was generated by attr_reader / attr_accessor: those readers live in @cls_attr_readers, not the @cls_meth_* tables the alias resolver searched.

Register the alias as a reader too and record the source's backing ivar (a new sparse @attr_alias_keys/@attr_alias_ivars map, IR-carried via compiler_state_sa) so obj.new reads the source slot rather than a nonexistent @new. A shared attr_reader_ivar helper resolves the backing ivar at every reader read/type site; it defaults to the reader name, so non-aliased readers are unchanged. The reader's inferred type propagates (typed uses like .upcase and interpolation work), and the inherited-attr_reader case (parent declares, child aliases) resolves through the parent walk.

Closes #1356

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
5097b39b887a
Parent
e304b98e78d5

compiler_helpers.rbmodifiedView file

@@ -117,6 +117,7 @@
117117 compiler_state_sa :ieval_extra_param_names, :compile_time_subst_keys, :compile_time_subst_vals, :cls_with_internal_ieval_lift, :toplevel_ivar_names, :toplevel_ivar_types
118118 compiler_state_sa :lambda_var_ret_names, :lambda_var_ret_types, :multi_const_inits, :meth_blk_param_types, :cls_cmeth_blk_param_types
119119 compiler_state_sa :iexec_block_pnames, :iexec_block_ptypes
120+ compiler_state_sa :attr_alias_keys, :attr_alias_ivars
120121
121122 compiler_state_ia :meth_body_ids, :meth_rest_index, :meth_kwrest_index, :meth_has_yield, :cls_rest_idxs, :cls_is_value_type
122123 compiler_state_ia :cls_is_sra, :const_expr_ids, :const_mutated, :gvar_written, :module_body_ids, :ffi_buf_sizes, :ffi_reader_offsets
@@ -1343,6 +1344,45 @@
13431344 acc
13441345 end
13451346
1347+ # Backing ivar base-name for an attr_reader. For a normal reader this
1348+ # is the reader name itself; for a reader created by `alias new old`
1349+ # (or `alias_method :new, :old`) where `old` is an attr_reader, it is
1350+ # `old`'s backing ivar so `obj.new` reads the source slot rather than a
1351+ # nonexistent `@new`. Walks the parent chain like cls_has_attr_reader.
1352+ # The identity default keeps every non-aliased reader unchanged, so
1353+ # this is purely additive for code that doesn't alias accessors.
1354+ def attr_reader_ivar(ci, mname)
1355+ if ci < 0
1356+ return mname
1357+ end
1358+ key = @cls_names[ci] + "#" + mname
1359+ i = 0
1360+ while i < @attr_alias_keys.length
1361+ if @attr_alias_keys[i] == key
1362+ return @attr_alias_ivars[i]
1363+ end
1364+ i = i + 1
1365+ end
1366+ # If `ci` declares this reader directly (a plain attr_reader, not via
1367+ # the parent), its backing ivar is the reader name itself — a subclass
1368+ # redeclaring a name must not inherit a parent's alias backing.
1369+ own = @cls_attr_readers[ci].split(";", -1)
1370+ di = 0
1371+ while di < own.length
1372+ if own[di] == mname
1373+ return mname
1374+ end
1375+ di = di + 1
1376+ end
1377+ if @cls_parents[ci] != ""
1378+ pi = find_class_idx(@cls_parents[ci])
1379+ if pi >= 0
1380+ return attr_reader_ivar(pi, mname)
1381+ end
1382+ end
1383+ mname
1384+ end
1385+
13461386 def im_push_names(acc, names, suffix)
13471387 k = 0
13481388 while k < names.length