Subject: bug#44073: cperl-mode: Cleanup - Delete a misleading comment [PATCH]

From: Harald Jörg <[email-masked]>
Date: Mon, 19 Oct 2020 01:55:48 +0200

Severity: minor

With this patch, cperl-mode.el no longer informs the user about "Font
lock bugs" which have been fixed - or worked around - long ago: Some of
Perl's punctuation variables contain string delimiters ($`, $' and $")
but they don't start strings.  Fontification handles this correctly.

The patch eliminates the comment, and it comes with a test to verify
that cperl-mode gets it right in all cases which were listed in the
comment, plus some more.

>From d5cc24d57c2653f8c21c7bfe0bb75ff4b2df7ada Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Harald=20J=C3=B6rg?= <[email-masked]>
Date: Mon, 19 Oct 2020 01:34:20 +0200
Subject: [PATCH] cperl-mode: Delete a misleading comment, add tests for
 verification

* test/lisp/progmodes/cperl-mode-tests.el
(cperl-mode-fontify-punct-vars): Add regression tests to verify
that fontification of punctuation variables doesn't start strings.

* lisp/progmodes/cperl-mode.el: Delete a comment which explains a
bug which has been fixed a long time ago.
---
 lisp/progmodes/cperl-mode.el                  |  7 ------
 .../fontify-punctuation-vars.pl               | 20 +++++++++++++++
 test/lisp/progmodes/cperl-mode-tests.el       | 25 +++++++++++++++++++
 3 files changed, 45 insertions(+), 7 deletions(-)
 create mode 100644 test/lisp/progmodes/cperl-mode-resources/fontify-punctuation-vars.pl

diff --git a/lisp/progmodes/cperl-mode.el b/lisp/progmodes/cperl-mode.el
index 5b6e50c820..ebbea6bed9 100644
--- a/lisp/progmodes/cperl-mode.el
+++ b/lisp/progmodes/cperl-mode.el
@@ -71,13 +71,6 @@
 
 ;; (define-key global-map [M-S-down-mouse-3] 'imenu)
 
-;;;; Font lock bugs as of v4.32:
-
-;; The following kinds of Perl code erroneously start strings:
-;; \$`  \$'  \$"
-;; $opt::s  $opt_s  $opt{s}  (s => ...)  /\s+.../
-;; likewise with m, tr, y, q, qX instead of s
-
 ;;; Code:
 
 ;;; Compatibility with older versions (for publishing on ELPA)
diff --git a/test/lisp/progmodes/cperl-mode-resources/fontify-punctuation-vars.pl b/test/lisp/progmodes/cperl-mode-resources/fontify-punctuation-vars.pl
new file mode 100644
index 0000000000..fa328438cb
--- /dev/null
+++ b/test/lisp/progmodes/cperl-mode-resources/fontify-punctuation-vars.pl
@@ -0,0 +1,20 @@
+# The following Perl punctiation variables contain characters which
+# are classified as string delimiters in the syntax table.  The mode
+# should not be confused by these.
+# The corresponding tests check that two consecutive '#' characters
+# are seen as comments, not as strings.
+my $pre = $`;  ##  $PREMATCH,      use another ` # to balance out
+my $pos = $';  ##  $POSTMATCH,     use another ' # to balance out
+my $lsp = $";  ##  $LIST_SEPARATOR use another " # to balance out
+
+# In the second level, we use the reference constructor \ on these
+# variables.  The backslash is an escape character *only* in strings.
+my $ref = \$`; ## \$PREMATCH,      use another ` # to balance out
+my $rif = \$'; ## \$POSTMATCH,     use another ' # to balance out
+my $raf = \$"; ## \$LIST_SEPARATOR use another " # to balance out
+
+my $opt::s = 0;       ## s is no substitution here
+my $opt_s  = 0;       ## s is no substitution here
+my %opt = (s => 0);   ## s is no substitution here
+$opt{s} = 0;          ## s is no substitution here
+$opt_s =~ /\s+.../    ## s is no substitution here
diff --git a/test/lisp/progmodes/cperl-mode-tests.el b/test/lisp/progmodes/cperl-mode-tests.el
index e2af2b5b8d..2a3a759093 100644
--- a/test/lisp/progmodes/cperl-mode-tests.el
+++ b/test/lisp/progmodes/cperl-mode-tests.el
@@ -196,4 +196,29 @@ cperl-mode-test-indent-styles
             (should (equal got expected)))))
       (cperl-set-style "CPerl"))))
 
+(ert-deftest cperl-mode-fontify-punct-vars ()
+  "Test fontification of Perl's punctiation variables.
+Perl has variable names containing unbalanced quotes for the list
+separator $\" and pre- and postmatch $` and $'.  A reference to
+these variables, for example \\$\", should not cause the dollar
+to be escaped, which would then start a string beginning with the
+quote character.  This used to be broken in cperl-mode at some
+point in the distant past, and is still broken in perl-mode. "
+  (skip-unless (eq cperl-test-mode #'cperl-mode))
+  (let ((file (ert-resource-file "fontify-punctuation-vars.pl")))
+    (with-temp-buffer
+      (insert-file-contents file)
+      (goto-char (point-min))
+      (funcall cperl-test-mode)
+      (while (search-forward "##" nil t)
+        ;; The third element of syntax-ppss is true if in a string,
+        ;; which would indicate bad interpretation of the quote.  The
+        ;; fourth element is true if in a comment, which should be the
+        ;; case.
+        (should (equal (nth 3 (syntax-ppss)) nil))
+        (should (equal (nth 4 (syntax-ppss)) t))
+      )
+    )))
+
+
 ;;; cperl-mode-tests.el ends here
-- 
2.20.1