Summary

Define-constant cannot be used with the FFI

Metadata

Description

I'm not 100% sure this is even a bug or just bad documentation/error messaging, but here goes:

If I compile the following "program", I get an error message:

 #!scm
 (define-constant one (foreign-value "1" int))

The error says

 Syntax error: cannot evaluate compiler-special-form
 
         (##core#define-foreign-variable code_0 int "1")

I can see why; in compiler.scm, line 1111 under ##core#define-constant it runs eval on the constant that's defined (unless it's a collapsible literal) but of course eval doesn't have access to compiler syntax, so it dies.

The question is - should this be an error or not? It might make sense to just put the foreign value as-is in each usage point.

PS: there's a small spelling error in there, the predicate collapsable-literal? should be collapsible-literal? with an "i" instead of an "a".

Changes and comments

[2011-05-10 19:55:21 UTC] sjamaan wrote:

Oddly enough, the following does not give an error. When I run it, it cleanly prints the number 1 on stdout. (well, not that odd, as it's a collapsible literal symbol "x", I guess...)

What I really don't understand is why it doesn't say "error in constant evaluation of ~S for named constant ~S" when eval fails. Is the syntax-error-hook bound to some odd error handler which quits the compiler immediately? If so, any error in eval would cause this to happen, I think?

 #!scm
 (define x (foreign-value "1" int))
 (define-constant one x)
 (print one)

[2011-05-15 14:32:46 UTC] felix changed priority from not urgent at all to minor

[2011-05-15 14:32:46 UTC] felix set milestone to 4.8.0

[2011-05-15 14:32:46 UTC] felix wrote:

The constant-value must be evaluatable at compile-time. I will document this. The example you give later actually is a bug - "x" is treated as a symbol, as you correctly point out. Yet it expands into a variable reference. I have to investigate this more.

[2011-05-17 14:05:35 UTC] felix changed status from new to closed

[2011-05-17 14:05:35 UTC] felix set resolution to fixed

[2011-05-17 14:05:35 UTC] felix wrote:

The example you give in your later comment will now trigger an error. Yes, any error in the evaluation of the constant expression will abort the compilation.

[2012-09-24 21:47:48 UTC] felix changed milestone from 4.8.0 to 4.9.0

[2012-09-24 21:47:48 UTC] felix wrote:

Milestone 4.8.0 deleted