fix: Emit string.Concat instead of Expression.Add for string + operator#2
Merged
fix: Emit string.Concat instead of Expression.Add for string + operator#2
Conversation
String concatenation via the + operator was incorrectly emitting Expression.MakeBinary(ExpressionType.Add, ...) which is only valid for numeric types. The built-in C# string + is a compiler intrinsic with no OperatorMethod, so it fell through to the plain MakeBinary path. Now emits Expression.Call(string.Concat, ...) matching the interpolation codepath, with Concat(string,string) for homogeneous operands and Concat(object,object) for mixed types. Also fixes the same latent bug in EmitCompoundAssignment for string +=. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes expression-tree emission for C# string concatenation so that +/+= producing string are emitted as Expression.Call(string.Concat, ...) rather than Expression.MakeBinary(ExpressionType.Add, ...), preventing runtime InvalidOperationException for string addition scenarios and aligning with the interpolation emission path.
Changes:
- Update generator emission for string
+andstring +=to callstring.Concat(string/string vs object/object overload selection). - Add new generator snapshot tests covering string concatenation variants.
- Add an integration test scenario/property to validate runtime correctness when executing expanded expressions.
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/ExpressiveSharp.Generator/Emitter/ExpressionTreeEmitter.cs | Emits string.Concat for + / += when the result type is string and the operator is intrinsic. |
| tests/ExpressiveSharp.IntegrationTests/Scenarios/Store/Models/Order.cs | Adds an [Expressive] property using + string concatenation for integration coverage. |
| tests/ExpressiveSharp.IntegrationTests/Scenarios/Common/Tests/StringOperationTests.cs | Adds integration test validating SummaryConcat expansion/runtime execution. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/StringConcatenationTests.cs | New snapshot tests for string concatenation emission. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/StringConcatenationTests.StringPlusString.verified.txt | New verified snapshot asserting Concat(string,string) emission. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/StringConcatenationTests.IntPlusString.verified.txt | New verified snapshot asserting Concat(object,object) + Concat(string,string) emission. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/StringConcatenationTests.SingleStringConcat.verified.txt | New verified snapshot asserting Concat(string,string) emission. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/ExtensionMemberTests.ExtensionMemberWithMemberAccess.verified.txt | Updates snapshot to use Expression.Call(string.Concat, ...) instead of ExpressionType.Add. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/ExtensionMemberTests.ExtensionMemberOnInterface.verified.txt | Updates snapshot to use Expression.Call(string.Concat, ...) instead of ExpressionType.Add. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/ConstructorTests.ProjectableConstructor_WithSwitchExpression_AndExtraProperty.verified.txt | Updates snapshot string building to use string.Concat calls. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/ConstructorTests.ProjectableConstructor_WithSwitchExpression.verified.txt | Updates snapshot string building to use string.Concat calls. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/ConstructorTests.ProjectableConstructor_WithMultipleLocalVariables.verified.txt | Updates snapshot string building to use string.Concat calls. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/ConstructorTests.ProjectableConstructor_WithLocalVariable.verified.txt | Updates snapshot string building to use string.Concat calls. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/ConstructorTests.ProjectableConstructor_WithFullObject.verified.txt | Updates snapshot string building to use string.Concat calls (and renumbers cached MethodInfos). |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/ConstructorTests.ProjectableConstructor_ThisInitializer_WithIfElseInDelegated.verified.txt | Updates snapshot string building to use string.Concat calls. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/ConstructorTests.ProjectableConstructor_ThisInitializer_WithBodyAfter.verified.txt | Updates snapshot string building to use string.Concat calls. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/ConstructorTests.ProjectableConstructor_ThisInitializer_ChainedThisAndBase.verified.txt | Updates snapshot string building to use string.Concat calls. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/ConstructorTests.ProjectableConstructor_ReferencingStaticConstMember.verified.txt | Updates snapshot string building to use string.Concat calls. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/ConstructorTests.ProjectableConstructor_ReferencingPreviouslyAssignedProperty.verified.txt | Updates snapshot string building to use string.Concat calls. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/ConstructorTests.ProjectableConstructor_ReferencingBasePropertyInDerivedBody.verified.txt | Updates snapshot string building to use string.Concat calls. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The fallback paths set field names but never emitted corresponding MethodInfo declarations, which would produce uncompilable generated code. Since string.Concat(string,string) and string.Concat(object,object) exist in all .NET versions, replace with InvalidOperationException. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
Summary
+operator was incorrectly emittingExpression.MakeBinary(ExpressionType.Add, ...)which is only valid for numeric types — at runtime this throwsInvalidOperationExceptionExpression.Call(string.Concat, ...), matching the existing interpolation codepathConcat(string, string)when both operands are strings,Concat(object, object)for mixed types (e.g.int + stringwith boxing)EmitCompoundAssignmentforstring +=OperatorMethod is null(built-in compiler intrinsic)Test plan
StringPlusString,IntPlusString,SingleStringConcat)+)Select_SummaryConcat_ReturnsCorrectValues) verifying runtime correctness🤖 Generated with Claude Code