Subject: [bug#49671] [PATCH] guix: records: Improve error reporting.

From: Julien Lepiller <[email-masked]>
Date: Sun, 31 Oct 2021 03:06:35 +0100

Hi Guix!

Here is a new patch that attempts to better catch various causes of
syntax errors in records. I think I fixed all the concerns Ludo had,
and I draw some inspiration from https://issues.guix.gnu.org/50914 for
using conditions.

Here are examples of what you get:

(operating-system
  ())

test.scm:1:0: error: invalid field specifier: ()
hint: The format of a field is `(field value)'

(operating-system
  ((services %base-services)))

test.scm:1:0: error: invalid field specifier: (services %base-services)
is not a valid field name

(operating-system
  (services))

test.scm:1:0: error: missing value in field specifier
hint: The field is missing a value: `(services <value>)'.

(operating-system
  (services (service tor-service-type) %base-services))

test.scm:1:0: error: multiple values in field specifier
hint: 2 values were associated with field `services'.  We got the
following values:

     (service tor-service-type)
     %base-services

Perhaps the additional values were intended to be other field
specifiers.  This usually indicates missing or misplaced parenthesis.

(operating-system
  services %base-services)

test.scm:1:0: error: invalid field specifier: #<syntax services>
hint: The format of a field is `(field value)'

(not sure why the last one is wrapped in syntax...)
From 9e08a887a08da3f0cc132d148b748eb2ce7db135 Mon Sep 17 00:00:00 2001
Message-Id: <[email-masked]>
From: Julien Lepiller <[email-masked]>
Date: Sun, 31 Oct 2021 02:58:14 +0100
Subject: [PATCH] guix: records: Improve error reporting.

* guix/records.scm (report-invalid-field-specifier): Handle various
invalidity causes separately.
---
 guix/records.scm | 56 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)

diff --git a/guix/records.scm b/guix/records.scm
index ed94c83dac..9baa2c2f1d 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -1,6 +1,7 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <[email-masked]>
 ;;; Copyright © 2018 Mark H Weaver <[email-masked]>
+;;; Copyright © 2021 Julien Lepiller <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -21,9 +22,13 @@ (define-module (guix records)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-9)
   #:use-module (srfi srfi-26)
+  #:use-module (srfi srfi-35)
+  #:use-module (ice-9 format)
   #:use-module (ice-9 match)
   #:use-module (ice-9 regex)
   #:use-module (ice-9 rdelim)
+  #:use-module (guix diagnostics)
+  #:use-module (guix ui)
   #:autoload (system base target) (target-most-positive-fixnum)
   #:export (define-record-type*
             this-record
@@ -83,10 +88,53 @@ (define-syntax record-error
          ;; WEIRD may be an identifier, thus lacking source location info, and
          ;; BINDINGS is a list, also lacking source location info.  Hopefully
          ;; PARENT-FORM provides source location info.
-         (apply syntax-violation name "invalid field specifier"
-                (if parent-form
-                    (list parent-form #'weird)
-                    (list #'weird)))))))
+         (let* ((weird-properties (source-properties #'weird))
+                (parent-properties (and parent-form (syntax-source parent-form)))
+                (location (source-properties->location
+                            (or (and (not (null? weird-properties)) weird-properties)
+                                (and (not (null? parent-properties)) parent-properties)
+                                '()))))
+           (syntax-case #'weird ()
+             (()                             ;the empty list
+              (raise-exception
+                (condition
+                  (&message (message (format #f (G_ "invalid field specifier: ~a")
+                                             #'weird)))
+                  (&error-location (location location))
+                  (&fix-hint (hint (G_ "The format of a field is `(field value)'"))))))
+             (((field ...) _ ...)            ;a list whose first element is a list
+              (raise-exception
+                (condition
+                  (&message (message (format #f (G_ "invalid field specifier: ~a \
+is not a valid field name")
+                                             (map syntax->datum #'(field ...)))))
+                  (&error-location (location location)))))
+             ((field)                        ;a list with one element
+              (raise-exception
+                (condition
+                  (&message (message (G_ "missing value in field specifier")))
+                  (&error-location (location location))
+                  (&fix-hint (hint (format #f (G_ "The field is missing a value: `(~a <value>)'.")
+                                           (syntax->datum #'field)))))))
+             ((field value ...)              ;any other list
+              (raise-exception
+                (condition
+                  (&message (message (G_ "multiple values in field specifier")))
+                  (&error-location (location location))
+                  (&fix-hint (hint (format #f (G_ "~a values were associated with \
+field `~a'.  We got the following values:~%@lisp~%~{~a~%~}~%@end lisp~%Perhaps the additional values
+were intended to be other field specifiers.  This usually indicates missing or \
+misplaced parenthesis.")
+                                           (length #'(value ...))
+                                           (syntax->datum #'field)
+                                           (map syntax->datum #'(value ...))))))))
+             (field                          ;not a list
+              (raise-exception
+                (condition
+                  (&message (message (format #f (G_ "invalid field specifier: ~a")
+                                             #'weird)))
+                  (&error-location (location location))
+                  (&fix-hint (hint (G_ "The format of a field is `(field value)'"))))))))))))
 
   (define (report-duplicate-field-specifier name ctor)
     "Report the first duplicate identifier among the bindings in CTOR."
-- 
2.33.1