Skip to content
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
7 changes: 3 additions & 4 deletions src/fsharp/MethodCalls.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 on the called arg and is not optional 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))
Expand Down
1 change: 1 addition & 0 deletions tests/fsharp/Compiler/Language/OptionalInteropTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,66 @@ let test () =
|> typecheck
|> shouldSucceed
|> ignore

[<Test>]
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

[<Test>]
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

[<Test>]
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<int32>) = if x.HasValue then encoder x.Value else Test()
static member nullable codec = Test.nullableE codec
"""
FSharp fsSrc
|> withLangVersionPreview
|> typecheck
|> shouldSucceed
|> ignore