You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fixes System.IndexOutOfRangeException (and InvalidProgramException for non-abstract classes) thrown by CompileFast when using implicit conversion operators declared on an abstract or non-abstract base class within a closure.
Root Cause
In TryEmitConvert, the else if (methodParamType != sourceType) branch unconditionally assumed that whenever the conversion operator's parameter type differs from the source type, it must be a Nullable<sourceType> wrapping case. It called GetNullableConstructor() → type.GetConstructors()[0], but abstract classes have no public constructors, so this returned an empty array → IndexOutOfRangeException. For non-abstract base classes it emitted a wrong newobj instruction → InvalidProgramException.
Fix
In src/FastExpressionCompiler/FastExpressionCompiler.cs, updated the else if (methodParamType != sourceType) branch to only emit Newobj when Nullable.GetUnderlyingType(methodParamType) == sourceType. For the polymorphic base class case (methodParamType is a base type/interface of sourceType), no additional emit is needed — the value already on the stack is assignment-compatible with the conversion method's parameter.
Test
Added regression test Issue500_IndexOutOfRangeException_with_value_objects_implicit_conversions (registered for both FastExpressionCompiler and FastExpressionCompiler.LightExpression) covering:
Abstract base class + closure — the exact case from the original report
Non-abstract base class + closure — previously caused InvalidProgramException
Direct parameter with explicit method reference — exercises the same code path without a closure
Done. The root cause was in TryEmitConvert (commit ffd609d): when an implicit conversion operator is declared on an abstract/base class (e.g., PrimitiveValueObject<string, MyPrimitive>), the parameter type differs from the source type (MyPrimitive). The code incorrectly assumed this always meant a Nullable<sourceType> wrapping case and called GetNullableConstructor() → GetConstructors()[0] → IndexOutOfRangeException for abstract classes (no public constructors).
Fix: In the else if (methodParamType != sourceType) branch, only emit Newobj when Nullable.GetUnderlyingType(methodParamType) == sourceType. For the polymorphic base class case, no extra emit is needed since the value on the stack is already assignment-compatible.
Added regression test Issue500 with 3 cases: abstract base class closure, non-abstract base class closure, and direct parameter with explicit method. All 1672 tests pass.
CopilotAI
changed the title
[WIP] Fix IndexOutOfRangeException with value objects conversions
Fix IndexOutOfRangeException with value objects implicit conversions
Apr 27, 2026
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
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.
Fixes
System.IndexOutOfRangeException(andInvalidProgramExceptionfor non-abstract classes) thrown byCompileFastwhen using implicit conversion operators declared on an abstract or non-abstract base class within a closure.Root Cause
In
TryEmitConvert, theelse if (methodParamType != sourceType)branch unconditionally assumed that whenever the conversion operator's parameter type differs from the source type, it must be aNullable<sourceType>wrapping case. It calledGetNullableConstructor()→type.GetConstructors()[0], but abstract classes have no public constructors, so this returned an empty array →IndexOutOfRangeException. For non-abstract base classes it emitted a wrongnewobjinstruction →InvalidProgramException.Fix
In
src/FastExpressionCompiler/FastExpressionCompiler.cs, updated theelse if (methodParamType != sourceType)branch to only emitNewobjwhenNullable.GetUnderlyingType(methodParamType) == sourceType. For the polymorphic base class case (methodParamTypeis a base type/interface ofsourceType), no additional emit is needed — the value already on the stack is assignment-compatible with the conversion method's parameter.Test
Added regression test
Issue500_IndexOutOfRangeException_with_value_objects_implicit_conversions(registered for bothFastExpressionCompilerandFastExpressionCompiler.LightExpression) covering:InvalidProgramExceptionAll 1672 tests pass.