Skip to content

Fix CompileFast returning wrong values for member access on an interface converted to a struct - #554

Merged
dadhi merged 2 commits into
masterfrom
copilot/fix-compilefast-interface-struct-issue
Oct 7, 2026
Merged

dadhi merged 2 commits into
masterfrom
copilot/fix-compilefast-interface-struct-issue

Conversation

Copilot AI commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

CompileFast() returned wrong results for ((Start)x).Member when x is an interface-typed value and Start is a struct. Depending on the member, it returned the struct itself, default, or a different member's value. Compile() and the object → struct case were correct.

// x is IHasId, Start : IHasId is a struct
Expression<Func<IHasId, object>> e = x => ((Start)x).Number;
e.Compile()(new Start());      // 42
e.CompileFast()(new Start());  // 0 (before this change)
  • Root cause: TryEmitConvert only emitted unbox.any when the source type was exactly object (or Enum). For an interface source it fell through to the generic path, which left the boxed reference on the stack. The member access then treated that reference as a struct value or address.
  • Fix: TryEmitConvert in FastExpressionCompiler.cs now also emits unbox.any when the source is an interface that the target value type implements. Interface → class conversions still go through castclass.
  • Tests: new Issue553_* test in FastExpressionCompiler.IssueTests, compiled into the LightExpression variant too. It covers a string property, an int property, a field and a method call on (Start)interface. Both variants are registered in TestsRunner/Program.cs.

The fix only covers interfaces. Other non-value source types that a struct can convert from, such as ValueType, are not touched here.

Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix CompileFast returning wrong values for interface converted to struct Fix CompileFast returning wrong values for member access on an interface converted to a struct Oct 7, 2026
Copilot AI requested a review from dadhi October 7, 2026 21:14
@dadhi
dadhi marked this pull request as ready for review October 7, 2026 21:22
@dadhi
dadhi merged commit 44dee3a into master Oct 7, 2026
2 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.

CompileFast returns wrong values when a member is accessed on an interface converted to a struct

2 participants