Commit df14870
## Background
The Ruby parser renames an instance method `initialize` to `::new` for
documentation purposes. This rename happened *after*
`container.add_method`, with a comment claiming the ordering is
intentional: "Rename after add_method to register duplicated 'new' and
'initialize' defined in c and ruby".
The actual reason for this placement is older. In the Ripper-based
streaming parser, documentation modifiers such as `:notnew:` were read
*after* the method line, so at `add_method` time the parser simply did
not know yet whether the method should be renamed:
```ruby
# Having now read the method parameters and documentation modifiers, we
# now know whether we have to rename #initialize to ::new
```
(lib/rdoc/parser/ripper_ruby.rb, removed in #1690)
The Prism parser processes directives and modifier lines before
`add_method`, so this constraint is gone. The post-add placement was a
consequence of the streaming parser's information ordering, not a design
goal — which is why it is safe to retire the post-add mutation pattern
now. The comment in the current code was a port-time rationalization of
an observable side effect.
## What the old placement actually did
Registering the method under the `#initialize` key and renaming it
afterwards had two effects:
1. `Context#methods_hash` was left keyed by a stale name (`#initialize`
pointing at a method now called `::new`). This is one of the obstacles
to making `Context#find_method` hash-based (see the discussion in
#1796).
2. Duplicate detection in `Context#add_method` was bypassed. A class
documenting both `::new` (an explicit `def self.new`, or a C-defined
`new`) and `#initialize` ended up with two `::new` entries on its page.
## Change
Move the rename block before `container.add_method`. All information it
uses (the method name, `singleton`, and `dont_rename_initialize` set by
the `:notnew:` directive) is already available at that point.
With the rename in place before registration, the normal deduplication
applies: the first registration wins, and the duplicate is reported by
the existing "Duplicate method" warning (visible with `--verbose`).
## Corpus diff
Compared per-class method lists (name, singleton, visibility, file) for
whole corpora, before vs after:
| Corpus | Changes |
| --- | --- |
| ruby/ruby | 4 classes lose a duplicated `::new` entry:
`Gem::Package::TarReader`, `Gem::Package::TarWriter`,
`Gem::Resolver::APISpecification`, `JSON::Ext::Generator::State` |
| activesupport 8.1.3 | 1 class:
`ActiveSupport::Deprecation::DeprecatedConstantProxy` |
| rdoc itself | no change |
All other entries are identical. Each changed class defines both `def
self.new` and `def initialize` (or, for `JSON::Ext::Generator::State`, a
C-defined `new` and a Ruby `initialize` — the exact "c and ruby" case
the old comment referred to). On master these pages show `new` twice,
with a duplicated `id="method-c-new"` anchor (invalid HTML; the index
can only link to the first entry); with this change they show it once.
## Cleanup
The second commit removes the `dont_rename_initialize` keyword argument
of `internal_add_method`. It has never been passed a truthy value since
its introduction: one call site passes an explicit `false` and the other
relies on the `false` default. It is unrelated to
`AnyMethod#dont_rename_initialize` (set by the `:notnew:` directive),
which remains the live mechanism; removing the constant-false parameter
also removes the confusion of two same-named flags in one method.
---------
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent f4e3c3b commit df14870
2 files changed
Lines changed: 36 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
451 | 451 | | |
452 | 452 | | |
453 | 453 | | |
454 | | - | |
455 | 454 | | |
456 | 455 | | |
457 | 456 | | |
| |||
722 | 721 | | |
723 | 722 | | |
724 | 723 | | |
725 | | - | |
| 724 | + | |
726 | 725 | | |
727 | 726 | | |
728 | 727 | | |
| |||
746 | 745 | | |
747 | 746 | | |
748 | 747 | | |
749 | | - | |
750 | | - | |
751 | | - | |
752 | | - | |
753 | | - | |
754 | | - | |
755 | 748 | | |
756 | | - | |
757 | | - | |
758 | | - | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
759 | 752 | | |
760 | 753 | | |
761 | 754 | | |
| |||
764 | 757 | | |
765 | 758 | | |
766 | 759 | | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
767 | 767 | | |
768 | 768 | | |
769 | 769 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
700 | 700 | | |
701 | 701 | | |
702 | 702 | | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
703 | 728 | | |
704 | 729 | | |
705 | 730 | | |
| |||
0 commit comments