[GHC] #10460: Allow foreign prim to return Any

#10460: Allow foreign prim to return Any
-------------------------------------+-------------------------------------
Reporter: ezyang | Owner:
Type: feature | Status: new
request | Milestone: 7.12.1
Priority: normal | Version: 7.11
Component: Compiler | Operating System: Unknown/Multiple
(Type checker) | Type of failure: GHC rejects
Keywords: | valid program
Architecture: | Blocked By:
Unknown/Multiple | Related Tickets:
Test Case: |
Blocking: |
Differential Revisions: |
-------------------------------------+-------------------------------------
Presently, we have
{{{
{-# LANGUAGE GHCForeignImportPrim #-}
module Serum where
import GHC.Exts
foreign import prim "chenycopy" cheneycopy :: Any -> Any
}}}
induces the error
{{{
Serum.hs:5:1:
Unacceptable result type in foreign declaration:
‘Any’ cannot be marshalled in a foreign call
When checking declaration:
foreign import prim safe "static chenycopy" cheneycopy
:: Any -> Any
}}}
We ought to allow a lifted value to be returned from a foreign primop; no
reason not to, anyway; no reason not to, and there are plenty of built-in
primops which do this.
For reference, lifted arguments were allowed in arugments in this commit:
{{{
commit e29001c9e0f73885c0b85d86c3a854519448013a
Author: Joachim Breitner

#10460: Allow foreign prim to return Any -------------------------------------+------------------------------------- Reporter: ezyang | Owner: ezyang Type: feature request | Status: new Priority: normal | Milestone: 7.12.1 Component: Compiler (Type | Version: 7.11 checker) | Keywords: Resolution: | Architecture: Operating System: Unknown/Multiple | Unknown/Multiple Type of failure: GHC rejects | Test Case: valid program | Blocking: Blocked By: | Differential Revisions: Related Tickets: | -------------------------------------+------------------------------------- Changes (by ezyang): * owner: => ezyang Old description:
Presently, we have
{{{ {-# LANGUAGE GHCForeignImportPrim #-} module Serum where import GHC.Exts
foreign import prim "chenycopy" cheneycopy :: Any -> Any }}}
induces the error
{{{ Serum.hs:5:1: Unacceptable result type in foreign declaration: ‘Any’ cannot be marshalled in a foreign call When checking declaration: foreign import prim safe "static chenycopy" cheneycopy :: Any -> Any }}}
We ought to allow a lifted value to be returned from a foreign primop; no reason not to, anyway; no reason not to, and there are plenty of built-in primops which do this.
For reference, lifted arguments were allowed in arugments in this commit:
{{{ commit e29001c9e0f73885c0b85d86c3a854519448013a Author: Joachim Breitner
Mon Mar 12 01:20:12 2012 Committer: Simon Marlow Wed Mar 14 06:01:18 2012 Allow Any as an argument type to foreign prim functions }}}
New description:
Presently, we have
{{{
{-# LANGUAGE GHCForeignImportPrim #-}
module Serum where
import GHC.Exts
foreign import prim "chenycopy" cheneycopy :: Any -> Any
}}}
induces the error
{{{
Serum.hs:5:1:
Unacceptable result type in foreign declaration:
‘Any’ cannot be marshalled in a foreign call
When checking declaration:
foreign import prim safe "static chenycopy" cheneycopy
:: Any -> Any
}}}
We ought to allow a lifted value to be returned from a foreign primop; no
reason not to, anyway; no reason not to, and there are plenty of built-in
primops which do this.
For reference, lifted arguments were allowed in arugments in this commit:
{{{
commit e29001c9e0f73885c0b85d86c3a854519448013a
Author: Joachim Breitner

#10460: Allow foreign prim to return Any -------------------------------------+------------------------------------- Reporter: ezyang | Owner: ezyang Type: feature request | Status: new Priority: normal | Milestone: 7.12.1 Component: Compiler (Type | Version: 7.11 checker) | Keywords: Resolution: | Architecture: Operating System: Unknown/Multiple | Unknown/Multiple Type of failure: GHC rejects | Test Case: valid program | Blocking: Blocked By: | Differential Revisions: Related Tickets: | -------------------------------------+------------------------------------- Comment (by simonpj): Generally ok but * The ability to pass `Any` to `foreign import prim` is not documented in [https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ffi.html #ffi-prim 8.1.3 of the user manual], and it jolly well should be. If you want to extend this to arbitrary FFI imports then we should document that too. * Who converts the argument to `Any`? The caller? With `unsafeCoerce`? * You should never combine a '''safe''' foreign import with `Any`. "Safe" means that the call may block and GC may happen, so passing a pointer into the heap is a bad idea. (And this constraint should be documented.) Simon -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10460#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10460: Allow foreign prim to return Any
-------------------------------------+-------------------------------------
Reporter: ezyang | Owner: ezyang
Type: feature request | Status: new
Priority: normal | Milestone: 7.12.1
Component: Compiler (Type | Version: 7.11
checker) | Keywords:
Resolution: | Architecture:
Operating System: Unknown/Multiple | Unknown/Multiple
Type of failure: GHC rejects | Test Case:
valid program | Blocking:
Blocked By: | Differential Revisions:
Related Tickets: |
-------------------------------------+-------------------------------------
Comment (by Edward Z. Yang

#10460: Allow foreign prim to return Any -------------------------------------+------------------------------------- Reporter: ezyang | Owner: ezyang Type: feature request | Status: new Priority: normal | Milestone: 7.12.1 Component: Compiler (Type | Version: 7.11 checker) | Keywords: Resolution: | Architecture: Operating System: Unknown/Multiple | Unknown/Multiple Type of failure: GHC rejects | Test Case: valid program | Blocking: Blocked By: | Differential Revisions: Related Tickets: | -------------------------------------+------------------------------------- Comment (by simonpj): Sorry I think I was mixed up. A `foreign prim` is always `unsafe`, isn't it. And you are only proposing to add `Any` as a possible result for `foreign prim` not for other `foreign` declarations, correct? So my third bullet is irrelevant. There's a missing close-paren in the (welcome) doc fix. Simon -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10460#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

And you are only proposing to add Any as a possible result for foreign
#10460: Allow foreign prim to return Any -------------------------------------+------------------------------------- Reporter: ezyang | Owner: ezyang Type: feature request | Status: new Priority: normal | Milestone: 7.12.1 Component: Compiler (Type | Version: 7.11 checker) | Keywords: Resolution: | Architecture: Operating System: Unknown/Multiple | Unknown/Multiple Type of failure: GHC rejects | Test Case: valid program | Blocking: Blocked By: | Differential Revisions: Related Tickets: | -------------------------------------+------------------------------------- Comment (by ezyang): prim not for other foreign declarations, correct? Yep.
Who converts the argument to Any? The caller? With unsafeCoerce?
Yes and yes. Another possibility is to accept arbitrary types for foreign prim ops (in the same way that GHC's primop mechanism works), but that's more work. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10460#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10460: Allow foreign prim to return Any
-------------------------------------+-------------------------------------
Reporter: ezyang | Owner: ezyang
Type: feature request | Status: new
Priority: normal | Milestone: 7.12.1
Component: Compiler (Type | Version: 7.11
checker) | Keywords:
Resolution: | Architecture:
Operating System: Unknown/Multiple | Unknown/Multiple
Type of failure: GHC rejects | Test Case:
valid program | Blocking:
Blocked By: | Differential Revisions:
Related Tickets: |
-------------------------------------+-------------------------------------
Comment (by Edward Z. Yang

#10460: Allow foreign prim to return Any
-------------------------------------+-------------------------------------
Reporter: ezyang | Owner: ezyang
Type: feature request | Status: new
Priority: normal | Milestone: 7.12.1
Component: Compiler (Type | Version: 7.11
checker) | Keywords:
Resolution: | Architecture:
Operating System: Unknown/Multiple | Unknown/Multiple
Type of failure: GHC rejects | Test Case:
valid program | Blocking:
Blocked By: | Differential Revisions:
Related Tickets: |
-------------------------------------+-------------------------------------
Comment (by Edward Z. Yang

#10460: Allow foreign prim to return Any -------------------------------------+------------------------------------- Reporter: ezyang | Owner: ezyang Type: feature request | Status: closed Priority: normal | Milestone: 7.12.1 Component: Compiler (Type | Version: 7.11 checker) | Keywords: Resolution: fixed | Architecture: Operating System: Unknown/Multiple | Unknown/Multiple Type of failure: GHC rejects | Test Case: valid program | Blocking: Blocked By: | Differential Revisions: Related Tickets: | -------------------------------------+------------------------------------- Changes (by ezyang): * status: new => closed * resolution: => fixed -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10460#comment:8 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC