Skip to content

CLR binding errors: name the plausible overloads, and the broken generic constraint (#141) - #155

Merged
Largo merged 2 commits into
masterfrom
claude/fix-141-overload-errors
Nov 8, 2026
Merged

Largo merged 2 commits into
masterfrom
claude/fix-141-overload-errors

Conversation

@Largo

@Largo Largo commented Nov 8, 2026

Copy link
Copy Markdown
Owner

The two parts of #141 that #148 left open: misleading conversion errors from CLR overload resolution, and the error for a type argument that breaks a generic constraint.

Conversion errors (CLR calls that cannot bind name the plausible conversions)

Call Before After
System::Math.abs(nil) / ('x') / (Object.new) no implicit conversion of nil into System::SByte no implicit conversion of nil into Integer or Float
System::String.IsNullOrEmpty(Object.new) #<Object:0x…> is not a symbol nor a string no implicit conversion of Object into String
Take(StringBuilder)/Take(Version) called with 'x' names the first overload tried … into System::Text::StringBuilder or System::Version
  • Abs(nil): every numeric overload accepts nil on the last narrowing level and none can convert it. SelectBestConversionFor said neither was better, and the DLR then ordered the parameter types and picked the narrowest, Abs(SByte), which failed at run time. RubyOverloadResolver now returns Ambiguous for an argument that both overloads lack a conversion for. A new DLR patch, Build/dlr-patches/0006-SelectBestConversionFor-ambiguous.patch, makes GetPreferredParameter honour that answer instead of ordering the types. The existing MakeProtocolConversionError then names every target. The base SelectBestConversionFor never returns Ambiguous, so the patch changes nothing for other DLR languages. Existing DLR checkouts need the patch applied by hand (Util/check-dlr-patches.sh).
  • Bind-time failures: MakeOverloadConversionError takes the overloads that got furthest through the arguments and lists the targets for that argument.
  • String parameters: a CLR method's String parameter used ConvertToSymbolAction, the conversion for method names. The new ConvertToClrStringAction accepts the same values (String, Symbol, to_str) and gives the String error. Library methods keep the method-name action and MRI's wording.
  • The into wording stays, because ruby/spec pins it.
  • Behaviour change (in ClrOverloadProtocolConversions1, whose expectations are updated): if another argument prefers an overload, the binder now calls that one. Math.max(Time.at(100), 1) fails with "into Integer" (before: "Integer or Float"). Math.min(FloatLike.new, 1.5) now calls Min(Double, Double) through FloatLike#to_f (before: a TypeError). A single Float parameter already called to_f this way.

Generic constraints (Type arguments that break a generic constraint raise a clear ArgumentError)

Method#of, UnboundMethod#of, Module#[] and a type group's [] used to pass on the CLR's ArgumentException, and for a Ruby class it named IronRuby.Builtins.RubyObject. They now raise, for example:

Foo does not satisfy the new() constraint on T of 'make_new': a Ruby class has no public parameterless CLR constructor
Integer does not satisfy the class constraint on T of 'ref_only': it is a value type
Foo does not satisfy the struct constraint on T of 'Nullable': it is not a non-nullable value type

new(), class, struct and type bounds are checked. A constraint between parameters (where S : T) is left to the CLR and becomes a generic ArgumentError. If another overload of the same arity has constraints the arguments meet, Method#of still selects it.

Activator.CreateInstance[T] (the example in the issue) has no new() constraint. With a Ruby class it still fails inside Activator, with "No parameterless constructor defined".

Not covered

The issue also lists "a".to_clr_string.foo! (a TypeError, "Mutating method", where a NoMethodError was expected). Neither #148 nor this PR changes it.

Testing

  • New tests ClrInterop_OverloadConversionErrors1 and ClrInterop_GenericConstraints1 (in the new ClrBindingErrorTests.cs, because ClrInteropSpecTests.cs uses BCL types only).
  • IronRuby.Tests: all 1,090 pass, Linux net10.0.
  • ruby/spec language: 2,934 examples, 0 failures, 1 error: the known into/to wording in send_spec.
  • The patch set, 0006 included, applies to a clean DLR at DLR_REF.

Fixes #141

🤖 Generated with Claude Code

Largo and others added 2 commits November 8, 2026 21:46
…Error

Method#of, UnboundMethod#of, Module#[] and a type group's [] passed the
CLR's ArgumentException through when a type argument broke a constraint,
and for a Ruby class it named the CLR type behind it:
"GenericArguments[0], 'IronRuby.Builtins.RubyObject', on '...' violates
the constraint of type 'T'".

Protocols.FindGenericConstraintViolation checks the new(), class and
struct constraints and type bounds, and CreateGenericConstraintError
names the argument as Ruby code wrote it and the constraint:
"Foo does not satisfy the new() constraint on T of 'make_new': a Ruby
class has no public parameterless CLR constructor". A constraint
between the parameters themselves (where S : T) is left to the CLR,
whose exception becomes a generic ArgumentError. An overload whose
constraints the arguments break is no longer one Method#of selects, so
another overload of the same arity still can be.

Activator.CreateInstance[T] has no new() constraint, so with a Ruby
class it still fails inside Activator ("No parameterless constructor").

Checked with ClrInterop_GenericConstraints1 (in ClrBindingErrorTests.cs,
which the next commit adds with its own test) and the full IronRuby.Tests
suite on Linux net10.0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
When no overload of a CLR method could take the arguments, the TypeError
named the conversion of an overload that was never the plausible one:

- System::Math.abs(nil) said "no implicit conversion of nil into
  System::SByte". Every Abs overload accepts nil on the last narrowing
  level (by to_int or to_f) and none can convert it, but the DLR then
  ordered the parameter types and picked the narrowest, Abs(SByte), whose
  conversion failed at run time. RubyOverloadResolver now answers
  Ambiguous for an argument that both overloads lack a conversion for,
  and a new DLR patch (0006) makes GetPreferredParameter honour that
  instead of ordering the types. The call is then ambiguous, and the
  existing MakeProtocolConversionError names every target: "no implicit
  conversion of nil into Integer or Float" (MRI's Math.sqrt(nil) says
  "into Float"). The base SelectBestConversionFor never returns
  Ambiguous, so the patch changes nothing for other DLR languages.
- Overloads that each fail to convert some argument at bind time
  (Take(StringBuilder), Take(Version) called with a String) reported the
  first overload tried. MakeOverloadConversionError takes the overloads
  that got furthest through the arguments and lists what they would have
  converted that argument to.
- A CLR method's String parameter converted with the action for method
  names, so String.IsNullOrEmpty(Object.new) said "#<Object...> is not a
  symbol nor a string". ConvertToClrStringAction accepts the same values
  (String, Symbol, to_str) and says "no implicit conversion of Object
  into String". Library methods keep ConvertToSymbolAction and MRI's
  wording for a bad method name.

The "into" wording stays, as ruby/spec pins it.

ClrOverloadProtocolConversions1 changes with it: o.real(nil), (Time),
(:sym) and (FloatLike) name "Integer or Float" instead of the narrowest
overload's Integer. Where another argument prefers an overload, that
one is now called: Math.max(Time.at(100), 1) fails "into Integer", and
Math.min(FloatLike.new, 1.5) is Min(Double, Double) by FloatLike#to_f,
as a lone Float parameter already was.

Checked with the new ClrInterop_OverloadConversionErrors1, the full
IronRuby.Tests suite (1,090 tests, all pass) and ruby/spec's language
suite (only the known into/to error in send_spec) on Linux net10.0.

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

coderabbitai Bot commented Nov 8, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

  • Ask an admin to enable usage-based reviews

Open in CodeRabbit

Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing.

Next included review available in 43 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available. Your 53 included PR review attempts over the past 7 days set your current allowance at 2 reviews per hour.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 468c0db and 9193cdc.

Files selected for processing: 13

  • Build/dlr-patches/0006-SelectBestConversionFor-ambiguous.patch
  • Build/dlr-patches/README.md
  • Src/IronRuby.Tests/RubyTests.cs
  • Src/IronRuby.Tests/Runtime/ClrBindingErrorTests.cs
  • Src/IronRuby.Tests/Runtime/ClrOverloadResolutionTests.cs
  • Src/Libraries/Builtins/MethodOps.cs
  • Src/Libraries/Builtins/ModuleOps.cs
  • Src/Libraries/Extensions/TypeGroupOps.cs
  • Src/Ruby/Runtime/Calls/RubyMethodGroupBase.cs
  • Src/Ruby/Runtime/Calls/RubyOverloadResolver.cs
  • Src/Ruby/Runtime/Conversions/Converter.cs
  • Src/Ruby/Runtime/Conversions/ProtocolConversionAction.cs
  • Src/Ruby/Runtime/Protocols.cs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@Largo
Largo merged commit 486545c into master Nov 8, 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.

Interop rough edges: Clr::Name.new(nil), new() constraint, generic inference from Method, conversion messages

1 participant