Skip to content

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

Merged
Largo merged 1 commit into
Largo:masterfrom
sholden:fix/clr-extensions-stale-overloads
Oct 29, 2026
Merged

Largo merged 1 commit into
Largo:masterfrom
sholden:fix/clr-extensions-stale-overloads

Conversation

@sholden

@sholden sholden commented Oct 29, 2026 •

Copy link
Copy Markdown

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

This is the first of a stack. The next two are open in my fork for now and come here once this merges:

  1. This PR
  2. Refinements: only refined calls pay, and the JIT and OSR keep working sholden/ironruby#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 sholden/ironruby#3 scopes CLR extension methods with using

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved CLR extension-method handling when extension names overlap with existing Ruby method groups. Overload selection now works more consistently for inherited methods, generic types, and interface-based extensions, while preserving existing instance-method behavior.
    • Improved recognition of LINQ extension methods on lists, including predicate-based count calls.

@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
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: 5b99af73-d3d1-45c5-9e63-854a3f20748a
Commits

Reviewing files that changed from the base of the PR and between f1ddae6 and afdefbf.

Files selected for processing: 4
  • Src/IronRuby.Tests/RubyTests.cs
  • Src/IronRuby.Tests/Runtime/ClrTests.cs
  • Src/IronRuby.Tests/Runtime/ClrTests_Globals.cs
  • Src/Ruby/Builtins/RubyClass.cs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Walkthrough

The change updates CLR method-group cache invalidation to include extension methods’ Ruby-mangled names. It adds extension fixtures and tests for overload selection across class, inherited, interface, generic, and LINQ method groups.

Changes: CLR extension method lookup

Layer / File(s) Summary
Method-group cache invalidation
Src/Ruby/Builtins/RubyClass.cs
PrepareExtensionMethodsUpdate now invalidates subclass method groups using each extension method’s CLR name and, when available, its Ruby-mangled name.
Extension lookup fixtures and tests
Src/IronRuby.Tests/Runtime/ClrTests_Globals.cs, Src/IronRuby.Tests/Runtime/ClrTests.cs, Src/IronRuby.Tests/RubyTests.cs
Added extension fixtures and registered tests for class, inherited, interface, generic, and LINQ method groups, including behavior before and after extension activation.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Possibly related PRs

  • Largo/ironruby#22: Adds method-lookup caches and version guards; both changes address extension-aware method cache invalidation.

Suggested reviewers: largo

Merge Risk: Minimal · up to afdef

No actionable merge-blocking risk was established. The reported property-getter issue remains outside this change.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: CLR extension methods reaching a method group that was already used before using_clr_extensions. The objectives describe a stale-cache fix in PrepareExtensionMethodsUp…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: CLR extension methods reaching a method group that was already used before using_clr_extensions. The objectives describe a stale-cache fix in PrepareExtensionMethodsUpdate that clears CLR method groups under both the Ruby name and the CLR name, so the title matches the changeset. The title is specific and identifies the bug area without listing files.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 4 files.

Finishing Touches
🧪 Generate unit tests (beta)
  • 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.

…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]>
@Largo Largo self-assigned this Oct 29, 2026
@Largo
Largo force-pushed the fix/clr-extensions-stale-overloads branch from 2c21b91 to afdefbf Compare October 29, 2026 14:35
@Largo
Largo merged commit c1b360b into Largo:master Oct 29, 2026
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