From 599bb693ff677b2c49e3c53c363bbcd53b733548 Mon Sep 17 00:00:00 2001 From: Tomas Grosup Date: Mon, 5 Aug 2024 18:11:39 +0200 Subject: [PATCH] prevent duplicate warnings coming from speculative method resolution (#17477) --- src/Compiler/Checking/ConstraintSolver.fs | 2 +- .../Language/Nullness/NullableReferenceTypesTests.fs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Compiler/Checking/ConstraintSolver.fs b/src/Compiler/Checking/ConstraintSolver.fs index 00f9790d1f0..7371a8ece4d 100644 --- a/src/Compiler/Checking/ConstraintSolver.fs +++ b/src/Compiler/Checking/ConstraintSolver.fs @@ -1101,7 +1101,7 @@ and SolveNullnessSubsumesNullness (csenv: ConstraintSolverEnv) m2 (trace: Option | NullnessInfo.WithNull, NullnessInfo.WithoutNull -> CompleteD | NullnessInfo.WithoutNull, NullnessInfo.WithNull -> - if csenv.g.checkNullness then + if csenv.g.checkNullness && not csenv.IsSpeculativeForMethodOverloading 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 de89ba8f633..ffc875d9015 100644 --- a/tests/FSharp.Compiler.ComponentTests/Language/Nullness/NullableReferenceTypesTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/Language/Nullness/NullableReferenceTypesTests.fs @@ -15,6 +15,18 @@ let typeCheckWithStrictNullness cu = cu |> withNullnessOptions |> typecheck + +[] +let ``Does not duplicate warnings`` () = + FSharp """ +module MyLib +let getLength (x: string | null) = x.Length + """ + |> asLibrary + |> typeCheckWithStrictNullness + |> shouldFail + |> withDiagnostics [Error 3261, Line 3, Col 36, Line 3, Col 44, "Nullness warning: The types 'string' and 'string | null' do not have compatible nullability."] + [] let ``Cannot pass possibly null value to a strict function``() =