From f5543c911bc4eded76814a7762bb5c062f104d93 Mon Sep 17 00:00:00 2001 From: "Kevin Ransom (msft)" Date: Wed, 22 Feb 2023 11:22:39 -0800 Subject: [PATCH] Fix 14745 - Reactivate byref tests (#14766) * Embed IsReadOnlyAttribute * temp * remove asnetstandard20 from cs * Reactivate byref tests * Update Structs02_asNetStandard20.fs.il.release.bsl fix merge issue --- .gitignore | 13 --- .../EmittedIL/Misc/byrefTests.fs} | 82 ++++++++++++++----- .../FSharp.Compiler.ComponentTests.fsproj | 1 + tests/FSharp.Test.Utilities/Compiler.fs | 2 +- tests/fsharp/FSharpSuite.Tests.fsproj | 1 - 5 files changed, 65 insertions(+), 34 deletions(-) rename tests/{fsharp/Compiler/Language/ByrefTests.fs => FSharp.Compiler.ComponentTests/EmittedIL/Misc/byrefTests.fs} (83%) diff --git a/.gitignore b/.gitignore index 875098dae1..78418b2b36 100644 --- a/.gitignore +++ b/.gitignore @@ -125,16 +125,3 @@ nCrunchTemp_* tests/FSharp.Compiler.Service.Tests/FSharp.CompilerService.SurfaceArea.netstandard.actual *.vsp -/Z.fs -/Y.fs -/X.fs -/Library.fs -/Ghost.fs -/D.fsi -/D.fs -/C.fsi -/C.fs -/B.fsi -/B.fs -/A.fsi -/A.fs diff --git a/tests/fsharp/Compiler/Language/ByrefTests.fs b/tests/FSharp.Compiler.ComponentTests/EmittedIL/Misc/byrefTests.fs similarity index 83% rename from tests/fsharp/Compiler/Language/ByrefTests.fs rename to tests/FSharp.Compiler.ComponentTests/EmittedIL/Misc/byrefTests.fs index 548a28addc..d4dd00a7c8 100644 --- a/tests/fsharp/Compiler/Language/ByrefTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/EmittedIL/Misc/byrefTests.fs @@ -1,17 +1,16 @@ // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. -namespace FSharp.Compiler.UnitTests +namespace FSharp.Compiler.ComponentTests.EmittedIL -open NUnit.Framework +open Xunit open FSharp.Compiler.Diagnostics open FSharp.Test open FSharp.Test.Utilities open FSharp.Test.Compiler -[] -module ByrefTests = +module byrefTests = - [] + [] let ``No defensive copy on .NET struct`` () = CompilerAssert.Pass """ @@ -51,7 +50,7 @@ let test6 () = DateTime.Now.Test().Test().Test() """ - [] + [] let ``Extension method scope errors`` () = CompilerAssert.TypeCheckWithErrors """ @@ -119,7 +118,7 @@ let f5 () = // TODO: A better way to test the ones below are to use a custom struct in C# code that contains explicit use of their "readonly" keyword. #if NETCOREAPP // NETCORE makes DateTime a readonly struct; therefore, it should not error. - [] + [] let ``No defensive copy on .NET struct - netcore`` () = CompilerAssert.Pass """ @@ -148,7 +147,7 @@ let test2 () = """ #else // Note: Currently this is assuming NET472. That may change which might break these tests. Consider using custom C# code. - [] + [] let ``Defensive copy on .NET struct for inref`` () = CompilerAssert.TypeCheckWithErrors """ @@ -201,7 +200,7 @@ let test1 () = #endif #if NETCOREAPP - [] + [] let ``Consume CSharp interface with a method that has a readonly byref`` () = let cs = """ @@ -240,7 +239,7 @@ type MyClass() = #endif - [] + [] let ``Can take native address to get a nativeptr of a mutable value`` () = CompilerAssert.Pass """ @@ -252,7 +251,7 @@ let test () = () """ - [] + [] let ``Cannot take native address to get a nativeptr of an immmutable value`` () = CompilerAssert.TypeCheckWithErrors """ @@ -266,8 +265,7 @@ let test () = (FSharpDiagnosticSeverity.Error, 256, (6, 13, 6, 16), "A value must be mutable in order to mutate the contents or take the address of a value type, e.g. 'let mutable x = ...'") |] - [] - [] + [] let ``Returning an 'inref<_>' from a property should emit System.Runtime.CompilerServices.IsReadOnlyAttribute on the return type of the signature`` () = let src = """ @@ -297,8 +295,56 @@ type C() = |> verifyIL [verifyProperty;verifyMethod] |> ignore - [] - [] + [] + let ``Returning an 'inref<_>' from a property should emit System.Runtime.CompilerServices.IsReadOnlyAttribute on the return type of the signature and generate the `` () = + let src = + """ +module Test + +type C() = + let x = 59 + member _.X: inref<_> = &x + """ + + let verifyProperty = """.property instance int32& modreq([netstandard]System.Runtime.InteropServices.InAttribute) + X() + { + .custom instance void System.Runtime.CompilerServices.IsReadOnlyAttribute::.ctor() = ( 01 00 00 00 ) + .get instance int32& modreq([netstandard]System.Runtime.InteropServices.InAttribute) Test/C::get_X() + }""" + + let verifyMethod = """.method public hidebysig specialname + instance int32& modreq([netstandard]System.Runtime.InteropServices.InAttribute) + get_X() cil managed + { + .param [0] + .custom instance void System.Runtime.CompilerServices.IsReadOnlyAttribute::.ctor() = ( 01 00 00 00 )""" + + let verifyIsReadOnlyAttribute = """ +.class private auto ansi beforefieldinit System.Runtime.CompilerServices.IsReadOnlyAttribute + extends [System.Runtime]System.Attribute +{ + .method public specialname rtspecialname + instance void .ctor() cil managed + { + .custom instance void [netstandard]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 ) + .custom instance void [netstandard]System.Diagnostics.DebuggerNonUserCodeAttribute::.ctor() = ( 01 00 00 00 ) + // Code size 7 (0x7) + .maxstack 8 + IL_0000: ldarg.0 + IL_0001: call instance void [System.Runtime]System.Attribute::.ctor() + IL_0006: ret + } // end of method IsReadOnlyAttribute::.ctor + +} // end of class System.Runtime.CompilerServices.IsReadOnlyAttribute""" + + FSharp src + |> asNetStandard20 + |> compile + |> verifyIL [verifyProperty;verifyMethod;verifyIsReadOnlyAttribute] + |> ignore + + [] let ``Returning an 'inref<_>' from a generic method should emit System.Runtime.CompilerServices.IsReadOnlyAttribute on the return type of the signature`` () = let src = """ @@ -320,8 +366,7 @@ type C<'T>() = |> verifyIL [verifyMethod] |> ignore - [] - [] + [] let ``Returning an 'inref<_>' from an abstract generic method should emit System.Runtime.CompilerServices.IsReadOnlyAttribute on the return type of the signature`` () = let src = """ @@ -344,8 +389,7 @@ type C<'T>() = |> verifyIL [verifyMethod] |> ignore - [] - [] + [] let ``Returning an 'inref<_>' from an abstract property should emit System.Runtime.CompilerServices.IsReadOnlyAttribute on the return type of the signature`` () = let src = """ diff --git a/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj b/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj index d6bf558b6c..3871313684 100644 --- a/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj +++ b/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj @@ -127,6 +127,7 @@ + diff --git a/tests/FSharp.Test.Utilities/Compiler.fs b/tests/FSharp.Test.Utilities/Compiler.fs index 19eca28eb2..464ae28ec9 100644 --- a/tests/FSharp.Test.Utilities/Compiler.fs +++ b/tests/FSharp.Test.Utilities/Compiler.fs @@ -506,7 +506,7 @@ module rec Compiler = let asNetStandard20 (cUnit: CompilationUnit) : CompilationUnit = match cUnit with | FS fs -> FS { fs with TargetFramework = TargetFramework.NetStandard20 } - | CS cs -> CS { cs with TargetFramework = TargetFramework.NetStandard20 } + | CS _ -> failwith "References are not supported in CS" | IL _ -> failwith "References are not supported in IL" let asExe (cUnit: CompilationUnit) : CompilationUnit = diff --git a/tests/fsharp/FSharpSuite.Tests.fsproj b/tests/fsharp/FSharpSuite.Tests.fsproj index d9b02982c8..fa40f1cdd8 100644 --- a/tests/fsharp/FSharpSuite.Tests.fsproj +++ b/tests/fsharp/FSharpSuite.Tests.fsproj @@ -61,7 +61,6 @@ -