From patchwork Fri Apr 28 19:19:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\\(" X-Patchwork-Id: 49658 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id B748327BBE2; Fri, 28 Apr 2023 20:20:38 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id AB0B627BBEB for ; Fri, 28 Apr 2023 20:20:37 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1psTdg-0002SZ-Up; Fri, 28 Apr 2023 15:20:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1psTdV-0002Pd-Qh for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:05 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1psTdU-0004O8-Db for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:05 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1psTdU-00063l-8w for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:04 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63135] [PATCH v2 1/5] records: match-record: Raise a syntax error if TYPE is nonexistent. Resent-From: "(" Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 28 Apr 2023 19:20:04 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63135 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 63135@debbugs.gnu.org Cc: "\(" , Josselin Poiret Received: via spool by 63135-submit@debbugs.gnu.org id=B63135.168270956223151 (code B ref 63135); Fri, 28 Apr 2023 19:20:04 +0000 Received: (at 63135) by debbugs.gnu.org; 28 Apr 2023 19:19:22 +0000 Received: from localhost ([127.0.0.1]:34542 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1psTcn-00061G-Qr for submit@debbugs.gnu.org; Fri, 28 Apr 2023 15:19:22 -0400 Received: from knopi.disroot.org ([178.21.23.139]:41914) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1psTcl-00060U-Gy for 63135@debbugs.gnu.org; Fri, 28 Apr 2023 15:19:19 -0400 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 0323B40274; Fri, 28 Apr 2023 21:19:19 +0200 (CEST) X-Virus-Scanned: SPAM Filter at disroot.org Received: from knopi.disroot.org ([127.0.0.1]) by localhost (disroot.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id QQTJKUjPd4rO; Fri, 28 Apr 2023 21:19:15 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1682709555; bh=8BmRwHNyibV4LMLlHFnVJrXMVrH1J/ivFDj7yyHIQKM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=BuBC4y/xa8T62CYq7v2HhLy/Ep/NT0Xfu0vYmzjKPAccADO36SquZPU/eXv44rbG7 NWdLiaWn1FrPF/0fu1FKm2vJwvwKsQs8oIpW9hholNS9rEjBTxWyJDzC9Y7ycOm63Y G29jMVYhRqzdaWX+U+kTvq5JAYwStYrqopjt0+VbqX+C4FrcvAfjdUgV2vKl1WiZVj J64tp23IXvkLL1V+0W312yUZ86cf0FoB2y0VNMz7xno10L625LkgZ4PD/uUx+Mjluf 8aMmsXpdlswMbvgJiHJdUJofI/Du/71YSmeAVtNT18cptcxY8YUaKoCa9Gz1jYZY2n UzcjD0JxvIzpw== Date: Fri, 28 Apr 2023 20:19:01 +0100 Message-Id: <20230428191905.13860-2-paren@disroot.org> In-Reply-To: <20230428191905.13860-1-paren@disroot.org> References: <20230428191905.13860-1-paren@disroot.org> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: "\(" X-ACL-Warn: , "\( via Guix-patches" X-Patchwork-Original-From: "\( via Guix-patches" via From: "\\(" Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * guix/records.scm (match-record): Raise a human-compherensible syntax error if the given record type identifier is unbound. Co-authored-by: Josselin Poiret --- guix/records.scm | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/guix/records.scm b/guix/records.scm index 7d43b064d8..d8966998c1 100644 --- a/guix/records.scm +++ b/guix/records.scm @@ -105,7 +105,12 @@ (define (report-duplicate-field-specifier name ctor) (define-syntax map-fields (lambda (x) - (syntax-violation 'map-fields "bad use of syntactic keyword" x x))) + (syntax-case x () + ((_ type within) + (syntax-violation (syntax->datum #'within) + "undefined guix record-type" + #'type)) + (_ (syntax-violation 'map-fields "bad use of syntactic keyword" x x))))) (define-syntax-parameter this-record (lambda (s) @@ -459,7 +464,7 @@ (define-syntax type "This macro lets us query record type info at macro-expansion time." (syntax-case s (map-fields) - ((_ map-fields macro) + ((_ (map-fields _ _) macro) #'(macro (field ...))) (id (identifier? #'id) @@ -595,7 +600,7 @@ (define-syntax match-record-inner #'(let-syntax ((field-offset (syntax-rules () ((_ f) (lookup-field field 0 f))))) - (let* ((offset (type map-fields field-offset)) + (let* ((offset (type (map-fields type match-record) field-offset)) (variable (struct-ref record offset))) (match-record-inner record type (rest ...) body ...)))) ((_ record type (field rest ...) body ...) From patchwork Fri Apr 28 19:19:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\\(" X-Patchwork-Id: 49660 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 48B4427BBE9; Fri, 28 Apr 2023 20:20:48 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 084D327BBE2 for ; Fri, 28 Apr 2023 20:20:46 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1psTdd-0002S1-Nx; Fri, 28 Apr 2023 15:20:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1psTdT-0002PR-Vv for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:06 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1psTdS-0004FU-WE for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1psTdS-00063J-RL for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63135] [PATCH v2 2/5] records: match-record: Display more helpful field-not-found error. Resent-From: "(" Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 28 Apr 2023 19:20:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63135 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 63135@debbugs.gnu.org Cc: "\(" , Josselin Poiret Received: via spool by 63135-submit@debbugs.gnu.org id=B63135.168270956023129 (code B ref 63135); Fri, 28 Apr 2023 19:20:02 +0000 Received: (at 63135) by debbugs.gnu.org; 28 Apr 2023 19:19:20 +0000 Received: from localhost ([127.0.0.1]:34536 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1psTcm-00060u-FD for submit@debbugs.gnu.org; Fri, 28 Apr 2023 15:19:20 -0400 Received: from knopi.disroot.org ([178.21.23.139]:41434) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1psTcj-000607-NQ for 63135@debbugs.gnu.org; Fri, 28 Apr 2023 15:19:19 -0400 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 155EA40320; Fri, 28 Apr 2023 21:19:17 +0200 (CEST) X-Virus-Scanned: SPAM Filter at disroot.org Received: from knopi.disroot.org ([127.0.0.1]) by localhost (disroot.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 7nB_b2AMIdMi; Fri, 28 Apr 2023 21:19:15 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1682709555; bh=aa/TNg6F4FuftaCqmdSb5c/md+/36DE3Ku+qtLN2znA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=fV9CW7LSXi9ARkPMWRX0vatEhki9vuoVhPNH57YPWP5y7/qSbxi4fZtSgz1qlrGRi ME4U7uMz35JWmjPo8RDzMyr1ywuSMdATBbg3TjmaVs+gVktGS/8iJ+711akgmEvBKS YeLP5rzBjMdHDjVCdXqjhikwxyPrLc1fyXOVndBFH0T7kk4r75wHsfWq26qmOFzGMy gYX+0/QHMZgGdBARfaGn6Mg4qQOKh9LQSwyKRIyVApYgsOKVX4XRWaFJEsHaPwZsH8 neJg9p1J2PxPc5MGX1VPCB7JRBT32sAbbtPDr0D7gBXf0QqoJiceD6euNMaMuU5Y3Q PnlS+T56eQLsA== Date: Fri, 28 Apr 2023 20:19:02 +0100 Message-Id: <20230428191905.13860-3-paren@disroot.org> In-Reply-To: <20230428191905.13860-1-paren@disroot.org> References: <20230428191905.13860-1-paren@disroot.org> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: "\(" X-ACL-Warn: , "\( via Guix-patches" X-Patchwork-Original-From: "\( via Guix-patches" via From: "\\(" Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * guix/records.scm (match-record): Display MATCH-RECORD as the origin of "unknown record type field" errors. Show the original MATCH-RECORD form, rather than an intermediate LOOKUP-FIELD form, within said errors. --- guix/records.scm | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/guix/records.scm b/guix/records.scm index d8966998c1..4bee9d0aac 100644 --- a/guix/records.scm +++ b/guix/records.scm @@ -582,44 +582,46 @@ (define-syntax lookup-field (lambda (s) "Look up FIELD in the given list and return an expression that represents its offset in the record. Raise a syntax violation when the field is not -found." +found, displaying it as originating in form S*." (syntax-case s () - ((_ field offset ()) - (syntax-violation 'lookup-field "unknown record type field" - s #'field)) - ((_ field offset (head tail ...)) + ((_ s* field offset ()) + (syntax-violation 'match-record + "unknown record type field" + #'s* #'field)) + ((_ s* field offset (head tail ...)) (free-identifier=? #'field #'head) #'offset) - ((_ field offset (_ tail ...)) - #'(lookup-field field (+ 1 offset) (tail ...)))))) + ((_ s* field offset (_ tail ...)) + #'(lookup-field s* field (+ 1 offset) (tail ...)))))) (define-syntax match-record-inner (lambda (s) (syntax-case s () - ((_ record type ((field variable) rest ...) body ...) + ((_ s* record type ((field variable) rest ...) body ...) #'(let-syntax ((field-offset (syntax-rules () ((_ f) - (lookup-field field 0 f))))) + (lookup-field s* field 0 f))))) (let* ((offset (type (map-fields type match-record) field-offset)) (variable (struct-ref record offset))) - (match-record-inner record type (rest ...) body ...)))) - ((_ record type (field rest ...) body ...) + (match-record-inner s* record type (rest ...) body ...)))) + ((_ s* record type (field rest ...) body ...) ;; Redirect to the canonical form above. - #'(match-record-inner record type ((field field) rest ...) body ...)) - ((_ record type () body ...) + #'(match-record-inner s* record type ((field field) rest ...) body ...)) + ((_ s* record type () body ...) #'(begin body ...))))) (define-syntax match-record - (syntax-rules () + (lambda (s) "Bind each FIELD of a RECORD of the given TYPE to it's FIELD name. The order in which fields appear does not matter. A syntax error is raised if an unknown field is queried. The current implementation does not support thunked and delayed fields." ;; TODO support thunked and delayed fields - ((_ record type (fields ...) body ...) - (if (eq? (struct-vtable record) type) - (match-record-inner record type (fields ...) body ...) - (throw 'wrong-type-arg record))))) + (syntax-case s () + ((_ record type (fields ...) body ...) + #`(if (eq? (struct-vtable record) type) + (match-record-inner #,s record type (fields ...) body ...) + (throw 'wrong-type-arg record)))))) ;;; records.scm ends here From patchwork Fri Apr 28 19:19:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\\(" X-Patchwork-Id: 49662 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 8C3B027BBE2; Fri, 28 Apr 2023 20:21:00 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 6BCE527BBE9 for ; Fri, 28 Apr 2023 20:20:58 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1psTdm-0002Wy-EK; Fri, 28 Apr 2023 15:20:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1psTdU-0002PX-9C for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:05 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1psTdT-0004Nt-Ti for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:04 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1psTdT-00063Y-Oh for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63135] [PATCH v2 3/5] records: match-record: Support thunked and delayed fields. Resent-From: "(" Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 28 Apr 2023 19:20:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63135 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 63135@debbugs.gnu.org Cc: "\(" , Josselin Poiret Received: via spool by 63135-submit@debbugs.gnu.org id=B63135.168270956223145 (code B ref 63135); Fri, 28 Apr 2023 19:20:03 +0000 Received: (at 63135) by debbugs.gnu.org; 28 Apr 2023 19:19:22 +0000 Received: from localhost ([127.0.0.1]:34540 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1psTcn-000619-AO for submit@debbugs.gnu.org; Fri, 28 Apr 2023 15:19:21 -0400 Received: from knopi.disroot.org ([178.21.23.139]:41632) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1psTck-00060A-B2 for 63135@debbugs.gnu.org; Fri, 28 Apr 2023 15:19:19 -0400 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id B663240316; Fri, 28 Apr 2023 21:19:17 +0200 (CEST) X-Virus-Scanned: SPAM Filter at disroot.org Received: from knopi.disroot.org ([127.0.0.1]) by localhost (disroot.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 1Dl51W6z95T8; Fri, 28 Apr 2023 21:19:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1682709556; bh=VR5KpZOjanz7iRoDHHzMvUH88/n7hp56BE3Gxtae7XM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=kPcU7yCT+hatnu3N85nZNDXeAD6i5uLg0pBy398tvQ/6iER3r2X6IyGeBMiixqu7o Sz0H2PURAMtzhiH6ezZ0CD4NfTmsiUsw7UCT8zr+xD3rzAXl3h7kiuBup2wA9TELGD In+p0fCD68ofN6bHshT/VFIv/3A9gNBr4E50POVaOC8fCDOTnvQlY6bk/ahwBhLLHL jk+rTbPiWjiLGpeXgq5XECZxM5DKZStL+zXKZJDie+n6zbPHidIpYsM8A3vuXOl7ec lhM031MWW8XKwxx37NbyWbFOFKE20lsWiTtyFLlVM3HQgc3xnWP7bpWuU+UAPO18gt 4ZX/3ObuldBtA== Date: Fri, 28 Apr 2023 20:19:03 +0100 Message-Id: <20230428191905.13860-4-paren@disroot.org> In-Reply-To: <20230428191905.13860-1-paren@disroot.org> References: <20230428191905.13860-1-paren@disroot.org> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: "\(" X-ACL-Warn: , "\( via Guix-patches" X-Patchwork-Original-From: "\( via Guix-patches" via From: "\\(" Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * guix/records.scm (match-record): Unwrap matched thunked and delayed fields. * tests/records.scm ("match-record, thunked field", "match-record, delayed field"): New tests. --- guix/records.scm | 60 ++++++++++++++++++++++++++++++----------------- tests/records.scm | 29 +++++++++++++++++++++++ 2 files changed, 68 insertions(+), 21 deletions(-) diff --git a/guix/records.scm b/guix/records.scm index 4bee9d0aac..041eb2f297 100644 --- a/guix/records.scm +++ b/guix/records.scm @@ -21,6 +21,7 @@ (define-module (guix records) #:use-module (srfi srfi-1) #:use-module (srfi srfi-9) #:use-module (srfi srfi-26) + #:use-module (srfi srfi-71) #:use-module (ice-9 match) #:use-module (ice-9 rdelim) #:autoload (system base target) (target-most-positive-fixnum) @@ -428,10 +429,19 @@ (define (compute-abi-cookie field-specs) (defaults (filter-map field-default-value #'((field properties ...) ...))) (sanitizers (filter-map field-sanitizer - #'((field properties ...) ...))) + #'((field properties ...) ...))) (cookie (compute-abi-cookie field-spec))) (with-syntax (((field-spec* ...) (map field-spec->srfi-9 field-spec)) + ((field-type ...) + (map (match-lambda + ((? thunked-field?) + (datum->syntax s 'thunked)) + ((? delayed-field?) + (datum->syntax s 'delayed)) + (else + (datum->syntax s 'normal))) + field-spec)) ((thunked-field-accessor ...) (filter-map (lambda (field) (and (thunked-field? field) @@ -465,7 +475,7 @@ (define-syntax type macro-expansion time." (syntax-case s (map-fields) ((_ (map-fields _ _) macro) - #'(macro (field ...))) + #'(macro ((field field-type) ...))) (id (identifier? #'id) #'#,(rtd-identifier #'type))))) @@ -578,31 +588,42 @@ (define (recutils->alist port) ;;; Pattern matching. ;;; -(define-syntax lookup-field +(define-syntax lookup-field+wrapper (lambda (s) - "Look up FIELD in the given list and return an expression that represents -its offset in the record. Raise a syntax violation when the field is not -found, displaying it as originating in form S*." - (syntax-case s () - ((_ s* field offset ()) + "Look up FIELD in the given list and return both an expression that represents +its offset in the record and a procedure that wraps it to return its \"true\" value +(for instance, FORCE is returned in the case of a delayed field). RECORD is passed +to thunked values. Raise a syntax violation when the field is not found, displaying +it as originating in form S*." + (syntax-case s (normal delayed thunked) + ((_ s* record field offset ()) (syntax-violation 'match-record "unknown record type field" #'s* #'field)) - ((_ s* field offset (head tail ...)) + ((_ s* record field offset ((head normal) tail ...)) + (free-identifier=? #'field #'head) + #'(values offset identity)) + ((_ s* record field offset ((head delayed) tail ...)) (free-identifier=? #'field #'head) - #'offset) - ((_ s* field offset (_ tail ...)) - #'(lookup-field s* field (+ 1 offset) (tail ...)))))) + #'(values offset force)) + ((_ s* record field offset ((head thunked) tail ...)) + (free-identifier=? #'field #'head) + #'(values offset (cut <> record))) + ((_ s* record field offset (_ tail ...)) + #'(lookup-field+wrapper s* record field + (+ 1 offset) (tail ...)))))) (define-syntax match-record-inner (lambda (s) (syntax-case s () ((_ s* record type ((field variable) rest ...) body ...) - #'(let-syntax ((field-offset (syntax-rules () - ((_ f) - (lookup-field s* field 0 f))))) - (let* ((offset (type (map-fields type match-record) field-offset)) - (variable (struct-ref record offset))) + #'(let-syntax ((field-offset+wrapper + (syntax-rules () + ((_ f) + (lookup-field+wrapper s* record field 0 f))))) + (let* ((offset wrap (type (map-fields type match-record) + field-offset+wrapper)) + (variable (wrap (struct-ref record offset)))) (match-record-inner s* record type (rest ...) body ...)))) ((_ s* record type (field rest ...) body ...) ;; Redirect to the canonical form above. @@ -614,10 +635,7 @@ (define-syntax match-record (lambda (s) "Bind each FIELD of a RECORD of the given TYPE to it's FIELD name. The order in which fields appear does not matter. A syntax error is raised if -an unknown field is queried. - -The current implementation does not support thunked and delayed fields." - ;; TODO support thunked and delayed fields +an unknown field is queried." (syntax-case s () ((_ record type (fields ...) body ...) #`(if (eq? (struct-vtable record) type) diff --git a/tests/records.scm b/tests/records.scm index b1203dfeb7..4f0aeb3903 100644 --- a/tests/records.scm +++ b/tests/records.scm @@ -561,4 +561,33 @@ (define-record-type* foo make-foo (make-fresh-user-module))) (lambda (key . args) key))) +(test-equal "match-record, delayed field" + "foo bar bar foo" + (begin + (define-record-type* with-delayed make-with-delayed + with-delayed? + (delayed with-delayed-delayed + (delayed))) + + (let ((rec (with-delayed + (delayed "foo bar bar foo")))) + (match-record rec (delayed) + delayed)))) + +(test-equal "match-record, thunked field" + '("foo" "foobar") + (begin + (define-record-type* with-thunked make-with-thunked + with-thunked? + (normal with-thunked-normal) + (thunked with-thunked-thunked + (thunked))) + + (let ((rec (with-thunked + (normal "foo") + (thunked (string-append (with-thunked-normal this-record) + "bar"))))) + (match-record rec (normal thunked) + (list normal thunked))))) + (test-end) From patchwork Fri Apr 28 19:19:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\\(" X-Patchwork-Id: 49661 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id ED7E027BBEA; Fri, 28 Apr 2023 20:20:53 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 4610A27BBE2 for ; Fri, 28 Apr 2023 20:20:53 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1psTdp-0002ag-Pp; Fri, 28 Apr 2023 15:20:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1psTdT-0002PS-W7 for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:05 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1psTdS-0004CN-HT for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1psTdS-00063B-D6 for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63135] [PATCH v2 4/5] dir-locals: Fix MATCH-RECORD indentation. Resent-From: "(" Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 28 Apr 2023 19:20:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63135 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 63135@debbugs.gnu.org Cc: "\(" , Josselin Poiret Received: via spool by 63135-submit@debbugs.gnu.org id=B63135.168270956023123 (code B ref 63135); Fri, 28 Apr 2023 19:20:02 +0000 Received: (at 63135) by debbugs.gnu.org; 28 Apr 2023 19:19:20 +0000 Received: from localhost ([127.0.0.1]:34534 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1psTcm-00060n-6s for submit@debbugs.gnu.org; Fri, 28 Apr 2023 15:19:20 -0400 Received: from knopi.disroot.org ([178.21.23.139]:41590) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1psTck-000608-5B for 63135@debbugs.gnu.org; Fri, 28 Apr 2023 15:19:18 -0400 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id A32E840274; Fri, 28 Apr 2023 21:19:17 +0200 (CEST) X-Virus-Scanned: SPAM Filter at disroot.org Received: from knopi.disroot.org ([127.0.0.1]) by localhost (disroot.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Z-0VSZKiypH3; Fri, 28 Apr 2023 21:19:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1682709556; bh=RQOqLPb86/6MOEVAHzd6IhmBJjOyLl+ukQRIqgZf3aw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=kY1H9jUQ8yJLSXlM0ICIB770OzrGrAy9BbzkfZGtlkkN6WSAMv2Az2YTQAZpxvWzi sJiE13uxwD2I/Da5eJfPjuGlKPD73hCin2ZUnkfBHCyYV6p2PyvhqJK0PebIoyLxk0 crPjsHfKrN0wp3/eQHC9pG8evnJIV0yOHuzw+U2RElKyPbni2Ffib8zsMMTYck5Cyg 0BCCHxJFpoFB4uYKiQHjvanKfpgpjEloh7fkYbuAybqGPZuTQ5u9GBaA7O7NyYJ3Md +JNwvX94mwO8ZN4GuQVShgCmVbGoEydRPRTYf2tlO+nYw7aegAEapByTBWgegla7CE 9A1ujTqB0LuqQ== Date: Fri, 28 Apr 2023 20:19:04 +0100 Message-Id: <20230428191905.13860-5-paren@disroot.org> In-Reply-To: <20230428191905.13860-1-paren@disroot.org> References: <20230428191905.13860-1-paren@disroot.org> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: "\(" X-ACL-Warn: , "\( via Guix-patches" X-Patchwork-Original-From: "\( via Guix-patches" via From: "\\(" Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * .dir-locals.el: Treat the fourth form onwards as the body, rather than the third onwards. --- .dir-locals.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.dir-locals.el b/.dir-locals.el index a5f627a9ba..3ffd25ee94 100644 --- a/.dir-locals.el +++ b/.dir-locals.el @@ -70,7 +70,7 @@ (eval . (put 'guard 'scheme-indent-function 1)) (eval . (put 'lambda* 'scheme-indent-function 1)) (eval . (put 'substitute* 'scheme-indent-function 1)) - (eval . (put 'match-record 'scheme-indent-function 2)) + (eval . (put 'match-record 'scheme-indent-function 3)) ;; TODO: Contribute these to Emacs' scheme-mode. (eval . (put 'let-keywords 'scheme-indent-function 3)) From patchwork Fri Apr 28 19:19:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\\(" X-Patchwork-Id: 49659 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id BFC5127BBEA; Fri, 28 Apr 2023 20:20:40 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 1824B27BBE2 for ; Fri, 28 Apr 2023 20:20:40 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1psTda-0002Ra-TH; Fri, 28 Apr 2023 15:20:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1psTdT-0002PN-Tx for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:05 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1psTdT-0004JV-Di for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1psTdT-00063Q-8h for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63135] [PATCH v2 5/5] records: Add MATCH-RECORD-LAMBDA. Resent-From: "(" Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 28 Apr 2023 19:20:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63135 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 63135@debbugs.gnu.org Cc: "\(" , Josselin Poiret Received: via spool by 63135-submit@debbugs.gnu.org id=B63135.168270956123138 (code B ref 63135); Fri, 28 Apr 2023 19:20:03 +0000 Received: (at 63135) by debbugs.gnu.org; 28 Apr 2023 19:19:21 +0000 Received: from localhost ([127.0.0.1]:34538 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1psTcm-000615-UH for submit@debbugs.gnu.org; Fri, 28 Apr 2023 15:19:21 -0400 Received: from knopi.disroot.org ([178.21.23.139]:41768) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1psTck-00060H-Us for 63135@debbugs.gnu.org; Fri, 28 Apr 2023 15:19:19 -0400 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 358B44021E; Fri, 28 Apr 2023 21:19:18 +0200 (CEST) X-Virus-Scanned: SPAM Filter at disroot.org Received: from knopi.disroot.org ([127.0.0.1]) by localhost (disroot.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PBCvUDFffq0U; Fri, 28 Apr 2023 21:19:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1682709556; bh=73P5wsZTJ6hDn+xXhAEBY6bfYosiRAiY3VVjTHHz1Kc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=QEmJ9uKAGI2WWRhC/AzyLSwDXXpqjvEavfsvFFmtHoxmGC1PImlcalhxVI+rHY7K5 QqLGZ9g5XvN4rEEhhe+AKChydkBNT/+91maEKHCKuydP1jer7FJAyPJAcqPgcyXz+K m7ismylrMj5nei/N+0dAnOkI4IQsxKxpCR+4sCJL8ZsvjKH+cjWkyEIUNx792Knd4o pexECYPS257XJB1MVw2Ap+Gy8sRbtHZcS1bEKsM53bvOunjeWrbXhFrkujNTHpbJvf xYmktTxyDsY3Xf8ksJsXwCJLctJVDyiBlIxrYFtwmhVTNGCQjjV48nSqfa48VYMACB x6miogJY9AHgw== Date: Fri, 28 Apr 2023 20:19:05 +0100 Message-Id: <20230428191905.13860-6-paren@disroot.org> In-Reply-To: <20230428191905.13860-1-paren@disroot.org> References: <20230428191905.13860-1-paren@disroot.org> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: "\(" X-ACL-Warn: , "\( via Guix-patches" X-Patchwork-Original-From: "\( via Guix-patches" via From: "\\(" Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * guix/records.scm (match-record-lambda): New syntax. * tests/records.scm ("match-record-lambda"): New test. --- .dir-locals.el | 1 + guix/records.scm | 15 ++++++++++++++- tests/records.scm | 12 ++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/.dir-locals.el b/.dir-locals.el index 3ffd25ee94..d79b5c9d7e 100644 --- a/.dir-locals.el +++ b/.dir-locals.el @@ -71,6 +71,7 @@ (eval . (put 'lambda* 'scheme-indent-function 1)) (eval . (put 'substitute* 'scheme-indent-function 1)) (eval . (put 'match-record 'scheme-indent-function 3)) + (eval . (put 'match-record-lambda 'scheme-indent-function 2)) ;; TODO: Contribute these to Emacs' scheme-mode. (eval . (put 'let-keywords 'scheme-indent-function 3)) diff --git a/guix/records.scm b/guix/records.scm index 041eb2f297..504a023e87 100644 --- a/guix/records.scm +++ b/guix/records.scm @@ -31,7 +31,8 @@ (define-module (guix records) alist->record object->fields recutils->alist - match-record)) + match-record + match-record-lambda)) ;;; Commentary: ;;; @@ -642,4 +643,16 @@ (define-syntax match-record (match-record-inner #,s record type (fields ...) body ...) (throw 'wrong-type-arg record)))))) +(define-syntax match-record-lambda + (lambda (s) + "Return a procedure accepting a single record of the given TYPE for which each +FIELD will be bound to its FIELD name within the returned procedure. A syntax error +is raised if an unknown field is queried." + (syntax-case s () + ((_ type (field ...) body ...) + #`(lambda (record) + (if (eq? (struct-vtable record) type) + (match-record-inner #,s record type (field ...) body ...) + (throw 'wrong-type-arg record))))))) + ;;; records.scm ends here diff --git a/tests/records.scm b/tests/records.scm index 4f0aeb3903..8ee306bddc 100644 --- a/tests/records.scm +++ b/tests/records.scm @@ -590,4 +590,16 @@ (define-record-type* with-thunked make-with-thunked (match-record rec (normal thunked) (list normal thunked))))) +(test-equal "match-record-lambda" + '("thing: foo" "thing: bar") + (begin + (define-record-type* with-text make-with-text + with-text? + (text with-text-text)) + + (map (match-record-lambda (text) + (string-append "thing: " text)) + (list (with-text (text "foo")) + (with-text (text "bar")))))) + (test-end)