Skip to content

CLR extension methods named like accessors don't break property lookups (rebased) - #61

Merged
Largo merged 2 commits into
masterfrom
carry/51-accessor-names
Oct 29, 2026
Merged

Largo merged 2 commits into
masterfrom
carry/51-accessor-names

Conversation

@Largo

@Largo Largo commented Oct 29, 2026

Copy link
Copy Markdown
Owner

#51 rebased onto master, which now has #50 and #56. The original PR came from @sholden's branch, which still sits on the pre-rebase #50 and conflicts with master. Commits, authorship kept:

  • 10a488d Scott Holden: Extension methods named like accessors don't break property lookups
  • 7653bad Scott Holden: Tests: extension methods named like accessors, through both activations

One change in the rebase: master already has a ClrScopedExtensions6 (and 7) from #50, so this PR's ClrScopedExtensions6 is now ClrScopedExtensions8.

Checked together with #55, all of them merged onto master 62655f8 (Debug, net8.0, Linux): regen-initializers.sh leaves Initializers.Generated.cs unchanged; 0 build warnings; IronRuby.Tests 0 failures; spec/language 2934 examples, 0 failures; spec/core exception, mutex, conditionvariable, refinement, Module#refine/#using, main.using and Kernel#require all 0 failures; the 101 LINQ samples print exactly 101samples.expected; Util/error-message-matrix.rb, refinement-binding-matrix.rb, jit-matrix.rb and condvar-matrix.rb match CRuby 4.0.6 line for line, and refinement-matrix.rb differs only in object addresses.

The description below is from #51 (sholden's).


After using_clr_extensions, a classic extension method named like a property accessor made the lookup of that property throw a CLR NullReferenceException.

public static int get_Weight(this Widget w) => 7;
w = Probe::Widget.new
using_clr_extensions Probe
w.get_Weight   # 7
w.weight       # System::NullReferenceException

using Probe from #50 gives a NoMethodError for the same call. master has the bug too. It's the one #43 lists under "Found, not fixed".

The crash reached further than w.weight:

  • A set_Weight(this Widget, int) extension method broke w.weight = 1 the same way.
  • It broke a type's own property and indexer. On a class with a Weight property and an indexer, extension methods named get_Weight, set_Weight and get_Item made s.weight, s.weight = 5 and s[2] throw.
  • An extension method on an interface broke every class that implements it.
  • get_Heft(this object) made x.Heft throw on every object, 5 included.
  • method, respond_to?, send and clr_member of those names threw too.
  • A Ruby method of the property's name further up the ancestors never got the call. With Object#weight defined, w.weight still threw.

A Debug build threw from an assertion while it built the method group. A Release build cached the broken group, so respond_to?(:weight) returned true and every call threw from GetVisibleOverloads.

Cause

TryGetClrProperty asks GetClrMethods for get_Weight with specialNameOnly set. GetClrMethods dropped the type's own methods that aren't special-name, but returned extension methods without that check. TryGetClrMethod handed MakeGroup the count of what it got, and MakeGroup filtered a second time into an array of that size. The extension method's slot stayed null, and the group's constructor dereferenced it.

using Probe never crashed because #50 keeps its extension methods out of the class's own CLR lookup, which is where this bug was. RubyModule.ResolveMethodNoLock adds them after that lookup, so a property lookup never sees them.

What w.weight should do

It should raise NoMethodError, as using Probe already did. C# makes no property of a classic get_Weight(this Widget), so w.Weight doesn't compile. IronRuby makes no property of an ordinary instance method named get_Weight that isn't an accessor either. For such a class w.weight is a NoMethodError and w.get_Weight calls the method. Extension methods now behave the same way.

Fix

GetClrMethods now filters extension methods the way it already filtered the type's own methods. A property lookup skips get_Weight(this Widget) and finds nothing, or only the type's real accessors. MakeGroup copies the filtered list instead of filtering it again into an array sized by the unfiltered count, so it can't leave a null in a group again.

Ordinary method lookups don't change. Without specialNameOnly, IsVisible only checks access, and IronRuby only picks up public extension methods.

Call Before, using_clr_extensions After, both forms
w.get_Weight 7 7
w.weight, w.Weight NullReferenceException NoMethodError
w.weight = 1 NullReferenceException NoMethodError
w[0], no indexer NoMethodError NoMethodError
w.respond_to?(:weight) NullReferenceException, or true in Release false
w.weight with a Ruby Object#weight NullReferenceException Object#weight
s.weight, s[2], s.weight = 5 NullReferenceException the property and the indexer
s.get_Weight the getter the getter
s.get_Weight(3) the extension method the extension method
x.Heft with get_Heft(this object) NullReferenceException NoMethodError

w is a class with no property. s has a Weight property, an indexer, and extension methods named like their accessors. using Probe already gave the right-hand column. s.get_Weight ties the getter with get_Weight(this Scale), and the instance member wins the tie, as in #43. Receivers with a singleton class and instances of Ruby subclasses of Widget and Scale give the same results.

C# 14 extension properties such as Size don't change. The compiler emits the getter as a static get_Size(Widget) with neither [Extension] nor the special-name flag, so IronRuby's extension discovery never finds it. w.size is a NoMethodError and w.label("x") works through both forms, before and after this PR.

Tests

This branch sits on #50 at 1f7acf0, which is rebased onto Largo's master at d3a7cf9 and includes sholden#5. In #50, using resolves calls the way using_clr_extensions does and puts extension methods only where using_clr_extensions puts them. All results below are from that base, built against a DLR with Largo's patch 0102 applied. None of the accessor-name results changed from the first version of this PR.

  • ClrTests_Globals.cs has a new fixture namespace, IronRubyTests::ExtensionMethods4. Crate has no property or indexer. Scale has a Weight property and an indexer whose accessor names the extension methods reuse. IWeighed is an extended interface and get_Heft extends object. Plain has an instance method named get_Weight for comparison.
  • ClrExtensionMethods9 runs a set of calls after using_clr_extensions, and ClrScopedExtensions6 runs the same calls after using. Both expect the same output. They compare errors by class and NameError#name, not by message, so the change of error messages from `weight' to 'weight' in Error messages quote names 'like this', as Ruby 3.4 and 4.0 do #52 won't break them. The scoped test was ClrScopedExtensions5 until using System::Linq: CLR extension methods scoped like C#'s using directive #50 took that name.
  • Without the fix, 17 of the 29 lines ClrExtensionMethods9 prints are System::NullReferenceException. ClrScopedExtensions6 passes either way. It's there to keep the two forms in step.
  • IronRuby.Tests passes with 0 failures, ClrExtensionMethods0–9 and ClrScopedExtensions1–6 included. spec/language passes with 2935 examples and 0 failures.
  • CI passed on this head, bba7a5c, in all 7 jobs: the build and IronRuby.Tests on Ubuntu and Windows with net8.0 and net10.0, ruby/spec on net8.0 and net10.0, and the no-programs check.
  • Three probe scripts run under no activation, using_clr_extensions and using. The first has 46 calls and covers the table above. The second has 45 and covers what using System::Linq: CLR extension methods scoped like C#'s using directive #50's switch to resolving like using_clr_extensions touches: receivers with a singleton class, send and public_send, and Ruby methods named get_Weight, weight and weight= on Object and on the class. The third has 27 and covers the classes using System::Linq: CLR extension methods scoped like C#'s using directive #50 now limits extension methods to: Ruby subclasses of Widget and Scale, a Ruby class that includes the extended interface, class objects, and Ruby objects reaching get_Heft(this object). Without the fix, 18, 18 and 7 lines throw under using_clr_extensions. With it, the second and third probes print the same through both forms. The first differs in two lines, one for each item under "Found, not fixed": an extension method named like a property, and clr_member. The no-activation and using runs print the same with and without the fix. Debug, Release and -X:PrivateBinding print the same.
  • The runtime commit, 041c8cc, applies cleanly to Largo's master at d3a7cf9, the base of this stack. I cherry-picked it there in a scratch worktree and built it against the same patched DLR. w.weight gives a NoMethodError, the three probes print the same as on this branch without activation and through using_clr_extensions, and IronRuby.Tests passes with 0 failures.

Found, not fixed

One difference between the two forms remains, and it isn't about accessor names. An extension method named like a CLR property or field of the type it extends wins under using_clr_extensions, because the class's CLR lookup tries methods, extension methods included, before properties and fields. Under using, #50 merges it into the property's group, where the DLR's CompareEquivalentParameters prefers the property unless the call spells the extension method's exact name. A field wins outright. With Size(this Sized) beside a Size property and Tag(this Tagged) beside a Tag field:

s.size s.Size t.tag t.Tag
using_clr_extensions extension extension extension extension
using property extension field field

list.count under using System::Linq is the same case. using_clr_extensions binds the two Enumerable.Count overloads alone. using binds a group of List#get_Count and both overloads, and the property wins. The values agree there. This is #50's resolution rule, so I've left it for #50.

clr_member doesn't see extension methods brought in by using. w.clr_member(:get_Weight) returns the extension method through using_clr_extensions and raises NameError through using Probe. method and respond_to? do see them.

Stack

  1. CLR extension methods reach a method group used before using_clr_extensions #43 fixes stale CLR overloads after using_clr_extensions. Largo has merged it.
  2. Refinements: only refined calls pay, and the JIT and OSR keep working #49 makes refinements cost nothing where they can't change a call
  3. using System::Linq: CLR extension methods scoped like C#'s using directive #50 scopes CLR extension methods with using, and now includes 101 LINQ samples: using System::Linq, to_seq as a refinement, blocks, and CI runs them sholden/ironruby#5
  4. This PR. The runtime commit, 041c8cc, also applies to master on its own. The test commit needs using System::Linq: CLR extension methods scoped like C#'s using directive #50's LegacyExtensionsTestOutput helper and using of a namespace.

🤖 Generated with Claude Code

sholden and others added 2 commits October 29, 2026 19:32
After using_clr_extensions, an extension method named like a property
accessor made the property's lookup throw a CLR NullReferenceException:

  public static int get_Weight(this Widget w) => 7;

  using_clr_extensions Probe
  w.get_Weight   # 7
  w.weight       # NullReferenceException

A Debug build threw while it made the method group. A Release build cached
the broken group, so respond_to?(:weight) said true and every call threw.
Extension methods named get_Weight, set_Weight and get_Item did the same to
a type that has a real Weight property and an indexer, and broke its own
w.weight, w.weight = 1 and w[0]. One on an interface broke every class that
implements it, and get_Heft(this object) broke x.Heft on every object.
method, respond_to? and clr_member of those names threw as well.

TryGetClrProperty asks GetClrMethods for get_<name> with specialNameOnly
set. GetClrMethods dropped the type's own methods that aren't special-name
but returned extension methods unfiltered. TryGetClrMethod gave MakeGroup
the count of what it got, and MakeGroup filtered again into an array of
that size, so the extension method's slot stayed null.

GetClrMethods now filters extension methods the way it filters the type's
own. A property lookup skips get_Weight(this Widget) as it skips an
instance method named get_Weight that isn't an accessor. C# makes a
property of neither. The lookup finds nothing, or only the type's real
accessors. MakeGroup copies the filtered list instead of filtering it a
second time.

w.weight is now a NoMethodError, as it is through `using Probe' and for an
instance method named get_Weight. w.get_Weight still returns 7.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
IronRubyTests::ExtensionMethods4 has extension methods named like
accessors. Crate has no property or indexer. Scale has a Weight property
and an indexer whose accessors have the same names as the extension
methods. IWeighed is an extended interface, and get_Heft extends object.
Plain has an instance method named get_Weight that isn't an accessor, for
comparison.

ClrExtensionMethods9 runs the calls after using_clr_extensions and
ClrScopedExtensions8 runs them after `using'. Both expect the same output.
The extension methods answer to their own names, crate.weight and crate[0]
are undefined, Scale's property and indexer still work, and method,
respond_to? and clr_member agree with the calls. The tests compare errors
by class and name, not by message.

ClrExtensionMethods9 fails without the previous commit, with 17 of its 29
lines a NullReferenceException. ClrScopedExtensions8 passes either way and
keeps the two forms in step.

(Rebased onto master, which has its own ClrScopedExtensions6 and 7 from #50: renamed ClrScopedExtensions8.)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Caution

Review failed

The pull request is closed.

Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Largo
Largo merged commit d0e782a into master Oct 29, 2026
7 of 8 checks passed
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.

2 participants