GNU bug report logs -
#60225
[PATCH] records: match-record supports specifying a different variable name.
Previous Next
Reported by: Attila Lendvai <attila <at> lendvai.name>
Date: Tue, 20 Dec 2022 17:42:02 UTC
Severity: normal
Tags: patch
Done: Ludovic Courtès <ludo <at> gnu.org>
Bug is archived. No further changes may be made.
To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 60225 in the body.
You can then email your comments to 60225 AT debbugs.gnu.org in the normal way.
Toggle the display of automated, internal messages from the tracker.
Report forwarded
to
guix-patches <at> gnu.org
:
bug#60225
; Package
guix-patches
.
(Tue, 20 Dec 2022 17:42:02 GMT)
Full text and
rfc822 format available.
Acknowledgement sent
to
Attila Lendvai <attila <at> lendvai.name>
:
New bug report received and forwarded. Copy sent to
guix-patches <at> gnu.org
.
(Tue, 20 Dec 2022 17:42:02 GMT)
Full text and
rfc822 format available.
Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):
An example:
(match-record obj <my-type>
(field1 (field2 custom-var-name) field3)
...)
---
guix/records.scm | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/guix/records.scm b/guix/records.scm
index 13463647c8..0b2487a156 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -592,13 +592,16 @@ (define-syntax lookup-field
(define-syntax match-record-inner
(lambda (s)
(syntax-case s ()
- ((_ record type (field rest ...) body ...)
- #`(let-syntax ((field-offset (syntax-rules ()
+ ((_ record type ((field-name variable-name) rest ...) body ...)
+ #'(let-syntax ((field-offset (syntax-rules ()
((_ f)
- (lookup-field field 0 f)))))
+ (lookup-field field-name 0 f)))))
(let* ((offset (type map-fields field-offset))
- (field (struct-ref record offset)))
+ (variable-name (struct-ref record offset)))
(match-record-inner record type (rest ...) body ...))))
+ ((_ record type (field rest ...) body ...)
+ ;; Redirect to the canonical form above.
+ #'(match-record-inner record type ((field field) rest ...) body ...))
((_ record type () body ...)
#'(begin body ...)))))
--
2.35.1
Information forwarded
to
guix-patches <at> gnu.org
:
bug#60225
; Package
guix-patches
.
(Wed, 21 Dec 2022 22:17:01 GMT)
Full text and
rfc822 format available.
Message #8 received at 60225 <at> debbugs.gnu.org (full text, mbox):
Hi,
Attila Lendvai <attila <at> lendvai.name> skribis:
> An example:
>
> (match-record obj <my-type>
> (field1 (field2 custom-var-name) field3)
> ...)
Nice! It looks like a useful extension to me.
> - ((_ record type (field rest ...) body ...)
> - #`(let-syntax ((field-offset (syntax-rules ()
> + ((_ record type ((field-name variable-name) rest ...) body ...)
> + #'(let-syntax ((field-offset (syntax-rules ()
I’d just drop ‘-name’, it’s all names anyway. :-)
Could you also add a test in ‘tests/records.scm’ next to the others?
TIA,
Ludo’.
Information forwarded
to
guix-patches <at> gnu.org
:
bug#60225
; Package
guix-patches
.
(Thu, 22 Dec 2022 02:16:02 GMT)
Full text and
rfc822 format available.
Message #11 received at 60225 <at> debbugs.gnu.org (full text, mbox):
An example:
(match-record obj <my-type>
(field1 (field2 custom-var-name) field3)
...)
* guix/records.scm (match-record-inner): Add support for the new syntax.
* tests/records.scm ("match-record, simple"): Add a simple test case for the
new syntax.
---
guix/records.scm | 9 ++++++---
tests/records.scm | 4 ++--
2 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/guix/records.scm b/guix/records.scm
index 13463647c8..1f097c7108 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -592,13 +592,16 @@ (define-syntax lookup-field
(define-syntax match-record-inner
(lambda (s)
(syntax-case s ()
- ((_ record type (field rest ...) body ...)
- #`(let-syntax ((field-offset (syntax-rules ()
+ ((_ record type ((field variable) rest ...) body ...)
+ #'(let-syntax ((field-offset (syntax-rules ()
((_ f)
(lookup-field field 0 f)))))
(let* ((offset (type map-fields field-offset))
- (field (struct-ref record offset)))
+ (variable (struct-ref record offset)))
(match-record-inner record type (rest ...) body ...))))
+ ((_ record type (field rest ...) body ...)
+ ;; Redirect to the canonical form above.
+ #'(match-record-inner record type ((field field) rest ...) body ...))
((_ record type () body ...)
#'(begin body ...)))))
diff --git a/tests/records.scm b/tests/records.scm
index 8504c8d5a5..b1203dfeb7 100644
--- a/tests/records.scm
+++ b/tests/records.scm
@@ -540,8 +540,8 @@ (define-record-type* <foo> foo make-foo
(first second)
(list first second))
(match-record (foo (first 'a) (second 'b)) <foo>
- (second first)
- (list first second)))))
+ (second (first first/new-var))
+ (list first/new-var second)))))
(test-equal "match-record, unknown field"
'syntax-error
--
2.35.1
Information forwarded
to
guix-patches <at> gnu.org
:
bug#60225
; Package
guix-patches
.
(Thu, 22 Dec 2022 02:18:01 GMT)
Full text and
rfc822 format available.
Message #14 received at 60225 <at> debbugs.gnu.org (full text, mbox):
* tests/records.scm ("match-record, syntactic interference"): New failing test.
---
i'm not sure what's going on here, but it looks like a bug to me.
i've experienced this in real code, and the error message was
not very helpful.
tests/records.scm | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/tests/records.scm b/tests/records.scm
index b1203dfeb7..8a48e2fd07 100644
--- a/tests/records.scm
+++ b/tests/records.scm
@@ -561,4 +561,27 @@ (define-record-type* <foo> foo make-foo
(make-fresh-user-module)))
(lambda (key . args) key)))
+(test-expect-fail 1)
+(test-equal "match-record, syntactic interference"
+ '(1 2)
+ (begin
+ (define* (make-form #:optional (bindings '()))
+ `(begin
+ (use-modules (guix records))
+
+ (let ((first 42)) ; here it does not interfere
+ (define-record-type* <foo> foo make-foo
+ foo?
+ (first foo-first (default 1))
+ (second foo-second))
+
+ (let (,@bindings) ; but here it does interfere
+ (match-record (foo (second 2)) <foo>
+ (first second)
+ (list first second))))))
+ ;; This works fine.
+ (eval (make-form) (make-fresh-user-module))
+ ;; But this fails, although I think it shouldn't.
+ (eval (make-form '((second 43))) (make-fresh-user-module))))
+
(test-end)
--
2.35.1
Reply sent
to
Ludovic Courtès <ludo <at> gnu.org>
:
You have taken responsibility.
(Tue, 27 Dec 2022 22:50:02 GMT)
Full text and
rfc822 format available.
Notification sent
to
Attila Lendvai <attila <at> lendvai.name>
:
bug acknowledged by developer.
(Tue, 27 Dec 2022 22:50:02 GMT)
Full text and
rfc822 format available.
Message #19 received at 60225-done <at> debbugs.gnu.org (full text, mbox):
Hi,
Attila Lendvai <attila <at> lendvai.name> skribis:
> An example:
>
> (match-record obj <my-type>
> (field1 (field2 custom-var-name) field3)
> ...)
>
> * guix/records.scm (match-record-inner): Add support for the new syntax.
> * tests/records.scm ("match-record, simple"): Add a simple test case for the
> new syntax.
Applied, thanks!
Ludo’.
Information forwarded
to
guix-patches <at> gnu.org
:
bug#60225
; Package
guix-patches
.
(Tue, 27 Dec 2022 22:54:02 GMT)
Full text and
rfc822 format available.
Message #22 received at 60225 <at> debbugs.gnu.org (full text, mbox):
Attila Lendvai <attila <at> lendvai.name> skribis:
> * tests/records.scm ("match-record, syntactic interference"): New failing test.
> ---
>
> i'm not sure what's going on here, but it looks like a bug to me.
[...]
> + (let (,@bindings) ; but here it does interfere
> + (match-record (foo (second 2)) <foo>
> + (first second)
> + (list first second))))))
This has to do with how macro “literals” are matched (info "(guile)
Syntax Rules"):
A literal matches an input expression if the input expression is an
identifier with the same name as the literal, and both are unbound(1).
Although literals can be unbound, usually they are bound to allow
them to be imported, exported, and renamed. *Note Modules::, for more
information on imports and exports. In Guile there are a few standard
auxiliary syntax definitions, as specified by R6RS and R7RS:
In the example above, the ‘let’ binding for ‘second’ was shadowing the
other ‘second’.
(I think this was recently discussed on guix-devel or something.)
Ludo’.
bug archived.
Request was from
Debbugs Internal Request <help-debbugs <at> gnu.org>
to
internal_control <at> debbugs.gnu.org
.
(Wed, 25 Jan 2023 12:24:05 GMT)
Full text and
rfc822 format available.
This bug report was last modified 2 years and 146 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.