From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Helmut Eller Newsgroups: gmane.emacs.bugs Subject: bug#65030: 30.0.50; Check keyword args of make-process Date: Thu, 03 Aug 2023 08:47:44 +0200 Message-ID: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="5599"; mail-complaints-to="usenet@ciao.gmane.io" To: 65030@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Aug 03 08:49:15 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qRS95-0001Ja-1g for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 03 Aug 2023 08:49:15 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qRS8x-0006lU-HV; Thu, 03 Aug 2023 02:49:07 -0400 Original-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 1qRS8s-0006l5-I7 for bug-gnu-emacs@gnu.org; Thu, 03 Aug 2023 02:49:03 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qRS8s-0000XC-9h for bug-gnu-emacs@gnu.org; Thu, 03 Aug 2023 02:49:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qRS8s-0002TV-5F for bug-gnu-emacs@gnu.org; Thu, 03 Aug 2023 02:49:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Helmut Eller Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 03 Aug 2023 06:49:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 65030 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16910452849279 (code B ref -1); Thu, 03 Aug 2023 06:49:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 3 Aug 2023 06:48:04 +0000 Original-Received: from localhost ([127.0.0.1]:50571 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qRS7w-0002PW-2F for submit@debbugs.gnu.org; Thu, 03 Aug 2023 02:48:04 -0400 Original-Received: from lists.gnu.org ([2001:470:142::17]:46004) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qRS7t-0002OO-Oh for submit@debbugs.gnu.org; Thu, 03 Aug 2023 02:48:02 -0400 Original-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 1qRS7o-0005nJ-9C for bug-gnu-emacs@gnu.org; Thu, 03 Aug 2023 02:47:56 -0400 Original-Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qRS7m-0008Vk-AA for bug-gnu-emacs@gnu.org; Thu, 03 Aug 2023 02:47:56 -0400 Original-Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-3fbea14706eso6042885e9.2 for ; Wed, 02 Aug 2023 23:47:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691045272; x=1691650072; h=mime-version:message-id:date:subject:to:from:from:to:cc:subject :date:message-id:reply-to; bh=RPZooucziZ7IiTKWGQk05I3lCxyJLSy7z3vJbSVD3fc=; b=DSlea9p4sLy2AnU+KHr/8s/ijPZ5Yepy4uhhw970j0DU1ba13raxJZM2TnnzXkVs6w Klm3R/er0weKfbqRJBVvaCrtrvyrIY/GwpN9yyMQB+lZVCKNLDiOtHLanQymUGUxChIB ZrSztLQpP64yjLRGQa+xiikYZWu1aYudvV2wBygD+A/G9mer5n/T7vWXq33hTe6GP6ez /bmxy/u4HTz+qGQsp5Cf+hgb0l21rW5D1khphFmeDHZorP7A2ZDQpo4HQRi3e8B3ePXZ GTk7HksNmJ8nJNkVg2VVOljejH0UAuIuKEtlki/Q5yjGu12UY5hrfstDpd9/KAU+klTv +Qig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691045272; x=1691650072; h=mime-version:message-id:date:subject:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=RPZooucziZ7IiTKWGQk05I3lCxyJLSy7z3vJbSVD3fc=; b=lODxsc9u57y6BoLY1khH7IBCrZL/DG4sHCGJSFAgpTZIl1/JTW4qt7LmAkXzcSrL8N I+XjV8TQmVV42bAbXcYeV1ZqToPkqvyi2pegZ1++27Q55HfPB17iu6IMau2vka0kHK8m uKo6T6zh2cNrQ3KQOgQ1UsteZh5VNTRJ0zqCKfWhhIBUUi2xtqk8r9EeuVqsFZKXUkDJ 3MJk8yXGN18IcjPCoNH3Xdk546Rqpc+0DNYg1KmOUwcBZVBFCkmegJMPWdx7mNrBMY7g D1wnSprB+7bJsQ87HZYGrKwhdKq1AwdjWxAMUW49Piukra5BQzb0SBGXFLqhcHRTyiSa 2GeA== X-Gm-Message-State: ABy/qLY1vabZVg89fFo6yVK1FJ6PfA41zUc7qtDREPRyUSUBEhpCmCrA aTV9+RvAeKCsVWjk5zxZYEpwXR2c53c= X-Google-Smtp-Source: APBJJlHPagROq888IrzHCRbqCMzB8gbLnGRr9/Hzt3jDGn6yyhdl+DXDTvAjGRZfVtYm+criejG6PA== X-Received: by 2002:a1c:ed0f:0:b0:3fe:1c05:3c8f with SMTP id l15-20020a1ced0f000000b003fe1c053c8fmr6355540wmh.35.1691045271646; Wed, 02 Aug 2023 23:47:51 -0700 (PDT) Original-Received: from caladan (dialin-228086.xdsl.raiffeisen.net. [195.254.228.86]) by smtp.gmail.com with ESMTPSA id l7-20020a7bc347000000b003fbdbd0a7desm3420560wmj.27.2023.08.02.23.47.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 23:47:50 -0700 (PDT) Received-SPF: pass client-ip=2a00:1450:4864:20::333; envelope-from=eller.helmut@gmail.com; helo=mail-wm1-x333.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:266551 Archived-At: --=-=-= Content-Type: text/plain The functions make-process and make-network-process have many keyword arguments and it's somewhat easy to misspell some of them. E.g. using :coding-system instead of :coding. These functions don't detect such mistakes at runtime. What would people think about adding some checks as a compiler macro as with the patch below? I didn't know where to put this, so I just left it in bytecomp.el. Perhaps the advertised-calling-convention declaration could do this, but since keyword arguments seem to be generally discouraged, a special case for make-process and make-network-process maybe simpler. Helmut --=-=-= Content-Type: text/x-diff; charset=utf-8 Content-Disposition: inline; filename=0001-Check-keyword-args-of-make-process.patch Content-Transfer-Encoding: quoted-printable >From 758ba9b8b26333fc60ca4693616c5f2b355d4fcc Mon Sep 17 00:00:00 2001 From: Helmut Eller Date: Thu, 3 Aug 2023 08:33:40 +0200 Subject: [PATCH] Check keyword args of make-process The functions make-process and make-network-process have many keyword args and it's easy to misspell some of them. Use a compiler macro to warn about some possible mistakes. * lisp/emacs-lisp/bytecomp.el (bytecomp--check-keyword-args): New helper. (make-process, make-network-process): Define a compiler macro that performs some checks but doesn't anything else. * test/lisp/emacs-lisp/bytecomp-tests.el: Add some tests. * test/lisp/emacs-lisp/bytecomp-resources/: (warn-make-process-missing-keyword-arg.el, warn-make-process-missing-keyword-value.el, warn-make-process-repeated-keyword-arg.el, warn-make-process-unknown-keyword-arg.el): New test files --- lisp/emacs-lisp/bytecomp.el | 61 +++++++++++++++++++ .../warn-make-process-missing-keyword-arg.el | 3 + ...warn-make-process-missing-keyword-value.el | 3 + .../warn-make-process-repeated-keyword-arg.el | 3 + .../warn-make-process-unknown-keyword-arg.el | 4 ++ test/lisp/emacs-lisp/bytecomp-tests.el | 16 +++++ 6 files changed, 90 insertions(+) create mode 100644 test/lisp/emacs-lisp/bytecomp-resources/warn-make-proce= ss-missing-keyword-arg.el create mode 100644 test/lisp/emacs-lisp/bytecomp-resources/warn-make-proce= ss-missing-keyword-value.el create mode 100644 test/lisp/emacs-lisp/bytecomp-resources/warn-make-proce= ss-repeated-keyword-arg.el create mode 100644 test/lisp/emacs-lisp/bytecomp-resources/warn-make-proce= ss-unknown-keyword-arg.el diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el index 5b1d958e6c2..d3a434b5593 100644 --- a/lisp/emacs-lisp/bytecomp.el +++ b/lisp/emacs-lisp/bytecomp.el @@ -5782,6 +5782,67 @@ bytecomp--backward-word form ; arity error `(forward-word (- (or ,arg 1))))) =20 +(defun bytecomp--check-keyword-args (form arglist allowed-keys required-ke= ys) + (let ((fun (car form))) + (cl-flet ((missing (form keyword) + (byte-compile-warn-x + form + "`%S=C2=B4 called without required keyword argument %S" + fun keyword)) + (unrecognized (form keyword) + (byte-compile-warn-x + form + "`%S=C2=B4 called with unknown keyword argument %S" + fun keyword)) + (duplicate (form keyword) + (byte-compile-warn-x + form + "`%S=C2=B4 called with repeated keyword argument %S" + fun keyword)) + (missing-val (form keyword) + (byte-compile-warn-x + form + "missing value for keyword argument %S" + keyword))) + (let* ((seen '()) + (l arglist)) + (while (consp l) + (let ((key (car l))) + (cond ((and (keywordp key) (memq key allowed-keys)) + (cond ((memq key seen) + (duplicate l key)) + (t + (push key seen)))) + (t (unrecognized l key))) + (when (null (cdr l)) + (missing-val l key))) + (setq l (cddr l))) + (dolist (key required-keys) + (unless (memq key seen) + (missing form key)))))) + form) + +(put 'make-process 'compiler-macro + #'(lambda (form &rest args) + (bytecomp--check-keyword-args + form args + '(:name + :buffer :command :coding :noquery :stop :connection-type + :filter :sentinel :stderr :file-handler) + '(:name :command)))) + +(put 'make-network-process 'compiler-macro + #'(lambda (form &rest args) + (bytecomp--check-keyword-args + form args + '(:name + :buffer :host :service :type :family :local :remote :coding + :nowait :noquery :stop :filter :filter-multibyte :sentinel + :log :plist :tls-parameters :server :broadcast :dontroute + :keepalive :linger :oobinline :priority :reuseaddr :bindtodevi= ce + :use-external-socket) + '(:name :service)))) + (provide 'byte-compile) (provide 'bytecomp) =20 diff --git a/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-miss= ing-keyword-arg.el b/test/lisp/emacs-lisp/bytecomp-resources/warn-make-proc= ess-missing-keyword-arg.el new file mode 100644 index 00000000000..9369e78ff54 --- /dev/null +++ b/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-missing-key= word-arg.el @@ -0,0 +1,3 @@ +;;; -*- lexical-binding: t -*- +(defun foo () + (make-process :name "ls")) diff --git a/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-miss= ing-keyword-value.el b/test/lisp/emacs-lisp/bytecomp-resources/warn-make-pr= ocess-missing-keyword-value.el new file mode 100644 index 00000000000..4226349afef --- /dev/null +++ b/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-missing-key= word-value.el @@ -0,0 +1,3 @@ +;;; -*- lexical-binding: t -*- +(defun foo () + (make-process :name "ls" :command)) diff --git a/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-repe= ated-keyword-arg.el b/test/lisp/emacs-lisp/bytecomp-resources/warn-make-pro= cess-repeated-keyword-arg.el new file mode 100644 index 00000000000..18250f14ee9 --- /dev/null +++ b/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-repeated-ke= yword-arg.el @@ -0,0 +1,3 @@ +;;; -*- lexical-binding: t -*- +(defun foo () + (make-process :name "ls" :command "ls" :name "ls")) diff --git a/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-unkn= own-keyword-arg.el b/test/lisp/emacs-lisp/bytecomp-resources/warn-make-proc= ess-unknown-keyword-arg.el new file mode 100644 index 00000000000..4721035780b --- /dev/null +++ b/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-unknown-key= word-arg.el @@ -0,0 +1,4 @@ +;;; -*- lexical-binding: t -*- +(defun foo () + (make-process :name "ls" :command "ls" + :coding-system 'binary)) diff --git a/test/lisp/emacs-lisp/bytecomp-tests.el b/test/lisp/emacs-lisp/= bytecomp-tests.el index 593fd117685..6ed907ead7b 100644 --- a/test/lisp/emacs-lisp/bytecomp-tests.el +++ b/test/lisp/emacs-lisp/bytecomp-tests.el @@ -1199,6 +1199,22 @@ bytecomp--tests-obsolete-var "nowarn-inline-after-defvar.el" "Lexical argument shadows" 'reverse) =20 +(bytecomp--define-warning-file-test + "warn-make-process-missing-keyword-arg.el" + "called without required keyword argument :command") + +(bytecomp--define-warning-file-test + "warn-make-process-unknown-keyword-arg.el" + "called with unknown keyword argument :coding-system") + +(bytecomp--define-warning-file-test + "warn-make-process-repeated-keyword-arg.el" + "called with repeated keyword argument :name") + +(bytecomp--define-warning-file-test + "warn-make-process-missing-keyword-value.el" + "missing value for keyword argument :command") + ;;;; Macro expansion. =20 --=20 2.39.2 --=-=-=--