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

Code Quotations to fail during compilation or to support unmanaged pointers, not to fail during runtime

Open dzmitry-lahoda opened this issue 7 years ago • 2 comments

I propose that next code to fail compilation or not fail in runtime:

#nowarn "9"
open System
open Microsoft.FSharp.NativeInterop
open System.Native
open System.Reflection
open System.Runtime.InteropServices

[<EntryPoint>]
let main argv =
    let n = NativePtr.ofNativeInt<int32>(nativeint(1))
    let x = <@ NativePtr.get n 0 = 123  @>
    System.Console.WriteLine(x)
    0

The existing way of approaching this problem in F# is throw exception during runtime.

PS E:\dzmitry\src\uncorefx\src\uncorefx.tests> dotnet --version
2.1.400
PS E:\dzmitry\src\uncorefx\src\uncorefx.tests> dotnet run

Unhandled Exception: System.InvalidOperationException: System.IntPtr is not a GenericTypeDefinition. MakeGenericType may only be called on a type for which Type.IsGenericTypeDefinition is true.
   at System.RuntimeType.MakeGenericType(Type[] instantiation)
   at [email protected](BindingEnv env)
   at Microsoft.FSharp.Primitives.Basics.List.map[T,TResult](FSharpFunc`2 mapping, FSharpList`1 x)
   at [email protected](BindingEnv env)
   at Microsoft.FSharp.Primitives.Basics.List.map[T,TResult](FSharpFunc`2 mapping, FSharpList`1 x)
   at [email protected](BindingEnv env)
   at [email protected](BindingEnv env)
   at Microsoft.FSharp.Quotations.PatternsModule.deserialize(Type localAssembly, Type[] referencedTypeDefs, Type[] spliceTypes, FSharpExpr[] spliceExprs, Byte[]
bytes)
   at Program.main(String[] argv) in E:\dzmitry\src\uncorefx\src\uncorefx.tests\Program.fs:line 101

Pros and Cons

The advantages of making this adjustment to F# that it will explicit that quotations do not support pointers at compile time like C# expressions do or quotation to support pointers to allow quation with pointers scenarios.

Affidavit (please submit!)

Please tick this 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] I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • [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.

Please tick all that apply:

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

dzmitry-lahoda avatar Sep 07 '18 17:09 dzmitry-lahoda

I could see this, it's probably unexpected that the <@ NativePtr.get n 0 = 123 @> is calling something invalid under the covers.

cartermp avatar Sep 10 '18 16:09 cartermp

It seems like a plain bug

dsyme avatar Oct 09 '18 18:10 dsyme