Skip to content

Commit d9b92bb

Browse files
authored
Fixed destNullableTy internal error (#8655)
* Fixed internal error for destNullableTy by removing rule for optional * Added test
1 parent d59cbdf commit d9b92bb

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

src/fsharp/MethodCalls.fs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ let AdjustCalledArgTypeForOptionals (g: TcGlobals) enforceNullableOptionalsKnown
189189
// CSharpMethod(x = arg), optional C#-style argument, may have type Nullable<ty>.
190190
// The arg should have type ty. However for backwards compat, we also allow arg to have type Nullable<ty>
191191
| CallerSide _ ->
192-
if isNullableTy g calledArgTy && g.langVersion.SupportsFeature LanguageFeature.NullableOptionalInterop then
192+
if isNullableTy g calledArgTy && g.langVersion.SupportsFeature LanguageFeature.NullableOptionalInterop then
193193
// If inference has worked out it's a nullable then use this
194194
if isNullableTy g callerArg.CallerArgumentType then
195195
calledArgTy
@@ -1186,11 +1186,6 @@ let AdjustCallerArgForOptional tcFieldInit eCallerMemberName (infoReader: InfoRe
11861186
let minfo = GetIntrinsicConstructorInfosOfType infoReader m calledArgTy |> List.head
11871187
let callerArgExprCoerced = mkCoerceIfNeeded g calledNonOptTy callerArgTy callerArgExpr
11881188
MakeMethInfoCall amap m minfo [] [callerArgExprCoerced]
1189-
elif isOptionTy g calledArgTy then
1190-
// CSharpMethod(x=b) when 'b' has nullable type and 'x' has optional type --> CSharpMethod(Some b.Value)
1191-
let calledNonOptTy = destOptionTy g calledArgTy
1192-
let callerArgExprCoerced = mkCoerceIfNeeded g calledNonOptTy callerArgTy callerArgExpr
1193-
mkSome g (destNullableTy g callerArgTy) callerArgExprCoerced m
11941189
else
11951190
// CSharpMethod(x=b) --> CSharpMethod(?x=b)
11961191
callerArgExpr
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+
namespace FSharp.Compiler.UnitTests
4+
5+
open System.Collections.Immutable
6+
open NUnit.Framework
7+
open FSharp.Compiler.SourceCodeServices
8+
open FSharp.Compiler.UnitTests.Utilities
9+
open Microsoft.CodeAnalysis
10+
11+
[<TestFixture>]
12+
module OptionalInteropTests =
13+
14+
[<Test>]
15+
let ``C# method with an optional parameter and called with an option type should compile`` () =
16+
let csSrc =
17+
"""
18+
using Microsoft.FSharp.Core;
19+
20+
namespace CSharpTest
21+
{
22+
public static class Test
23+
{
24+
public static void M(FSharpOption<int> x = null) { }
25+
}
26+
}
27+
"""
28+
29+
let fsSrc =
30+
"""
31+
open System
32+
open CSharpTest
33+
34+
Test.M(x = Some 1)
35+
"""
36+
37+
let fsharpCoreAssembly =
38+
typeof<_ list>.Assembly.Location
39+
|> MetadataReference.CreateFromFile
40+
41+
let cs =
42+
CompilationUtil.CreateCSharpCompilation(csSrc, CSharpLanguageVersion.CSharp8, TargetFramework.NetStandard20, additionalReferences = ImmutableArray.CreateRange [fsharpCoreAssembly])
43+
|> CompilationReference.Create
44+
45+
let fs = Compilation.Create(fsSrc, SourceKind.Fsx, CompileOutput.Exe, options = [|"--langversion:preview"|], cmplRefs = [cs])
46+
CompilerAssert.Compile fs

tests/fsharp/FSharpSuite.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<Compile Include="Compiler\Warnings\ExperimentalAttributeTests.fs" />
6161
<Compile Include="Compiler\Warnings\PatternMatchingWarningTests.fs" />
6262
<Compile Include="Compiler\SourceTextTests.fs" />
63+
<Compile Include="Compiler\Language\OptionalInteropTests.fs" />
6364
<Compile Include="Compiler\Language\UIntTests.fs" />
6465
<Compile Include="Compiler\Language\FixedIndexSliceTests.fs" />
6566
<Compile Include="Compiler\Language\SlicingQuotationTests.fs" />

0 commit comments

Comments
 (0)