diff mbox series

[bug#67788] records: Check for missing mandatory innate fields when, inheriting.

Message ID aabc0db6-418f-4d2f-8528-00c44ec5d234@protonmail.com
State New
Headers show
Series [bug#67788] records: Check for missing mandatory innate fields when, inheriting. | expand

Commit Message

Skyler Ferris Dec. 28, 2023, 12:30 a.m. UTC
his gives the user a more helpful error message if they inherit from
another instance but forget to supply a value for a field which is
innate and has no default.

* guix/records.scm (record-inheritance): Check for missing innate fields

Change-Id: I08b898d8da551af50aa5b0b60187dcad8d259bc1
---
# This version of the commit addse a test case which confirms that
# theuseful commit message is given. It is based on the test immediately
# above it, which checks for the error message in the non-innate case.
#
# Additionally, I can now confirm that all tests pass with this commit.
# The failures I noted previously were indeed environmental problems and
# have been resolved locally (and were unrelated to the commit).
  guix/records.scm  |  9 +++++++++
  tests/records.scm | 18 ++++++++++++++++++
  2 files changed, 27 insertions(+)


base-commit: fe86819d8bde674766659c22b215d3a689a8026e

Comments

Skyler Ferris Dec. 28, 2023, 2:05 a.m. UTC | #1
It looks like this update has the some non-breaking space problem that 
also occurred on the other patch I updated, 87786. I'll send a fixed 
version once I am able.
diff mbox series

Patch

diff --git a/guix/records.scm b/guix/records.scm
index f4d12a861d..109c5cf4dc 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -161,6 +161,15 @@  (define-syntax make-syntactic-constructor
                 (record-error 'name s "extraneous field initializers ~a"
                               unexpected)))

+           ;; Make sure all mandatory and innate fields are included.
+           (let* ((fields           (map (compose car syntax->datum) 
field+value))
+                  (optional         (map (compose car syntax->datum) 
'defaults))
+                  (mandatory        (lset-difference eq? '(expected 
...) optional))
+                  (mandatory+innate (lset-intersection eq? mandatory 
'innate))
+                  (missing          (lset-difference eq? 
mandatory+innate fields)))
+             (when (pair? missing)
+               (record-error 'name s "missing field initializers ~a" 
missing)))
+
             #`(make-struct/no-tail type
                            #,@(map (lambda (field index)
                                      (or (field-inherited-value field)
diff --git a/tests/records.scm b/tests/records.scm
index 5464892d3b..39f4bfdf9c 100644
--- a/tests/records.scm
+++ b/tests/records.scm
@@ -402,6 +402,24 @@  (define (location-alist loc)
             (string-match "missing .*initialize.*baz" message)
             (equal? form '(foo))))))

+(test-assert "define-record-type* & innate & missing initializers"
+  (catch 'syntax-error
+    (lambda ()
+      (eval '(begin
+               (define-record-type* <foo> foo make-foo
+                 foo?
+                 (bar foo-bar (default 42))
+                 (baz foo-baz (innate)))
+
+               (let ((base (foo (bar #t) (baz #t))))
+                 (foo (inherit base))))
+            (test-module))
+      #f)
+    (lambda (key proc message location form . args)
+      (and (eq? proc 'foo)
+           (string-match "missing .*initialize.*baz" message)
+           (equal? form '(foo (inherit base)))))))
+
  (test-assert "define-record-type* & extra initializers"
    (catch 'syntax-error
      (lambda ()