|
1 | 1 | module Language.NullableCSharpImport |
2 | 2 |
|
3 | | -open FSharp.Test |
4 | 3 | open Xunit |
| 4 | +open FSharp.Test |
5 | 5 | open FSharp.Test.Compiler |
6 | 6 |
|
| 7 | +let typeCheckWithStrictNullness cu = |
| 8 | + cu |
| 9 | + |> withLangVersionPreview |
| 10 | + |> withCheckNulls |
| 11 | + |> withWarnOn 3261 |
| 12 | + |> withOptions ["--warnaserror+"] |
| 13 | + |> compile |
| 14 | + |
| 15 | +[<Fact>] |
| 16 | +let ``Passing null to IlGenerator BeginCatchBlock is fine`` () = |
| 17 | + FSharp """module MyLibrary |
| 18 | +open System.Reflection.Emit |
| 19 | +open System |
| 20 | +
|
| 21 | +let passValueToIt (ilg: ILGenerator) = |
| 22 | + let maybeType : Type | null = null |
| 23 | + ilg.BeginCatchBlock(maybeType)""" |
| 24 | + |> asLibrary |
| 25 | + |> typeCheckWithStrictNullness |
| 26 | + |> shouldSucceed |
| 27 | + |
| 28 | +[<Fact>] |
| 29 | +let ``Consumption of netstandard2 BCL api which is not annotated`` () = |
| 30 | + FSharp """module MyLibrary |
| 31 | +open System.Reflection |
| 32 | +
|
| 33 | +[<StructuralEquality; StructuralComparison>] |
| 34 | +type PublicKey = |
| 35 | + | PublicKey of byte[] |
| 36 | + | PublicKeyToken of byte[] |
| 37 | +
|
| 38 | +let FromAssemblyName (aname: AssemblyName) = |
| 39 | + match aname.GetPublicKey() with |
| 40 | + | Null |
| 41 | + | NonNull [||] -> |
| 42 | + match aname.GetPublicKeyToken() with |
| 43 | + | Null |
| 44 | + | NonNull [||] -> None |
| 45 | + | NonNull bytes -> Some(PublicKeyToken bytes) |
| 46 | + | NonNull bytes -> Some(PublicKey bytes)""" |
| 47 | + |> asLibrary |
| 48 | + |> typeCheckWithStrictNullness |
| 49 | + |> shouldSucceed |
| 50 | + |
| 51 | + |
7 | 52 | [<FactForNETCOREAPP>] |
8 | 53 | let ``Consumption of nullable C# - no generics, just strings in methods and fields`` () = |
9 | 54 | let csharpLib = |
@@ -67,12 +112,8 @@ let ``Consumption of nullable C# - no generics, just strings in methods and fiel |
67 | 112 |
|
68 | 113 | """ |
69 | 114 | |> asLibrary |
70 | | - |> withLangVersionPreview |
71 | 115 | |> withReferences [csharpLib] |
72 | | - |> withCheckNulls |
73 | | - |> withWarnOn 3261 |
74 | | - |> withOptions ["--warnaserror+"] |
75 | | - |> compile |
| 116 | + |> typeCheckWithStrictNullness |
76 | 117 | |> shouldFail |
77 | 118 | |> withDiagnostics [ |
78 | 119 | Error 3261, Line 5, Col 40, Line 5, Col 85, "Nullness warning: The types 'string' and 'string | null' do not have compatible nullability." |
|
0 commit comments