Description
When running the following test in a visual studio project which is unoptimized the test raises the expected DiscardException.
[<Xunit.Fact>]
let testUnoptimized () =
FsCheck.Prop.discard ()
trueWhen running the exact same test in a project which is optimized, the following unexpected exception is raised:
Message:
System.MethodAccessException : Attempt by method 'test.testOptimized()' to access method 'FsCheck.Testable+DiscardException..ctor()' failed.
Stack Trace:
test.testOptimized() line 6
This is not what happens when I run the FsCheck.Test project tests. There, Prop.discard () works as expected and as far as I can tell this is also with optimization on.
The above example, in both a project which has optimization enabled and in one with optimization disabled, is available here:
https://github.com/OkkeHendriks/fscheck_discard_issue
Does someone see my stupid mistake or is more going on?
Version info:
I tried to use the same .NET framework version (4.5.2) and other assemblies/packages as in the FsCheck.Test project on master.
$ dotnet --info
.NET SDK (reflecting any global.json):
Version: 5.0.102
Commit: 71365b4d42
Runtime Environment:
OS Name: Windows
OS Version: 10.0.17763
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.102\
Host (useful for support):
Version: 5.0.2
Commit: cb5f173b96
.NET SDKs installed:
2.1.811 [C:\Program Files\dotnet\sdk]
3.1.101 [C:\Program Files\dotnet\sdk]
5.0.102 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
To install additional .NET runtimes or SDKs:
https://aka.ms/dotnet-download
Microsoft (R) F# Interactive version 11.0.0.0 for F# 5.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

Activity
kurtschelfthout commentedon Feb 17, 2021
My guess is that this is an F# compiler bug, somewhat similar to dotnet/fsharp#7422 . It looks to me like fsc's inlining optimization is too aggressive. I bet that in your optimized build it inlines the
discard()method in your test assembly, which then can't execute because theDiscardExceptionis internal in FsCheck.dll.It works in FsCheck.Test because FsCheck has an
InternalsVisibileToAttributefor it...so we can test some internals ;).You can probably confirm by disassembling the test assembly and visually checking my inlining guess. Adding
[<MethodImpl(MethodImplOptions.NoInlining)>]toProp.discardshould also fix it, and I'd consider that a reasonable workaround.If that all checks out and you fancy it, worth creating a small repro and submitting an issue to the fine folks over at https://github.com/dotnet/fsharp too...
OkkeHendriks commentedon Feb 18, 2021
First of all, thanks for the quick reply.
Seems like it is indeed inlined in the optimized build:
And not in the unoptimized build:
I tried with a build of FsCheck with the following, and this indeed works as intended, I created a PR for this, see #550.
I will have a look later at creating a simple example repo for the underlying issue and add it to the discussion at dotnet/fsharp#7422.
kurtschelfthout commentedon Feb 18, 2021
Released in https://www.nuget.org/packages/FsCheck/2.14.5