cisco / ChezScheme

Chez Scheme
Apache License 2.0
6.99k stars 987 forks source link

Mysterious invalid syntax when building transformer output with `quote-syntax` #861

Closed dpk closed 1 month ago

dpk commented 3 months ago
(library (quote-syntax-bug)
  (export my-if yes-or-no)
  (import (chezscheme))

  (define-syntax my-if
    (lambda (stx)
      (syntax-case stx ()
        ((_ test then else)
         (list (quote-syntax if) #'test #'then #'else)))))

  (define-syntax yes-or-no
    (lambda (stx)
      (syntax-case stx ()
        ((_ what)
         (my-if (syntax->datum #'what) #''yes #''no))))))
> (import (quote-syntax-bug))
Exception: invalid syntax if at line 9, char 30 of quote-syntax-bug.sls
Type (debug) to enter the debugger.

Changing quote-syntax to syntax in the definition of my-if fixes it. Commenting out yes-or-no and using my-if at the REPL fails. But pasting the definition of my-if into the REPL and invoking it directly from the REPL works.

mnieper commented 3 months ago

I am investigating this issue. The root cause of the problem seems to be the behaviour of the tests in this program:

(import (chezscheme))
(define qif (quote-syntax if))
(assert (symbol? (syntax->datum qif)))
(assert (identifier? qif))

The first test succeeds, the second test fails.

mnieper commented 3 months ago

Okay, this was an easy-to-find mistake. The issue is fixed with the following patch:

diff --git a/s/syntax.ss b/s/syntax.ss
index 197d1b10..9e845119 100644
--- a/s/syntax.ss
+++ b/s/syntax.ss
@@ -6027,10 +6027,11 @@
          (_ (syntax-error (source-wrap e w ae))))))

 (global-extend 'core 'quote-syntax
-   (lambda (e r w ae)
+  (lambda (e r w ae)
+    (let ([e (source-wrap e w ae)])
       (syntax-case e ()
-         ((_ e) (build-data no-source (source-wrap (syntax e) w ae)))
-         (_ (syntax-error (source-wrap e w ae))))))
+         ((_ e) (build-data no-source (syntax e)))
+         (_ (syntax-error e))))))

 (global-extend 'core 'syntax
   (let ()

I am going to submit a formal pull request.

LiberalArtist commented 1 month ago

This has been completed in https://github.com/cisco/ChezScheme/commit/613e127ba4ad5f6a0d03043c6f6688b5bf0d6145 and can be closed.