fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

Release inline optimization leads to MethodAccessException if used with assembly:InternalsVisibleTo attribute

Open Lanayx opened this issue 2 years ago • 0 comments

We've faced an issue that internal function get inlined in the public one and this in turn leads to MethodAccessException on caller side.

Repro steps

Repro zip is attached. just run MethodAccessBug console application in Release mode (not reproduced in Debug)

MethodAccessBug.zip

Expected behavior

Call to internal function is not inlined an no exception is thrown

Actual behavior

Call to internal function is inlined an exception is thrown

Unhandled exception. System.MethodAccessException: Attempt by method '<StartupCode$MethodAccessBug>.$Program.main@()' to access method 'InternalLib.InternalModule.doSmth()' failed.
   at <StartupCode$MethodAccessBug>.$Program.main@() in C:\Temp\MethodAccessBug\MethodAccessBug\Program.fs:line 3

Known workarounds

Decorate public method with

[<MethodImpl(MethodImplOptions.NoInlining)>]

or with

[<NoCompilerInlining>]

Related information

  • Windows 11
  • .NET 7

Lanayx avatar Oct 10 '23 20:10 Lanayx