From 3c368a9022cfce45837e1f77dd78002082bd0a32 Mon Sep 17 00:00:00 2001 From: Tomas Grosup Date: Thu, 25 Jan 2024 14:45:12 +0100 Subject: [PATCH] Fix ILType.Array import --- src/Compiler/Checking/import.fs | 4 +- .../FSharpChecker/TransparentCompiler.fs | 6 +-- .../Language/NullableCsharpImportTests.fs | 53 ++++++++++++++++--- 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/Compiler/Checking/import.fs b/src/Compiler/Checking/import.fs index f629df6c5e9..cf85e3c2e56 100644 --- a/src/Compiler/Checking/import.fs +++ b/src/Compiler/Checking/import.fs @@ -316,10 +316,10 @@ let rec ImportILTypeWithNullness (env: ImportMap) m tinst (nf:Nullness.NullableF | ILType.Void -> env.g.unit_ty,nf - | ILType.Array(bounds, ty) -> + | ILType.Array(bounds, innerTy) -> let n = bounds.Rank let (arrayNullness,nf) = Nullness.evaluateFirstOrderNullnessAndAdvance ty nf - let struct(elemTy,nf) = ImportILTypeWithNullness env m tinst nf ty + let struct(elemTy,nf) = ImportILTypeWithNullness env m tinst nf innerTy mkArrayTy env.g n arrayNullness elemTy m, nf | ILType.Boxed tspec | ILType.Value tspec -> diff --git a/tests/FSharp.Compiler.ComponentTests/FSharpChecker/TransparentCompiler.fs b/tests/FSharp.Compiler.ComponentTests/FSharpChecker/TransparentCompiler.fs index 57a9e266fe8..60c7e8be881 100644 --- a/tests/FSharp.Compiler.ComponentTests/FSharpChecker/TransparentCompiler.fs +++ b/tests/FSharp.Compiler.ComponentTests/FSharpChecker/TransparentCompiler.fs @@ -237,8 +237,8 @@ let ``File is not checked twice`` () = |> Seq.map (fun (k, g) -> k, g |> Seq.map fst |> Seq.toList) |> Map - Assert.Equal([Weakened; Started; Finished], intermediateTypeChecks["FileFirst.fs"]) - Assert.Equal([Weakened; Started; Finished], intermediateTypeChecks["FileThird.fs"]) + Assert.Equal([Weakened; Requested; Started; Finished], intermediateTypeChecks["FileFirst.fs"]) + Assert.Equal([Weakened; Requested; Started; Finished], intermediateTypeChecks["FileThird.fs"]) [] let ``If a file is checked as a dependency it's not re-checked later`` () = @@ -261,7 +261,7 @@ let ``If a file is checked as a dependency it's not re-checked later`` () = |> Seq.map (fun (k, g) -> k, g |> Seq.map fst |> Seq.toList) |> Map - Assert.Equal([Weakened; Started; Finished], intermediateTypeChecks["FileThird.fs"]) + Assert.Equal([Weakened; Requested; Started; Finished; Requested], intermediateTypeChecks["FileThird.fs"]) // [] TODO: differentiate complete and minimal checking requests diff --git a/tests/FSharp.Compiler.ComponentTests/Language/NullableCsharpImportTests.fs b/tests/FSharp.Compiler.ComponentTests/Language/NullableCsharpImportTests.fs index 60930ad56dd..f3c9577530a 100644 --- a/tests/FSharp.Compiler.ComponentTests/Language/NullableCsharpImportTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/Language/NullableCsharpImportTests.fs @@ -1,9 +1,54 @@ module Language.NullableCSharpImport -open FSharp.Test open Xunit +open FSharp.Test open FSharp.Test.Compiler +let typeCheckWithStrictNullness cu = + cu + |> withLangVersionPreview + |> withCheckNulls + |> withWarnOn 3261 + |> withOptions ["--warnaserror+"] + |> compile + +[] +let ``Passing null to IlGenerator BeginCatchBlock is fine`` () = + FSharp """module MyLibrary +open System.Reflection.Emit +open System + +let passValueToIt (ilg: ILGenerator) = + let maybeType : Type | null = null + ilg.BeginCatchBlock(maybeType)""" + |> asLibrary + |> typeCheckWithStrictNullness + |> shouldSucceed + +[] +let ``Consumption of netstandard2 BCL api which is not annotated`` () = + FSharp """module MyLibrary +open System.Reflection + +[] +type PublicKey = + | PublicKey of byte[] + | PublicKeyToken of byte[] + +let FromAssemblyName (aname: AssemblyName) = + match aname.GetPublicKey() with + | Null + | NonNull [||] -> + match aname.GetPublicKeyToken() with + | Null + | NonNull [||] -> None + | NonNull bytes -> Some(PublicKeyToken bytes) + | NonNull bytes -> Some(PublicKey bytes)""" + |> asLibrary + |> typeCheckWithStrictNullness + |> shouldSucceed + + [] let ``Consumption of nullable C# - no generics, just strings in methods and fields`` () = let csharpLib = @@ -67,12 +112,8 @@ let ``Consumption of nullable C# - no generics, just strings in methods and fiel """ |> asLibrary - |> withLangVersionPreview |> withReferences [csharpLib] - |> withCheckNulls - |> withWarnOn 3261 - |> withOptions ["--warnaserror+"] - |> compile + |> typeCheckWithStrictNullness |> shouldFail |> withDiagnostics [ Error 3261, Line 5, Col 40, Line 5, Col 85, "Nullness warning: The types 'string' and 'string | null' do not have compatible nullability."