unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#48315: (ice-9 match) does not suport #nil
@ 2021-05-09 15:57 Maxime Devos
  2021-05-12 20:03 ` Taylan Kammer
  0 siblings, 1 reply; 2+ messages in thread
From: Maxime Devos @ 2021-05-09 15:57 UTC (permalink / raw)
  To: 48315

[-- Attachment #1: Type: text/plain, Size: 264 bytes --]

guile --version:
guile (GNU Guile) 3.0.5

(use-modules (ice-9 match))

(match #nil (_ 'xyzzy))
-->
While compiling expression:
Syntax error:
unknown location: unexpected syntax in form ()

(match '() (_ 'xyzzy))
--> $16 = xyzzy

Greetings,
Maxime.

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 260 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* bug#48315: (ice-9 match) does not suport #nil
  2021-05-09 15:57 bug#48315: (ice-9 match) does not suport #nil Maxime Devos
@ 2021-05-12 20:03 ` Taylan Kammer
  0 siblings, 0 replies; 2+ messages in thread
From: Taylan Kammer @ 2021-05-12 20:03 UTC (permalink / raw)
  To: 48315

[-- Attachment #1: Type: text/plain, Size: 471 bytes --]

This was mind-boggling to debug!

In syntax-rules, the pattern (x ...) will match #nil, since it
matches the empty list.  This can have surprising consequences.

Consider:

  (define-syntax test
    (syntax-rules ()
      ((test (x ...))
       (x ...))
      ((test x)
       x)))

  (test (+ 1 2))  ; => 3
  (test 123)      ; => 123
  (test #f)       ; => #f
  ;; However...
  (test #nil)     ; error


Anyway, attached is a patch to fix the issue in match.


- Taylan

[-- Attachment #2: 0001-Fix-match-when-used-directly-on-the-nil-constant.patch --]
[-- Type: text/plain, Size: 1434 bytes --]

From b6d0b715a8bf0cc39b9fc3d46efeaf010f0d4351 Mon Sep 17 00:00:00 2001
From: Taylan Kammer <taylan.kammer@gmail.com>
Date: Wed, 12 May 2021 21:46:54 +0200
Subject: [PATCH] Fix match when used directly on the #nil constant.

* module/ice-9/match.upstream.scm (match): Make sure we don't match #nil
where we don't intend to.

Fixes <https://bugs.gnu.org/48315>.
---
 module/ice-9/match.upstream.scm | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/module/ice-9/match.upstream.scm b/module/ice-9/match.upstream.scm
index b1fc371b8..3b181b75b 100644
--- a/module/ice-9/match.upstream.scm
+++ b/module/ice-9/match.upstream.scm
@@ -269,9 +269,12 @@
      (match-syntax-error "missing match expression"))
     ((match atom)
      (match-syntax-error "no match clauses"))
-    ((match (app ...) (pat . body) ...)
-     (let ((v (app ...)))
-       (match-next v ((app ...) (set! (app ...))) (pat . body) ...)))
+    ;; The original implementation uses (app ...) not (op arg ...) here,
+    ;; but in Guile this would match #nil when it shouldn't.  Failing to
+    ;; match () doesn't matter since it leads to an error anyway.
+    ((match (op arg ...) (pat . body) ...)
+     (let ((v (op arg ...)))
+       (match-next v ((op arg ...) (set! (op arg ...))) (pat . body) ...)))
     ((match #(vec ...) (pat . body) ...)
      (let ((v #(vec ...)))
        (match-next v (v (set! v)) (pat . body) ...)))
-- 
2.30.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-05-12 20:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-09 15:57 bug#48315: (ice-9 match) does not suport #nil Maxime Devos
2021-05-12 20:03 ` Taylan Kammer

unofficial mirror of bug-guile@gnu.org 

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://yhetil.org/guile-bugs/0 guile-bugs/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 guile-bugs guile-bugs/ https://yhetil.org/guile-bugs \
		bug-guile@gnu.org
	public-inbox-index guile-bugs

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.yhetil.org/yhetil.lisp.guile.bugs
	nntp://news.gmane.io/gmane.lisp.guile.bugs


AGPL code for this site: git clone http://ou63pmih66umazou.onion/public-inbox.git