Skip to content

CLR extension methods named like accessors don't break property lookups - #51

Closed
Largo wants to merge 9 commits into
Largo:masterfrom
sholden:fix/clr-extension-accessor-names
Closed

Largo wants to merge 9 commits into
Largo:masterfrom
sholden:fix/clr-extension-accessor-names

Conversation

@Largo

@Largo Largo commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

Opened upstream from @sholden's branch sholden:fix/clr-extension-accessor-names, unchanged; this is their fork PR sholden#6. It is stacked on #50, so until that merges this diff also shows its commits. The commits this PR adds:

  • 041c8cc Extension methods named like accessors don't break property lookups
  • bba7a5c Tests: extension methods named like accessors, through both activations

The description below is sholden's. Bare PR numbers that were fork-local now point at sholden/ironruby, or at the upstream PR where one exists.


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 9 commits October 29, 2026 08:57
…ective

using_clr_extensions activates a namespace's extension methods for every call
in the runtime: a gem calling it changes what `list.select' means in every
other file. C# scopes them to the files that say `using System.Linq;', and
Ruby has the same notion for refinements, so `using' now accepts a CLR
namespace and brings its extension methods into the rest of the file, class
body or eval string, exactly where a refinement would be active:

  using System::Linq
  list.where { it > 1 }.order_by { -it }.to_a

`using System::Linq::Enumerable' brings in one static class's extension
methods, as C#'s `using static' does.

Extension methods come last, as in C#, rather than at the extended type's
place in the ancestors: a call that finds a Ruby method calls it
(list.select is Enumerable#select), one that finds a CLR method group gets them
as more overloads (list.count(pred) next to List#Count; an exact tie still goes
to the instance method), and one that finds nothing gets them alone
(list.where). The CLR-cased name (list.Select) still reaches them.

Built on the refinement machinery: a CLR namespace in a scope's `using' list is
part of its RefinementActivation, so methods defined there record it, call
sites guard on it, and send/public_send/method/respond_to? and irb see it. The
namespace's extension methods are filed with the types they extend (including
those of assemblies loaded later) under their CLR and Ruby names, and those
names join the per-(module, name) index stage one introduced - only calls of
those names on those types take the lexical guard. The group a call sees is
cached per class version, name and activation.

A program that `using's System::Linq pays nothing where it makes no LINQ call;
a hot LINQ call costs ~12% more than through using_clr_extensions, for the
guard. using_clr_extensions keeps working as before and now warns, once per
process, that it is deprecated.

ClrScopedExtensions1-4 cover the lexical scope, extension methods coming last
(Ruby method, merged CLR group, extension alone; Widget/Square/Box fixtures),
`using static', and a call bound before the first `using' of the namespace.
The tests of using_clr_extensions expect its one warning.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… only lexically

The scoped form put extension methods last, behind every Ruby method, so
`using System::Linq' turned list.select from LINQ's Select (what
using_clr_extensions gives) into Enumerable#select, and code moving from one
form to the other had to rename its calls to their CLR names. The scoped form is
meant to be using_clr_extensions confined to a file, class body or eval string,
so it now resolves the same way.

An extension method sits where a refinement of the type it extends would, and a
class's place also has those of the interfaces it implements, as
using_clr_extensions puts both in the class's own CLR method group:

  using System::Linq
  list.select { it * 2 }.to_a   # LINQ's Select, ahead of Enumerable#select
  dictionary.select { ... }     # LINQ's too, ahead of the IDictionary one
  [3, 1].select { it > 1 }      # Array#select: Array's ancestors have no IEnumerable[T]
  [3, 1].Select { ... }         # NoMethodError, as before

A CLR method group anywhere still gets them as more overloads, the instance member
winning an exact tie (list.count(pred), list.reverse). A Ruby method defined
ahead of them wins, and a site bound to them re-binds when one is defined or
removed. A singleton class sees them, as respond_to? and method did under
using_clr_extensions.

Across 13 receivers (List, Integer[], HashSet, Dictionary, an OfType iterator,
a List with a singleton class, Array, Hash, Range, String, Object, ...) and 30
calls, the two forms now print the same, except that a Ruby class including
IEnumerable[Object] gets LINQ through `using' and doesn't through
using_clr_extensions. 101samples.rb prints the same under both with only its
using line changed.

ClrScopedExtensions2 expects LINQ's select; ClrScopedExtensions5 covers Array,
Dictionary, a singleton, Enumerable#where behind LINQ's on a List and ahead of
nothing on an Array, and a Ruby select defined and removed on List[Integer].

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…sions has them

using_clr_extensions adds extension methods to the CLR method groups of classes
backed by a CLR type, its own and those of its interfaces, and nowhere else. The
scoped form also looked at interface modules themselves, so a Ruby class that
includes IEnumerable[Object] got LINQ through `using' and nothing through
using_clr_extensions. Now only a CLR class's place counts, when deciding whether
extensions shadow a Ruby method and when gathering them into a group.

Against master (14d53a5), over 20 receivers - CLR collections, a List with a
singleton class, Ruby subclasses of List with and without their own select,
class objects, Array, Hash, Range, String, Struct, Set, Enumerator - and 30
calls each: `using System::Linq' prints what master's using_clr_extensions
does, and a program that `using's it in one module prints, outside that
module, what master prints with no activation. using_clr_extensions and no
activation print what they print on master.

ClrScopedExtensions5 also checks the Ruby class including IEnumerable[Object].

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Src/Samples/Linq/101samples.rb stopped parsing when Ruby stopped accepting a
trailing comma in a nested block parameter, |(c,), p|, and nothing ran it
after that. Past the parse error it raised a NameError at FirstOrDefault,
which names Fixnum, gone since Ruby 3.2.

- |(c,), p| is |(c, _), p|.
- Fixnum is Integer, which is System::Int32 in IronRuby.
- Min: Projection called sum, copied from Sum: Projection, and printed the
  total length of the words instead of the shortest. It calls min now,
  through method(:min).of(Object, Integer) as Min: Grouped does. A
  Func<Object, int> fits both Min(Func<TSource, int>) and
  Min<TSource, TResult>(Func<TSource, TResult>), and IronRuby can't choose
  between them where C# picks the first.
- Repeat printed its 7s without a newline, so the next heading ran on.
- OfType was commented out as an IronRuby bug. It works now.

The file still calls using_clr_extensions. Its output is the reference for
moving it to `using'.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The samples were the last code outside the tests that called
using_clr_extensions, so they printed its deprecation warning. They say
`using System::Linq' now. Inside the scope calls resolve as they did under
using_clr_extensions, so every call keeps its name, and the output is the
same apart from the warning.

to_seq, which turns the samples' arrays into the IEnumerable<T> LINQ
extends, was a method added to Object for the whole program. It is a
refinement of Object now, active below the `using' like the LINQ methods.
Nothing in the file calls it before that point.

A probe at each of the 119 call sites with a LINQ method's name saw all of
them run: 112 reach a System.Linq.Enumerable method, and the other 7 are
Array#join on what to_a returned.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Nothing ran Src/Samples/Linq/101samples.rb, so nobody noticed when it stopped
parsing. build-and-test now runs it on each OS and target framework, after the
socket check, and diffs its stdout and stderr against
Src/Samples/Linq/101samples.expected, which holds this branch's output on
Linux and net10.0.

A wrong answer, an exception or a warning fails the step. Tried locally by
putting using_clr_extensions back, which adds its deprecation warning; by
changing count(pred) to where(pred); by raising in the middle; and by
dropping `using LinqSampleHelpers', which makes to_seq undefined. The sample
takes about 1.6 s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The samples passed every LINQ delegate as lambda { ... }. A block goes to a
method's trailing delegate parameter, so a call whose last argument was a
lambda now takes a block:
numbers.where { |n| n < 5 }, group_by(->(p) { p.category }) { |c, g| ... },
aggregate(start_balance) { |balance, next_withdrawal| ... }. A lambda that
isn't the last argument, such as a key selector before a result selector,
and the make_pair and identity helpers are -> lambdas now.

Two indexed samples keep a lambda. On .NET 9 and later OfType returns one
of LINQ's private iterator classes, whose own public Where(Func<T, bool>)
and Select(Func<T, R>) IronRuby binds to. A block with two parameters fits
those too, because a proc takes any number of arguments, so
digits.where { |digit, index| ... } got nil for index and
numbers.select { |num, index| ... } was ambiguous. A lambda's arity rules
them out. The indexed select_many, take_while and skip_while have no such
methods to compete with and take blocks. Each of the two has a comment.

The output is unchanged, so 101samples.expected is too. A probe at each of
the 119 call sites with a LINQ method's name still sees all of them reach
the same methods as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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
ClrScopedExtensions6 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. ClrScopedExtensions6 passes either way and
keeps the two forms in step.

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

coderabbitai Bot commented Oct 29, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 59 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e7ec2262-e113-4406-a26c-60edd973eea8
Commits

Reviewing files that changed from the base of the PR and between 3cc808e and bba7a5c.

Files selected for processing: 16
  • .github/workflows/ci.yml
  • README.md
  • Src/IronRuby.Tests/Helpers.cs
  • Src/IronRuby.Tests/RubyTests.cs
  • Src/IronRuby.Tests/Runtime/ClrBinderTests.cs
  • Src/IronRuby.Tests/Runtime/ClrRuntimeInteropTests.cs
  • Src/IronRuby.Tests/Runtime/ClrTests.cs
  • Src/IronRuby.Tests/Runtime/ClrTests_Globals.cs
  • Src/Libraries/Builtins/KernelOps.cs
  • Src/Libraries/Builtins/ModuleOps.cs
  • Src/Ruby/Builtins/RubyClass.cs
  • Src/Ruby/Builtins/RubyModule.cs
  • Src/Ruby/Runtime/RefinementActivation.cs
  • Src/Ruby/Runtime/RubyContext.cs
  • Src/Samples/Linq/101samples.expected
  • Src/Samples/Linq/101samples.rb
  • 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 commented Oct 29, 2026

Copy link
Copy Markdown
Owner Author

Landed as #61: the same commits rebased onto master (which now has #50 and #56), still authored by @sholden, and merged. One rename in the rebase: this PR's ClrScopedExtensions6 is ClrScopedExtensions8 there, since #50 brought its own 6 and 7. Thanks!

@Largo Largo closed this Oct 29, 2026
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