Skip to content

Fix alias detection for multiple rb_define_singleton_method#1732

Open
Earlopain wants to merge 2 commits into
ruby:masterfrom
Earlopain:c-singleton-method-detection
Open

Fix alias detection for multiple rb_define_singleton_method#1732
Earlopain wants to merge 2 commits into
ruby:masterfrom
Earlopain:c-singleton-method-detection

Conversation

@Earlopain

@Earlopain Earlopain commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

They were not detected because singleton_classes doesn't contain the variable. Fix this simply by moving the check for singleton_method further up.

Closes #1722

List of affected classes

Complex
Date
DateTime
Dir
ENV
File
Pathname
Process
Regexp
Socket
Thread
UNIXSocket
Fiddle::Handle
RubyVM::InstructionSequence

grafik grafik

Copilot AI review requested due to automatic review settings June 16, 2026 10:56
@Earlopain
Earlopain had a problem deploying to fork-preview-protection June 16, 2026 10:56 — with GitHub Actions Failure

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates the RDoc C parser’s alias handling so aliases created from duplicate C function definitions correctly preserve singleton/instance context, and expands tests to cover both instance and singleton scenarios.

Changes:

  • Rename alias-related tests to distinguish instance vs singleton cases.
  • Add new tests for “duplicate rb_define_method / rb_define_singleton_method” aliasing behavior.
  • Update alias creation to accept an explicit singleton: value and broaden singleton detection in handle_method.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
test/rdoc/parser/c_test.rb Renames existing alias tests and adds coverage for duplicate-define alias generation for instance and singleton methods.
lib/rdoc/parser/c.rb Passes explicit singleton context when creating aliases and fixes singleton detection for singleton_method / module_function types.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/rdoc/parser/c_test.rb
Comment on lines +249 to +261
def test_do_instance_duplicate_define_method
content = <<~C
VALUE blah(VALUE klass, VALUE year) {
}

void Init_Blah(void) {
cDate = rb_define_class("Date", rb_cObject);

rb_define_method(cDate, "blah", blah, 1);

rb_define_method(cDate, "bleh", blah, 1);
}
C
They were not detected because `singleton_classes` doesn't contain the variable.
Fix this simply by moving the check for `singleton_method` further up.

Closes ruby#1722
@Earlopain
Earlopain force-pushed the c-singleton-method-detection branch from b343fe6 to 1054ba6 Compare June 16, 2026 11:01
@Earlopain
Earlopain had a problem deploying to fork-preview-protection June 16, 2026 11:01 — with GitHub Actions Failure
Comment thread lib/rdoc/parser/c.rb Outdated
@@ -996,7 +996,7 @@ def handle_method(type, var_name, meth_name, function, param_count,
class_obj = find_class var_name, class_name

if existing_method = class_obj.method_list.find { |m| m.c_function == function }

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.

I think it is better to also check singleton here.

Suggested change
if existing_method = class_obj.method_list.find { |m| m.c_function == function }
if existing_method = class_obj.method_list.find { |m| m.c_function == function && m.singleton == singleton }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, added a test for that. It passed before already though it did call add_alias which is unexpected.

It currently calls `add_alias` for the added test, although it already
works out correctly regardless.
Copilot AI review requested due to automatic review settings July 18, 2026 20:17
@Earlopain
Earlopain requested a deployment to fork-preview-protection July 18, 2026 20:17 — with GitHub Actions Waiting

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dir.getwd shows Dir.pwd as its name

3 participants