Emit explicit element type for nullable value type arrays in CSharpHe… - #38712
Open
ilkertskn wants to merge 1 commit into
Open
Emit explicit element type for nullable value type arrays in CSharpHe…#38712ilkertskn wants to merge 1 commit into
ilkertskn wants to merge 1 commit into
Conversation
…lper
- A non-empty array literal is normally emitted as an implicitly-typed array (new[] { ... }),
but nullable value type elements are rendered as their underlying type, so the array is
inferred as the non-nullable type (e.g. int[] instead of int?[]), producing code that does
not compile
- Emit the element type explicitly for nullable value type arrays, matching the existing
handling for byte and object arrays
- Add tests for nullable value type arrays with and without null elements
Contributor
Author
|
@dotnet-policy-service agree |
There was a problem hiding this comment.
Pull request overview
Fixes C# literal generation for arrays of nullable value types in CSharpHelper by ensuring the emitted array literal preserves the intended nullable element type (e.g. int?[]) instead of being inferred as the underlying non-nullable type (e.g. int[]), which can cause generated code to fail compilation when assigned/used as T?[].
Changes:
- Emit an explicit element type for non-empty arrays when the element type is a nullable value type (
Nullable<T>), avoiding incorrect implicit array type inference. - Add coverage to
CSharpHelperTestfor nullable value type arrays both with and withoutnullelements.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/EFCore.Design.Tests/Design/Internal/CSharpHelperTest.cs | Adds tests asserting correct literal emission for int?[] arrays (with/without null). |
| src/EFCore.Design/Design/Internal/CSharpHelper.cs | Updates array literal emission to explicitly include the nullable element type for Nullable<T> arrays. |
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.
…lper