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
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 compiler_state_sa :lambda_var_ret_names, :lambda_var_ret_types, :multi_const_inits, :meth_blk_param_types, :cls_cmeth_blk_param_types compiler_state_sa :iexec_block_pnames, :iexec_block_ptypes compiler_state_sa :attr_alias_keys, :attr_alias_ivars compiler_state_ia :meth_body_ids, :meth_rest_index, :meth_kwrest_index, :meth_has_yield, :cls_rest_idxs, :cls_is_value_type compiler_state_ia :cls_is_sra, :const_expr_ids, :const_mutated, :gvar_written, :module_body_ids, :ffi_buf_sizes, :ffi_reader_offsets acc end # Backing ivar base-name for an attr_reader. For a normal reader this # is the reader name itself; for a reader created by `alias new old` # (or `alias_method :new, :old`) where `old` is an attr_reader, it is # `old`'s backing ivar so `obj.new` reads the source slot rather than a # nonexistent `@new`. Walks the parent chain like cls_has_attr_reader. # The identity default keeps every non-aliased reader unchanged, so # this is purely additive for code that doesn't alias accessors. def attr_reader_ivar(ci, mname) if ci < 0 return mname end key = @cls_names[ci] + "#" + mname i = 0 while i < @attr_alias_keys.length if @attr_alias_keys[i] == key return @attr_alias_ivars[i] end i = i + 1 end # If `ci` declares this reader directly (a plain attr_reader, not via # the parent), its backing ivar is the reader name itself — a subclass # redeclaring a name must not inherit a parent's alias backing. own = @cls_attr_readers[ci].split(";", -1) di = 0 while di < own.length if own[di] == mname return mname end di = di + 1 end if @cls_parents[ci] != "" pi = find_class_idx(@cls_parents[ci]) if pi >= 0 return attr_reader_ivar(pi, mname) end end mname end def im_push_names(acc, names, suffix) k = 0 while k < names.lengthspinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/alias_attr_reader_source.rbaddedView file
Loading diff…
test/alias_attr_reader_source.rb.expectedaddedView file
Loading diff…