From bc6cfd363a78f15cd960a9702199b9ac85635ded Mon Sep 17 00:00:00 2001 From: Will Smith Date: Thu, 1 Apr 2021 17:31:50 -0700 Subject: [PATCH 1/2] Fixed regression for Nullable types --- src/fsharp/MethodCalls.fs | 7 +-- .../Compiler/Language/OptionalInteropTests.fs | 1 + .../NullableOptionalRegressionTests.fs | 63 +++++++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/fsharp/MethodCalls.fs b/src/fsharp/MethodCalls.fs index 07bd84c44f5..6330a18593b 100644 --- a/src/fsharp/MethodCalls.fs +++ b/src/fsharp/MethodCalls.fs @@ -214,11 +214,10 @@ let AdjustCalledArgTypeForOptionals (g: TcGlobals) enforceNullableOptionalsKnown calledArgTy // If at the beginning of inference then use a type variable. else - let destTy = destNullableTy g calledArgTy match calledArg.OptArgInfo with - // Use the type variable from the Nullable if called arg is not optional. - | NotOptional when isTyparTy g destTy -> - destTy + // If inference has not solved the kind of Nullable for the called arg then use this. + | NotOptional when isTyparTy g (destNullableTy g calledArgTy) -> + calledArgTy | _ -> let compgenId = mkSynId range0 unassignedTyparName mkTyparTy (Construct.NewTypar (TyparKind.Type, TyparRigidity.Flexible, SynTypar(compgenId, TyparStaticReq.None, true), false, TyparDynamicReq.No, [], false, false)) diff --git a/tests/fsharp/Compiler/Language/OptionalInteropTests.fs b/tests/fsharp/Compiler/Language/OptionalInteropTests.fs index cffba65c156..19d32334850 100644 --- a/tests/fsharp/Compiler/Language/OptionalInteropTests.fs +++ b/tests/fsharp/Compiler/Language/OptionalInteropTests.fs @@ -6,6 +6,7 @@ open System.Collections.Immutable open NUnit.Framework open FSharp.Test.Utilities open FSharp.Test.Utilities.Utilities +open FSharp.Test.Utilities.Compiler open FSharp.Compiler.Diagnostics open Microsoft.CodeAnalysis diff --git a/tests/fsharp/Compiler/Regressions/NullableOptionalRegressionTests.fs b/tests/fsharp/Compiler/Regressions/NullableOptionalRegressionTests.fs index 93970223ce5..26391ffa1c3 100644 --- a/tests/fsharp/Compiler/Regressions/NullableOptionalRegressionTests.fs +++ b/tests/fsharp/Compiler/Regressions/NullableOptionalRegressionTests.fs @@ -33,3 +33,66 @@ let test () = |> typecheck |> shouldSucceed |> ignore + + [] + let ``Method should infer 'z' correctly``() = + let fsSrc = + """ +namespace FSharpTest + +open System + +type Test() = class end + +type Test with + + static member nullableE (encoder, x: Nullable<'a>) = if x.HasValue then encoder x.Value else Test() + static member nullable codec z = Test.nullableE(codec, z) + """ + FSharp fsSrc + |> withLangVersionPreview + |> typecheck + |> shouldSucceed + |> ignore + + [] + let ``Method should infer correctly``() = + let fsSrc = + """ +namespace FSharpTest + +open System + +type Test() = class end + +type Test with + + static member nullableE encoder (x: Nullable<'a>) = if x.HasValue then encoder x.Value else Test() + static member nullable codec = Test.nullableE codec + """ + FSharp fsSrc + |> withLangVersionPreview + |> typecheck + |> shouldSucceed + |> ignore + + [] + let ``Method should infer correctly 2``() = + let fsSrc = + """ +namespace FSharpTest + +open System + +type Test() = class end + +type Test with + + static member nullableE encoder (x: Nullable) = if x.HasValue then encoder x.Value else Test() + static member nullable codec = Test.nullableE codec + """ + FSharp fsSrc + |> withLangVersionPreview + |> typecheck + |> shouldSucceed + |> ignore From a031b05c0c7be8bd18d641e6bf7fe571fa5ca332 Mon Sep 17 00:00:00 2001 From: Will Smith Date: Thu, 1 Apr 2021 17:35:12 -0700 Subject: [PATCH 2/2] comment update --- src/fsharp/MethodCalls.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fsharp/MethodCalls.fs b/src/fsharp/MethodCalls.fs index 6330a18593b..94fbd7ff60f 100644 --- a/src/fsharp/MethodCalls.fs +++ b/src/fsharp/MethodCalls.fs @@ -215,7 +215,7 @@ let AdjustCalledArgTypeForOptionals (g: TcGlobals) enforceNullableOptionalsKnown // If at the beginning of inference then use a type variable. else match calledArg.OptArgInfo with - // If inference has not solved the kind of Nullable for the called arg then use this. + // If inference has not solved the kind of Nullable on the called arg and is not optional then use this. | NotOptional when isTyparTy g (destNullableTy g calledArgTy) -> calledArgTy | _ ->