Repository navigation
CLR binding errors: name the plausible overloads, and the broken generic constraint (#141) - #155
Merged
Merged
Conversation
…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]>
|
Warning Review limit reachedEnable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
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. View limit details
|
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.
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)System::Math.abs(nil)/('x')/(Object.new)no implicit conversion of nil into System::SByteno implicit conversion of nil into Integer or FloatSystem::String.IsNullOrEmpty(Object.new)#<Object:0x…> is not a symbol nor a stringno implicit conversion of Object into StringTake(StringBuilder)/Take(Version)called with'x'… into System::Text::StringBuilder or System::Versionnilon the last narrowing level and none can convert it.SelectBestConversionForsaid neither was better, and the DLR then ordered the parameter types and picked the narrowest,Abs(SByte), which failed at run time.RubyOverloadResolvernow returnsAmbiguousfor an argument that both overloads lack a conversion for. A new DLR patch,Build/dlr-patches/0006-SelectBestConversionFor-ambiguous.patch, makesGetPreferredParameterhonour that answer instead of ordering the types. The existingMakeProtocolConversionErrorthen names every target. The baseSelectBestConversionFornever returnsAmbiguous, so the patch changes nothing for other DLR languages. Existing DLR checkouts need the patch applied by hand (Util/check-dlr-patches.sh).MakeOverloadConversionErrortakes the overloads that got furthest through the arguments and lists the targets for that argument.Stringparameter usedConvertToSymbolAction, the conversion for method names. The newConvertToClrStringActionaccepts the same values (String, Symbol,to_str) and gives the String error. Library methods keep the method-name action and MRI's wording.intowording stays, because ruby/spec pins it.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 callsMin(Double, Double)throughFloatLike#to_f(before: a TypeError). A single Float parameter already calledto_fthis 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'sArgumentException, and for a Ruby class it namedIronRuby.Builtins.RubyObject. They now raise, for example:new(),class,structand 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#ofstill selects it.Activator.CreateInstance[T](the example in the issue) has nonew()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
ClrInterop_OverloadConversionErrors1andClrInterop_GenericConstraints1(in the newClrBindingErrorTests.cs, becauseClrInteropSpecTests.csuses BCL types only).language: 2,934 examples, 0 failures, 1 error: the knowninto/towording insend_spec.DLR_REF.Fixes #141
🤖 Generated with Claude Code