Repository navigation
CLR extension methods named like accessors don't break property lookups (rebased) - #61
Merged
Merged
Conversation
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]>
|
Caution Review failedThe pull request is closed. Finishing Touches
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. Comment |
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.
#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:
One change in the rebase: master already has a
ClrScopedExtensions6(and 7) from #50, so this PR'sClrScopedExtensions6is nowClrScopedExtensions8.Checked together with #55, all of them merged onto master 62655f8 (Debug, net8.0, Linux):
regen-initializers.shleavesInitializers.Generated.csunchanged; 0 build warnings; IronRuby.Tests 0 failures;spec/language2934 examples, 0 failures;spec/coreexception, mutex, conditionvariable, refinement,Module#refine/#using,main.usingandKernel#requireall 0 failures; the 101 LINQ samples print exactly101samples.expected;Util/error-message-matrix.rb,refinement-binding-matrix.rb,jit-matrix.rbandcondvar-matrix.rbmatch CRuby 4.0.6 line for line, andrefinement-matrix.rbdiffers 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 CLRNullReferenceException.using Probefrom #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: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 #50 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 #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 #50 at 1f7acf0, which is rebased onto Largo's master at d3a7cf9 and includes sholden#5. In #50,
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 #52 won't break them. The scoped test wasClrScopedExtensions5untilusing System::Linq: CLR extension methods scoped like C#'s using directive #50 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 #50'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 #50 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, #50 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 #50's resolution rule, so I've left it for #50.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 #50 scopes CLR extension methods withusing, and now includes 101 LINQ samples:using System::Linq,to_seqas a refinement, blocks, and CI runs them sholden/ironruby#5using System::Linq: CLR extension methods scoped like C#'s using directive #50'sLegacyExtensionsTestOutputhelper andusingof a namespace.🤖 Generated with Claude Code