Skip to content

CLR extension methods reach a method group used before using_clr_extensions - #1

Closed
sholden wants to merge 1 commit into
masterfrom
fix/clr-extensions-stale-overloads
Closed

sholden wants to merge 1 commit into
masterfrom
fix/clr-extensions-stale-overloads

Conversation

@sholden

@sholden sholden commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

A CLR method called before using_clr_extensions never saw the extension overloads the activation added.

require 'Demo.dll'
w = Demo::Widget.new
w.describe                 # binds Widget#Describe
using_clr_extensions Demo  # adds WidgetExtensions.Describe(this Widget, string)
w.describe("x")            # TypeError: no implicit conversion of String into Integer

Without the first w.describe, the last line returns the extension's result.

Cause

IronRuby caches a CLR method group in the class's method table under the name it was looked up by, so w.describe files Widget's Describe overloads under describe. When using_clr_extensions added more Describe overloads, RubyClass.PrepareExtensionMethodsUpdate cleared the cached groups under the CLR name Describe and nothing else. The group under describe survived and kept serving the old overloads, and method(:describe) returned it too. Calling w.Describe worked, which is what gave the cause away.

Extensions on interfaces and generic type definitions went stale the same way. After one List[Integer]#count call, using_clr_extensions System::Linq could no longer add Enumerable.Count's overloads, so list.count(->(x) { x > 1 }) raised an ArgumentError.

Fix

PrepareExtensionMethodsUpdate now clears the cached groups under the Ruby name as well as the CLR name, once per distinct name. An exact signature tie still goes to the instance method.

Tests

The fixture is a new IronRubyTests::ExtensionMethods3 namespace in ClrTests_Globals.cs. Its methods are PascalCase so the Ruby and CLR names differ. The older extension tests all use a lowercase f, which is why none of them caught this.

  • ClrExtensionMethods6 covers a class, its C# and Ruby subclasses, the exact tie, the Describe spelling and method(:describe).
  • ClrExtensionMethods7 covers an interface and a generic class.
  • ClrExtensionMethods8 covers List[Integer]#count with System::Linq.

All three fail with the fix reverted. The full IronRuby.Tests suite passes on net10.0.

Found, not fixed

A classic extension method named like a property getter, such as get_Weight(this Widget w), makes w.weight throw a NullReferenceException once it's activated. The code that builds a property's method group seems to size its array before it drops the methods that aren't compiler-generated accessors, which leaves a null in it.

Stack

  1. CLR extension methods reach a method group used before using_clr_extensions #1, this PR
  2. Refinements: only refined calls pay, and the JIT and OSR keep working #2 makes refinements cost nothing where they can't change a call
  3. using System::Linq: CLR extension methods scoped like C#'s using directive #3 scopes CLR extension methods with using

🤖 Generated with Claude Code

…nsions

A CLR method group is stored in the method table under the name it was looked
up by: w.describe stores Widget's Describe overloads under "describe". When
using_clr_extensions activated more Describe overloads, the groups were
invalidated under the extension's CLR name, "Describe", so the group under
"describe" survived and kept serving the old overloads. A call that resolved
describe before the activation made describe("x") a TypeError (no conversion
of String into Integer) afterwards, and method(:describe) returned the same
stale group. This affected extensions on a class, on an interface, and on a
generic definition (List[Integer]#count before System::Linq lost
Enumerable.Count's overloads).

The invalidation now covers the mangled name as well as the CLR name.

ClrExtensionMethods6-8 cover a class, its CLR and Ruby subclasses and
method(:describe); an interface and a generic type definition; and
List#count with System::Linq.

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

sholden commented Oct 29, 2026

Copy link
Copy Markdown
Owner Author

Moved upstream: Largo#43. The branch stays.

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.

1 participant