New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dzmitry-lahoda opened this Issue Sep 7, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@dzmitry-lahoda

dzmitry-lahoda commented Sep 7, 2018

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

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 Microsoft.FSharp.Quotations.PatternsModule.u_Expr@1432-7.Invoke(BindingEnv env)
   at Microsoft.FSharp.Primitives.Basics.List.map[T,TResult](FSharpFunc`2 mapping, FSharpList`1 x)
   at Microsoft.FSharp.Quotations.PatternsModule.u_Expr@1415-4.Invoke(BindingEnv env)
   at Microsoft.FSharp.Primitives.Basics.List.map[T,TResult](FSharpFunc`2 mapping, FSharpList`1 x)
   at Microsoft.FSharp.Quotations.PatternsModule.u_Expr@1415-4.Invoke(BindingEnv env)
   at Microsoft.FSharp.Quotations.PatternsModule.u_Expr@1437-9.Invoke(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:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • 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:

  • 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
@cartermp

This comment has been minimized.

Show comment
Hide comment
@cartermp

cartermp Sep 10, 2018

Member

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

Member

cartermp commented Sep 10, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment