From aef7b930c0a5774ba78dc0e38a142e667f33a4af Mon Sep 17 00:00:00 2001 From: Tomas Grosup Date: Thu, 7 Nov 2024 08:00:47 +0100 Subject: [PATCH] Fix missing nullness warning in case of multiple applicable candidates for method resolution (e.g. string and ROS) (#17918) * speculativeForoverloads exclusion was a mistake * DateTime not annotated on desktop * release notes --- .../.FSharp.Compiler.Service/9.0.200.md | 1 + src/Compiler/Checking/ConstraintSolver.fs | 3 +-- .../Nullness/NullableReferenceTypesTests.fs | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md b/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md index 0c604f1aa0d..360572c6093 100644 --- a/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md +++ b/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md @@ -9,6 +9,7 @@ * Fix concurrency issue in `ILPreTypeDefImpl` ([PR #17812](https://github.com/dotnet/fsharp/pull/17812)) * Fix nullness inference for member val and other OO scenarios ([PR #17845](https://github.com/dotnet/fsharp/pull/17845)) * Fix internal error when analyzing incomplete inherit member ([PR #17905](https://github.com/dotnet/fsharp/pull/17905)) +* Fix missing nullness warning in case of method resolution multiple candidates ([PR #17917](https://github.com/dotnet/fsharp/pull/17918)) ### Added diff --git a/src/Compiler/Checking/ConstraintSolver.fs b/src/Compiler/Checking/ConstraintSolver.fs index 6c79c33be97..6d59c68c7e8 100644 --- a/src/Compiler/Checking/ConstraintSolver.fs +++ b/src/Compiler/Checking/ConstraintSolver.fs @@ -1035,7 +1035,6 @@ and SolveTypMeetsTyparConstraints (csenv: ConstraintSolverEnv) ndeep m2 trace ty and shouldWarnUselessNullCheck (csenv:ConstraintSolverEnv) = csenv.g.checkNullness && - csenv.IsSpeculativeForMethodOverloading = false && csenv.SolverState.WarnWhenUsingWithoutNullOnAWithNullTarget.IsSome // nullness1: actual @@ -1102,7 +1101,7 @@ and SolveNullnessSubsumesNullness (csenv: ConstraintSolverEnv) m2 (trace: Option | NullnessInfo.WithNull, NullnessInfo.WithoutNull -> CompleteD | NullnessInfo.WithoutNull, NullnessInfo.WithNull -> - if csenv.g.checkNullness && not csenv.IsSpeculativeForMethodOverloading then + if csenv.g.checkNullness then WarnD(ConstraintSolverNullnessWarningWithTypes(csenv.DisplayEnv, ty1, ty2, n1, n2, csenv.m, m2)) else CompleteD diff --git a/tests/FSharp.Compiler.ComponentTests/Language/Nullness/NullableReferenceTypesTests.fs b/tests/FSharp.Compiler.ComponentTests/Language/Nullness/NullableReferenceTypesTests.fs index 0da1169b9a8..2933d00e438 100644 --- a/tests/FSharp.Compiler.ComponentTests/Language/Nullness/NullableReferenceTypesTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/Language/Nullness/NullableReferenceTypesTests.fs @@ -19,6 +19,23 @@ let typeCheckWithStrictNullness cu = +[] +let ``Does report when null goes to DateTime Parse`` () = + + FSharp """module TestLib +open System +let parsedDate = DateTime.Parse(null:(string|null)) +let parseDate2(s:string|null) = DateTime.Parse(s) +let parsedDate3 = DateTime.Parse(null) + """ + |> asLibrary + |> typeCheckWithStrictNullness + |> shouldFail + |> withDiagnostics + [Error 3261, Line 3, Col 18, Line 3, Col 52, "Nullness warning: The types 'string' and 'string | null' do not have compatible nullability." + Error 3261, Line 4, Col 33, Line 4, Col 50, "Nullness warning: The types 'string' and 'string | null' do not have compatible nullability." + Error 3261, Line 5, Col 19, Line 5, Col 39, "Nullness warning: The type 'string' does not support 'null'."] + [] let ``Can convert generic value to objnull arg`` () = FSharp """module TestLib