Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rdoc/code_object/class_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ def merge(class_module)

merge_collections method_list, cm.method_list, other_files do |add, meth|
if add then
@visibility = meth.visibility

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch on the root cause - merge is definitely reusing a stale @visibility left over from marshal_load.

I'd suggest fixing this one level down instead. Visibility is lexical state, and Parser::Ruby already tracks it separately in its own @visibility - it never touches Context#visibility. So add_method overwriting method.visibility with the container's @visibility is a leftover from the old design, and every caller passing in an already-complete method has to work around it:

  • marshal_load sets @visibility just so add_method picks it up
  • merge (this bug) inherits what marshal_load left behind
  • prepare_to_embed assigns self.visibility = code_object.visibility back onto
    the container, with a comment explaining the workaround
  • Parser::Ruby orders its calls carefully:
    a.visibility = visibility # should set after adding to container

Dropping the assignment in add_method (and the matching one in add_attribute) fixes this at the root and lets all four workarounds go away.
No new argument needed - AnyMethod already defaults to :public and every caller sets visibility itself, so marshal_load just does method.visibility = visibility directly.

add_method meth
else
@method_list.delete meth
Expand Down
8 changes: 8 additions & 0 deletions test/rdoc/rdoc_store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,14 @@ def test_save_class_merge_constant
assert_empty result.constants
end

def test_save_class_merge_method_visibility
@s.save_class @klass
assert_equal :public, @meth.visibility

@s.save_class @klass # save again to trigger ClassModule#merge
assert_equal :public, @meth.visibility
end

def test_save_class_methods
@s.save_class @klass

Expand Down