-
Notifications
You must be signed in to change notification settings - Fork 824
Fix IEquatable<T> nullness contravariance to match C# behavior #18770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix IEquatable<T> nullness contravariance to match C# behavior #18770
Conversation
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
tests/FSharp.Compiler.ComponentTests/Language/Nullness/NullableReferenceTypesTests.fs
Outdated
Show resolved
Hide resolved
…eReferenceTypesTests.fs
❗ Release notes required
Warning No PR link found in some release notes, please consider adding it.
|
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addressesI tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
… .gitignore Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good.
Just add release notes instructions for this in the usual format in docs release notes.
Treat this as "fixed" bug for upcasts to equatable.
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Added release notes entry for the IEquatable nullness fix in the preview.md file, treating it as a fixed bug for upcasts to equatable. Changes committed in 49de3f3. |
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Problem
The F# compiler was emitting nullness warnings when casting non-nullable values to
IEquatable<T>
, while C# treatsIEquatable<T>
as contravariant for nullness purposes and suppresses such warnings. This inconsistency created friction when working with F# code that needed to match C# nullness behavior.Solution
Extended the constraint solver to treat
IEquatable<T>
type parameters as contravariant for nullness purposes, even though they're not formally marked as contravariant in IL. This matches the behavior implemented in Roslyn for C#.Changes Made
ConstraintSolver.fs: Modified
SolveTypeEqualsTypeWithContravarianceEqns
to:TyconRef
parameter to identify the type constructorisIEquatableContravariantForNullness
helper function that checks forIEquatable<T>
IEquatable<T>
parameter 0 as contravariant for nullnessNullableReferenceTypesTests.fs: Added test case
No nullness warning when casting non-nullable to IEquatable
to validate the fixValidation
IEquatable<T>
contravarianceReferences
The fix is minimal and surgical, only affecting the specific
IEquatable<T>
nullness contravariance case while preserving all other nullness checking behavior.Warning
Firewall rules blocked me from connecting to one or more addresses
I tried to connect to the following addresses, but was blocked by firewall rules:
1p7vsblobprodcus324.vsblob.vsassets.io
/usr/share/dotnet/dotnet msbuild -v:quiet -restore /home/REDACTED/.packagemanagement/nuget/Projects/112289--a67d0eeb-92ff-4473-9f1c-a88ee35e0deb/Project.fsproj /nologo /t:InteractivePackageManagement
(dns block)m8dvsblobprodcus37.vsblob.vsassets.io
/usr/share/dotnet/dotnet msbuild -v:quiet -restore /home/REDACTED/.packagemanagement/nuget/Projects/112289--a67d0eeb-92ff-4473-9f1c-a88ee35e0deb/Project.fsproj /nologo /t:InteractivePackageManagement
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
This pull request was created as a result of the following prompt from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.