Skip to content
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

Bugfix for breaking change when using Option.ofObj :: Type parameter constraint null implies not struct #18323

Merged
merged 3 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/9.0.300.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Add support for C# `Experimental` attribute. ([PR #18253](https://github.com/dotnet/fsharp/pull/18253))
* Nullness warnings are issued for signature<>implementation conformance ([PR #18186](https://github.com/dotnet/fsharp/pull/18186))
* Symbols: Add FSharpAssembly.IsFSharp ([PR #18290](https://github.com/dotnet/fsharp/pull/18290))
* Type parameter constraint `null` in generic code will now automatically imply `not struct` ([Issue #18320](https://github.com/dotnet/fsharp/issues/18320), [PR #18323](https://github.com/dotnet/fsharp/pull/18323))

### Changed

Expand Down
2 changes: 2 additions & 0 deletions src/Compiler/Checking/ConstraintSolver.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2478,6 +2478,8 @@ and CheckConstraintImplication (csenv: ConstraintSolverEnv) tpc1 tpc2 =
| TyparConstraint.SupportsEquality _, TyparConstraint.SupportsEquality _
// comparison implies equality
| TyparConstraint.SupportsComparison _, TyparConstraint.SupportsEquality _
// 'null' implies reference type ('not struct')
| TyparConstraint.SupportsNull _, TyparConstraint.IsReferenceType _
| TyparConstraint.SupportsNull _, TyparConstraint.SupportsNull _
| TyparConstraint.NotSupportsNull _, TyparConstraint.NotSupportsNull _
| TyparConstraint.IsNonNullableStruct _, TyparConstraint.IsNonNullableStruct _
Expand Down
1 change: 1 addition & 0 deletions src/Compiler/CodeGen/IlxGen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5679,6 +5679,7 @@ and GenGenericParam cenv eenv (tp: Typar) =
tp.Constraints
|> List.exists (function
| TyparConstraint.IsReferenceType _
// 'null' automatically implies 'not struct'
| TyparConstraint.SupportsNull _ -> true
| _ -> false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ let iAcceptNullWithNullAnnotation(arg: 'a | null when 'a: not struct) =
else
0

// This test case emits the `class T` constraint in IL
let iAcceptNullExplicitAnnotation(arg: 'T when 'T:null) =
if isNull arg then
1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ let typeCheckWithStrictNullness cu =
|> withNullnessOptions
|> typecheck


[<Fact>]
let ``Can imply notstruct for classconstraint`` () =
FSharp """module Foo =
let failIfNull<'a when 'a : null> (a : 'a) : 'a option =
(a |> Option.ofObj)
"""
|> asLibrary
|> typecheck // This has nullable off!
|> shouldSucceed

[<Fact>]
let ``Warning on nullness hidden behind interface upcast`` () =
FSharp """module Test
Expand Down