fslang-suggestions icon indicating copy to clipboard operation
fslang-suggestions copied to clipboard

Support UnmanagedCallersOnlyAttribute

Open NatElkins opened this issue 9 months ago • 3 comments

I propose we support UnmanagedCallersOnlyAttribute without a warning.

The existing way of approaching this problem in F# is to use the class with a warning.

Pros and Cons

The advantages of making this adjustment to F# are that we get to support more native interop scenarios without warnings, and also help to promote F# as a first-class citizen of the .NET runtime.

The disadvantages of making this adjustment to F# are slight additional complexity.

Extra information

Estimated cost (XS, S, M, L, XL, XXL): S

Related suggestions: N/A

Related issues: https://github.com/dotnet/fsharp/issues/18445 https://github.com/dotnet/samples/issues/5647

Affidavit (please submit!)

Please tick these items by placing a cross in the box:

  • [x] This is not a question (e.g. like one you might ask on StackOverflow) and I have searched StackOverflow for discussions of this issue
  • [x] This is a language change and not purely a tooling change (e.g. compiler bug, editor support, warning/error messages, new warning, non-breaking optimisation) belonging to the compiler and tooling repository
  • [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it
  • [x] I have searched both open and closed suggestions on this site and believe this is not a duplicate

Please tick all that apply:

  • [x] This is not a breaking change to the F# language design
  • [x] I or my company would be willing to help implement and/or test this

For Readers

If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.

NatElkins avatar Apr 05 '25 22:04 NatElkins

The specific requirements to support this attribute can be found in the Remarks section: https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.unmanagedcallersonlyattribute?view=net-9.0

NatElkins avatar Apr 05 '25 22:04 NatElkins

I will copy the remarks here inline for others to see, since this is the main work to be done with this suggestion. This should affect both compilation of authored code that uses [<UnmanagedCallersOnlyAttribute>], but also checking such method is not used from referenced code ("Must not be called from managed code.").

On top of the C#-minded requirements below, this suggestion should also specify all the allowed placements (e.g. module functions on top of static members in types) and additional F#-specific requirements (not allowing curried arguments comes to mind as an additional enforcement).

Marking as approved in principle. It would be good to elaborate on the exact approach in a small RFC.

Methods marked with this attribute have the following restrictions:

  • Must be marked static.
  • Must not be called from managed code.
  • Must only have blittable arguments.
  • Must not have generic type parameters or be contained within a generic class.

T-Gro avatar Apr 08 '25 11:04 T-Gro

Adding this feature to F# would not be very valuable without also adding function pointers (#934).

teo-tsirpanis avatar May 06 '25 22:05 teo-tsirpanis