Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#10460 closed feature request (fixed)

Allow foreign prim to return Any

Reported by: ezyang Owned by: ezyang
Priority: normal Milestone: 8.0.1
Component: Compiler (Type checker) Version: 7.11
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: GHC rejects valid program Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description (last modified by ezyang)

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 <mail@joachim-breitner.de>  Mon Mar 12 01:20:12 2012
Committer:	Simon Marlow <marlowsd@gmail.com>  Wed Mar 14 06:01:18 2012

Allow Any as an argument type to foreign prim functions

I can volunteer to write the patch but it would be good if someone else OKs this before I proceed.

Change History (9)

comment:1 Changed 2 years ago by ezyang

Description: modified (diff)
Owner: set to ezyang

comment:2 Changed 2 years ago by simonpj

Generally ok but

  • The ability to pass Any to foreign import prim is not documented in 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

comment:3 Changed 2 years ago by Edward Z. Yang <ezyang@…>

In 98b0b2e41f2bdc66bf815ff5f3825832b2b6d34d/ghc:

Add information about allowed foreign prim args, see #10460.

Signed-off-by: Edward Z. Yang <ezyang@cs.stanford.edu>

comment:4 Changed 2 years ago 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

comment:5 Changed 2 years ago by ezyang

And you are only proposing to add Any as a possible result for foreign 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.

comment:6 Changed 2 years ago by Edward Z. Yang <ezyang@…>

In e5be846ba929da54472c03a7c3b05fdd1e483c01/ghc:

Typofix: missing period. (#10460)

Signed-off-by: Edward Z. Yang <ezyang@cs.stanford.edu>

comment:7 Changed 2 years ago by Edward Z. Yang <ezyang@…>

In cd9c5c6678e206ffcda955f66c26c7a4d89519c9/ghc:

Allow Any return in foreign prim, fixes #10460.

Signed-off-by: Edward Z. Yang <ezyang@cs.stanford.edu>

Test Plan: validate

Reviewers: simonpj, goldfire, austin

Subscribers: bgamari, thomie

Differential Revision: https://phabricator.haskell.org/D935

GHC Trac Issues: #10460

comment:8 Changed 2 years ago by ezyang

Resolution: fixed
Status: newclosed

comment:9 Changed 2 years ago by thoughtpolice

Milestone: 7.12.18.0.1

Milestone renamed

Note: See TracTickets for help on using tickets.