public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* `begin-for-syntax` and `define-for-syntax` violate hygiene when used inside macro templates
@ 2023-05-08 15:28 alexvong.90frf
  0 siblings, 0 replies; only message in thread
From: alexvong.90frf @ 2023-05-08 15:28 UTC (permalink / raw)
  To: kawa


[-- Attachment #1.1: Type: text/plain, Size: 1642 bytes --]

Hi!

I think the current implementation of `begin-for-syntax` and `define-for-syntax` violate hygiene when used inside macro templates.

For examples, if we run:

```
(begin-for-syntax
  (define (foo) 42))

(begin-for-syntax
  (display (foo))
  (newline))
```

Both kawa and racket display 42 correctly. But if we modify the program to place `begin-for-syntax` inside a macro templates:

```
(define-syntax install-foo!
  (syntax-rules ()
    ((_)
     (begin-for-syntax
      (define (foo) 42)))))

(install-foo!)

(begin-for-syntax
 (display (foo))
 (newline))
```

Racket signals the following error:

```
foo: undefined;
 cannot reference an identifier before its definition
  in module: top-level
  context...:
   top-level: [running body]
```

but kawa displays 42, violating hygiene.

Similar results are obtained if we replace `begin-for-syntax` by `define-for-syntax` or use guile instead of racket. In the case of guile, we need to use `(eval-when (expand load eval) ...)` rather than `begin-for-syntax`.

Currently, we can workaround this issue by using `generate-temporaries` and `with-syntax`:

```
(define-syntax install-foo!
  (lambda (stx)
    (syntax-case stx ()
      ((_)
       (with-syntax (((foo) (generate-temporaries #'(foo))))
         #'(begin-for-syntax
            (define (foo) 42)))))))
```

We can even generate an uninterned symbol and then convert it to an syntax object for extra safety if we wish to do so.



--
Alex

My PGP public key: https://salsa.debian.org/alexvong243f/pgp-key/-/raw/master/A5FCA28E0FF8E4C57F75555CDD66FE5CB79F5A5E.asc

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 855 bytes --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-05-08 15:28 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-08 15:28 `begin-for-syntax` and `define-for-syntax` violate hygiene when used inside macro templates alexvong.90frf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).