Subject: bug#49305: Regression regarding #nil in syntax expansions in 3.0.7

From: Rob Browning <[email-masked]>
Date: Wed, 30 Jun 2021 18:48:02 -0500

In 3.0.7, this:

  (define-syntax foo
    (syntax-rules ()
      ((_ x) (eq? #nil x))))

  (foo #t)

began crashing like this:

  ice-9/psyntax.scm:2795:12: In procedure syntax-violation:
  Syntax error:
  unknown location: unexpected syntax in form ()

git bisect suggested the source of the trouble might be this change:

  commit 0cc799185576712d69f11fc794454f2f5447bef7
  Date:   Thu Feb 25 09:33:15 2021 +0100

    Ensure that (syntax ()) results in ()

    * module/ice-9/psyntax.scm: Add a special case for ().  There are
    already special cases for pairs, vectors, etc; the issue is that with
    read-syntax, the () might be come into psyntax as an annotated syntax
    object, which here we would want to strip, to preserve the invariant to
    psyntax users that all lists are unwrapped.

And this change to the same code appears to fix the problem, while still
passing all the existing tests (the assumption being that we might need
to special case #nil too):

  diff --git a/module/ice-9/psyntax.scm b/module/ice-9/psyntax.scm
  index 663d9275a..bd4bd6723 100644
  --- a/module/ice-9/psyntax.scm
  +++ b/module/ice-9/psyntax.scm
  @@ -2157,6 +2157,7 @@
                         (lambda ()
                           (gen-syntax src #'(e1 e2 ...) r maps ellipsis? mod))
                       (lambda (e maps) (values (gen-vector e) maps))))
  +                 (x (eq? (syntax->datum #'x) #nil) (values '(quote #nil) maps))
                    (() (values '(quote ()) maps))
                    (_ (values `(quote ,e) maps))))))

  diff --git a/test-suite/tests/syntax.test b/test-suite/tests/syntax.test
  index a2999ac43..510e7104d 100644
  --- a/test-suite/tests/syntax.test
  +++ b/test-suite/tests/syntax.test
  @@ -1684,6 +1684,16 @@
           (hash interpreted most-positive-fixnum)
         (hash compiled most-positive-fixnum))))

  +(with-test-prefix "#nil in syntaxes"
  +  (pass-if-equal "does not crash"
  +      42
  +    (let ()
  +      (define-syntax foo
  +        (syntax-rules ()
  +          ;; In 3.0.7 this would crash with
  +          ;;   unknown location: unexpected syntax in form ()
  +          ((_ x) (when (eq? x #nil) 42))))
  +      (foo #nil))))

   ;;; Local Variables:
   ;;; eval: (put 'pass-if-syntax-error 'scheme-indent-function 1)

So I wondered whether this might be a plausible fix.

(I originally noticed because it breaks lokke, which like the elisp
 dialect, depends more heavily on #nil.)

Thanks
-- 
Rob Browning
rlb @defaultvalue.org and @debian.org
GPG as of 2011-07-10 E6A9 DA3C C9FD 1FF8 C676 D2C4 C0F0 39E9 ED1B 597A
GPG as of 2002-11-03 14DD 432F AE39 534D B592 F9A0 25C8 D377 8C7E 73A4