C#: Make value types assignable to their Nullable<T> counterparts#7151
Merged
knutwannheden merged 2 commits intomainfrom Mar 26, 2026
Merged
C#: Make value types assignable to their Nullable<T> counterparts#7151knutwannheden merged 2 commits intomainfrom
Nullable<T> counterparts#7151knutwannheden merged 2 commits intomainfrom
Conversation
In C#, a value type T is implicitly convertible to Nullable<T>. The TypeUtils.IsAssignableTo check did not account for this, returning false when checking e.g. int against int?. This caused recipe authors to lack a reliable way to reason about nullable assignability.
Verify that int? is mapped as Parameterized(System.Nullable, [Int]), while string? and object? (nullable reference types) are mapped as plain Class types without Nullable wrapping, matching Roslyn semantics.
6471721 to
4c88387
Compare
Nullable<T> counterparts
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.
Motivation
In C#, a value type
Tis implicitly convertible toNullable<T>(T?). TheTypeUtils.IsAssignableTocheck did not account for this —IsAssignableTo(int, int?)returnedfalse. This matters for C# recipes that need to reason about nullable assignability, such as theThrowIfGreaterThanfamily of recipes whereThrowIfGreaterThan<T>has awhere T : IComparable<T>constraint thatNullable<T>doesn't satisfy.Examples
Summary
Nullable<T>as a special case inIsAssignableTo(JavaType?, JavaType?)— when the target isParameterized(System.Nullable, [T]), unwrap and check if the source is assignable toTNullable<T>→Nullable<T>by unwrapping both sidesTest plan
TypeUtilsTestspass (including 5 new nullable tests)