unofficial mirror of bug-guix@gnu.org 
 help / color / mirror / code / Atom feed
* bug#34999: Record special field abstraction leakage
@ 2019-03-26  9:38 Ludovic Courtès
  2021-08-24 15:47 ` Leo Prikler
  0 siblings, 1 reply; 2+ messages in thread
From: Ludovic Courtès @ 2019-03-26  9:38 UTC (permalink / raw)
  To: bug-Guix

The changes I made in version-control.scm and gnucash.scm in commit
e6301fb76d0a8d931ece2e18d197e3c2cc53fc6c revealed an abstraction leakage
I wasn’t aware of: there’s a pattern where users “see” that thunked
fields are thunked:

  (package
    ;; …
    (inputs …)
    (arguments `(foo bar ,(inputs) …)))  ;<- here ‘inputs’ is seen as a thunk

Fortunately I could only find two occurrences of this and this use case
is more elegantly replaced by:

  (package-inputs this-record)

… which also has better semantics.  It’s remains a bug, though.

Ludo’.

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

* bug#34999: Record special field abstraction leakage
  2019-03-26  9:38 bug#34999: Record special field abstraction leakage Ludovic Courtès
@ 2021-08-24 15:47 ` Leo Prikler
  0 siblings, 0 replies; 2+ messages in thread
From: Leo Prikler @ 2021-08-24 15:47 UTC (permalink / raw)
  To: Ludovic Courtès, 34999

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

Hi Ludo,

I think I have found out why users see the thunked fields as below.
Am Dienstag, den 26.03.2019, 10:38 +0100 schrieb Ludovic Courtès:
> The changes I made in version-control.scm and gnucash.scm in commit
> e6301fb76d0a8d931ece2e18d197e3c2cc53fc6c revealed an abstraction
> leakage
> I wasn’t aware of: there’s a pattern where users “see” that thunked
> fields are thunked:
> 
>   (package
>     ;; …
>     (inputs …)
>     (arguments `(foo bar ,(inputs) …)))  ;<- here ‘inputs’ is seen as
> a thunk
The issue is that for constructing the records, we let*-bind the field
names to their values before calling the constructor.  In these let*-
bindings the fields are already wrapped, e.g. inputs will be bound to
the value that the record field inputs will have, not to the raw value.

I've attached a patch to fix this issue as well as a MWE to try it out.
I'm not sure about the broader semantics of this patch, though.  I fear
that exposing raw values through let-binding probably eliminates the
delayed/thunked nature of said fields in some ways.  WDYT?

[-- Attachment #2: 0001-guix-records-let-bind-raw-values-wrap-them-in-constr.patch --]
[-- Type: text/x-patch, Size: 2684 bytes --]

From 1f38ff4c8b93cde533cf3d3f67358aafe9cf3dfa Mon Sep 17 00:00:00 2001
From: Leo Prikler <leo.prikler@student.tugraz.at>
Date: Tue, 24 Aug 2021 17:32:33 +0200
Subject: [PATCH] guix: records: let*-bind raw values, wrap them in
 constructor.

This fixes the abstraction leakage mentioned in <https://bugs.gnu.org/34999>.

* guix/records.scm (make-syntactic-constructor)[field-bindings]: Bind to raw
value.
[field-value]: Always wrap the value.
[record-inheritance]: Wrap "inherited" values.
---
 guix/records.scm | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/guix/records.scm b/guix/records.scm
index ed94c83dac..074f1650c8 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -153,7 +153,10 @@ of TYPE matches the expansion-time ABI."
 
            #`(make-struct/no-tail type
                           #,@(map (lambda (field index)
-                                    (or (field-inherited-value field)
+                                    (or (and=>
+                                         (field-inherited-value field)
+                                         (lambda (value)
+                                           (wrap-field-value field value)))
                                         (if (innate-field? field)
                                             (wrap-field-value
                                              field (field-default-value field))
@@ -211,8 +214,7 @@ of TYPE matches the expansion-time ABI."
            (map (lambda (field+value)
                   (syntax-case field+value ()
                     ((field value)
-                     #`(field
-                        #,(wrap-field-value #'field #'value)))))
+                     #`(field value))))
                 field+value))
 
          (syntax-case s (inherit expected ...)
@@ -224,10 +226,11 @@ of TYPE matches the expansion-time ABI."
            ((_ (field value) (... ...))
             (let ((fields (map syntax->datum #'(field (... ...)))))
               (define (field-value f)
-                (or (find (lambda (x)
-                            (eq? f (syntax->datum x)))
-                          #'(field (... ...)))
-                    (wrap-field-value f (field-default-value f))))
+                (wrap-field-value f
+                                  (or (find (lambda (x)
+                                              (eq? f (syntax->datum x)))
+                                            #'(field (... ...)))
+                                      (field-default-value f))))
 
               ;; Pass S to make sure source location info is preserved.
               (report-duplicate-field-specifier 'name s)
-- 
2.33.0


[-- Attachment #3: 34999-mwe.scm --]
[-- Type: text/x-scheme, Size: 537 bytes --]

(use-modules (guix records))

(define-record-type* <thing> thing make-thing
  thing?
  this-thing
  (name thing-name (thunked))
  (name2 thing-name2))

(let* ((%thing
       (thing
        (name "foo")
        (name2 name)))
       (%thing2
        (thing
         (inherit %thing)
         (name "bar"))))
  (format #t "thing1:~%  name: ~a~%  name2: ~a~%~%"
          (thing-name %thing)
          (thing-name2 %thing))

  (format #t "thing2:~%  name: ~a~%  name2: ~a~%"
          (thing-name %thing2)
          (thing-name2 %thing2)))

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

end of thread, other threads:[~2021-08-24 15:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-26  9:38 bug#34999: Record special field abstraction leakage Ludovic Courtès
2021-08-24 15:47 ` Leo Prikler

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

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).