~ chicken-core (chicken-5) 9dcd35b7862054a96321767eabe85c5215b2bcb6


commit 9dcd35b7862054a96321767eabe85c5215b2bcb6
Author:     Peter Bex <peter@more-magic.net>
AuthorDate: Tue May 7 21:00:34 2019 +0200
Commit:     Evan Hanson <evhan@foldling.org>
CommitDate: Sun May 12 11:36:15 2019 +1200

    Fix how define-foreign-type defines its conversion procedures
    
    It used to call "define", but this may not be available in the current
    macro environment (or refer to something else than Scheme "define"),
    so it was changed in de342aacd5bd18a6533e4eb4e04c2955e0f02959 to use
    the ##core#set compiler form instead.  This would cause problems when
    define-foreign-type was used inside a module, because later references
    to the type would trigger an "undefined export" error, which would
    cause the module to be unresolvable.
    
    These procedures are gensymed, so they should not actually be
    registered for export, but this restores the old behaviour based on
    how "define" works at toplevel.  The compiler form already checks that
    it is used at toplevel, so we may safely make the assumption that this
    won't expand to an internal define in a lambda.
    
    Signed-off-by: Evan Hanson <evhan@foldling.org>

diff --git a/core.scm b/core.scm
index c4aa81bc..43dc604d 100644
--- a/core.scm
+++ b/core.scm
@@ -1241,6 +1241,11 @@
 				    (mark-variable ret '##compiler#always-bound)
 				    (hide-variable arg)
 				    (hide-variable ret)
+				    ;; NOTE: Above we already check we're in toplevel context,
+				    ;; so we can unconditionally register the export here.
+				    ;; TODO: Remove after fixing #1615
+				    (##sys#register-export arg (##sys#current-module))
+				    (##sys#register-export ret (##sys#current-module))
 				    (walk
 				     `(##core#begin
 					(##core#set! ,arg ,(first conv))
diff --git a/tests/compiler-tests.scm b/tests/compiler-tests.scm
index 6e5c8b27..d4585e37 100644
--- a/tests/compiler-tests.scm
+++ b/tests/compiler-tests.scm
@@ -69,6 +69,19 @@
 (import x)
 (assert (= 1 ((bar 42))))
 
+
+;; Test custom foreign type conversions
+
+(module y (my-add1)
+  (import scheme (chicken base) (chicken foreign))
+
+  (define-foreign-type my-int integer add1 sub1)
+
+  (define my-add1 (foreign-lambda* my-int ((my-int x)) "C_return(x+1);")))
+
+(import y)
+(assert (= 2 (my-add1 1)))
+
 ;;; rev. 14574 (reported by Peter Bex)
 ;
 ; - type specifiers in foreign-lambda in macros are incorrectly renamed
Trap