public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
From: alexvong.90frf@simplelogin.com
To: kawa@sourceware.org
Subject: `begin-for-syntax` and `define-for-syntax` violate hygiene when used inside macro templates
Date: Mon, 08 May 2023 15:28:33 +0000	[thread overview]
Message-ID: <168355972739.7.1921650802292105194.127745684@simplelogin.com> (raw)


[-- 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 --]

                 reply	other threads:[~2023-05-08 15:28 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=168355972739.7.1921650802292105194.127745684@simplelogin.com \
    --to=alexvong.90frf@simplelogin.com \
    --cc=kawa@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).