Repository navigation
Conversation
sholden
force-pushed
the
fix/clr-extension-accessor-names
branch
2 times, most recently
from
October 29, 2026 15:38
da1c08c to
5a9c26c
Compare
…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]>
sholden
force-pushed
the
feature/scoped-clr-extensions
branch
from
October 29, 2026 16:08
0901891 to
1f7acf0
Compare
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]>
sholden
force-pushed
the
fix/clr-extension-accessor-names
branch
from
October 29, 2026 16:15
5a9c26c to
bba7a5c
Compare
This was referenced Oct 29, 2026
sholden
force-pushed
the
feature/scoped-clr-extensions
branch
from
October 29, 2026 17:48
1f7acf0 to
e77892f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
After
using_clr_extensions, a classic extension method named like a property accessor made the lookup of that property throw a CLRNullReferenceException.using Probefrom #3 gives a NoMethodError for the same call. master has the bug too. It's the one Largo#43 lists under "Found, not fixed".The crash reached further than
w.weight:set_Weight(this Widget, int)extension method brokew.weight = 1the same way.Weightproperty and an indexer, extension methods namedget_Weight,set_Weightandget_Itemmades.weight,s.weight = 5ands[2]throw.get_Heft(this object)madex.Heftthrow on every object,5included.method,respond_to?,sendandclr_memberof those names threw too.Object#weightdefined,w.weightstill 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 fromGetVisibleOverloads.Cause
TryGetClrPropertyasksGetClrMethodsforget_WeightwithspecialNameOnlyset.GetClrMethodsdropped the type's own methods that aren't special-name, but returned extension methods without that check.TryGetClrMethodhandedMakeGroupthe count of what it got, andMakeGroupfiltered a second time into an array of that size. The extension method's slot stayed null, and the group's constructor dereferenced it.using Probenever crashed because #3 keeps its extension methods out of the class's own CLR lookup, which is where this bug was.RubyModule.ResolveMethodNoLockadds them after that lookup, so a property lookup never sees them.What
w.weightshould doIt should raise NoMethodError, as
using Probealready did. C# makes no property of a classicget_Weight(this Widget), sow.Weightdoesn't compile. IronRuby makes no property of an ordinary instance method namedget_Weightthat isn't an accessor either. For such a classw.weightis a NoMethodError andw.get_Weightcalls the method. Extension methods now behave the same way.Fix
GetClrMethodsnow filters extension methods the way it already filtered the type's own methods. A property lookup skipsget_Weight(this Widget)and finds nothing, or only the type's real accessors.MakeGroupcopies 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,IsVisibleonly checks access, and IronRuby only picks up public extension methods.using_clr_extensionsw.get_Weightw.weight,w.Weightw.weight = 1w[0], no indexerw.respond_to?(:weight)truein Releasefalsew.weightwith a RubyObject#weightObject#weights.weight,s[2],s.weight = 5s.get_Weights.get_Weight(3)x.Heftwithget_Heft(this object)wis a class with no property.shas aWeightproperty, an indexer, and extension methods named like their accessors.using Probealready gave the right-hand column.s.get_Weightties the getter withget_Weight(this Scale), and the instance member wins the tie, as in Largo#43. Receivers with a singleton class and instances of Ruby subclasses ofWidgetandScalegive the same results.C# 14 extension properties such as
Sizedon't change. The compiler emits the getter as a staticget_Size(Widget)with neither[Extension]nor the special-name flag, so IronRuby's extension discovery never finds it.w.sizeis a NoMethodError andw.label("x")works through both forms, before and after this PR.Tests
This branch sits on #3 at 1f7acf0, which is rebased onto Largo's master at d3a7cf9 and includes #5. In #3,
usingresolves calls the wayusing_clr_extensionsdoes and puts extension methods only whereusing_clr_extensionsputs 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.cshas a new fixture namespace,IronRubyTests::ExtensionMethods4.Cratehas no property or indexer.Scalehas aWeightproperty and an indexer whose accessor names the extension methods reuse.IWeighedis an extended interface andget_Heftextendsobject.Plainhas an instance method namedget_Weightfor comparison.ClrExtensionMethods9runs a set of calls afterusing_clr_extensions, andClrScopedExtensions6runs the same calls afterusing. Both expect the same output. They compare errors by class andNameError#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 #9 won't break them. The scoped test wasClrScopedExtensions5untilusing System::Linq: CLR extension methods scoped like C#'s using directive #3 took that name.ClrExtensionMethods9prints areSystem::NullReferenceException.ClrScopedExtensions6passes either way. It's there to keep the two forms in step.ClrExtensionMethods0–9andClrScopedExtensions1–6included.spec/languagepasses with 2935 examples and 0 failures.using_clr_extensionsandusing. The first has 46 calls and covers the table above. The second has 45 and covers whatusing System::Linq: CLR extension methods scoped like C#'s using directive #3's switch to resolving likeusing_clr_extensionstouches: receivers with a singleton class,sendandpublic_send, and Ruby methods namedget_Weight,weightandweight=onObjectand on the class. The third has 27 and covers the classesusing System::Linq: CLR extension methods scoped like C#'s using directive #3 now limits extension methods to: Ruby subclasses ofWidgetandScale, a Ruby class that includes the extended interface, class objects, and Ruby objects reachingget_Heft(this object). Without the fix, 18, 18 and 7 lines throw underusing_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, andclr_member. The no-activation andusingruns print the same with and without the fix. Debug, Release and-X:PrivateBindingprint the same.w.weightgives a NoMethodError, the three probes print the same as on this branch without activation and throughusing_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. Underusing, #3 merges it into the property's group, where the DLR'sCompareEquivalentParametersprefers the property unless the call spells the extension method's exact name. A field wins outright. WithSize(this Sized)beside aSizeproperty andTag(this Tagged)beside aTagfield:s.sizes.Sizet.tagt.Tagusing_clr_extensionsusinglist.countunderusing System::Linqis the same case.using_clr_extensionsbinds the twoEnumerable.Countoverloads alone.usingbinds a group ofList#get_Countand both overloads, and the property wins. The values agree there. This is #3's resolution rule, so I've left it for #3.clr_memberdoesn't see extension methods brought in byusing.w.clr_member(:get_Weight)returns the extension method throughusing_clr_extensionsand raises NameError throughusing Probe.methodandrespond_to?do see them.Stack
using_clr_extensions. Largo has merged it.using System::Linq: CLR extension methods scoped like C#'s using directive #3 scopes CLR extension methods withusing, and now includes 101 LINQ samples:using System::Linq,to_seqas a refinement, blocks, and CI runs them #5using System::Linq: CLR extension methods scoped like C#'s using directive #3'sLegacyExtensionsTestOutputhelper andusingof a namespace.🤖 Generated with Claude Code