[Git][ghc/ghc][wip/T22524] Adjust error message for trailing whitespace in as-pattern.

Jade (@Jade) gitlab at gitlab.haskell.org
Sat Feb 3 20:17:59 UTC 2024



Jade pushed to branch wip/T22524 at Glasgow Haskell Compiler / GHC


Commits:
628c5bb7 by Jade at 2024-02-03T21:21:46+01:00
Adjust error message for trailing whitespace in as-pattern.

Fixes #22524

- - - - -


5 changed files:

- compiler/GHC/Parser/Errors/Ppr.hs
- compiler/GHC/Parser/Lexer.x
- + testsuite/tests/parser/should_fail/SuffixAtFail.hs
- + testsuite/tests/parser/should_fail/SuffixAtFail.stderr
- testsuite/tests/parser/should_fail/all.T


Changes:

=====================================
compiler/GHC/Parser/Errors/Ppr.hs
=====================================
@@ -386,7 +386,8 @@ instance Diagnostic PsMessage where
       -> mkSimpleDecorated $ text "primitive string literal must contain only characters <= \'\\xFF\'"
     PsErrSuffixAT
       -> mkSimpleDecorated $
-           text "Suffix occurrence of @. For an as-pattern, remove the leading whitespace."
+           text "The symbol '@' occurs as a suffix." $$
+           text "For an as-pattern, there must not be any whitespace surrounding '@'."
     PsErrPrecedenceOutOfRange i
       -> mkSimpleDecorated $ text "Precedence out of range: " <> int i
     PsErrSemiColonsInCondExpr c st t se e


=====================================
compiler/GHC/Parser/Lexer.x
=====================================
@@ -1784,6 +1784,21 @@ qvarsym, qconsym :: StringBuffer -> Int -> Token
 qvarsym buf len = ITqvarsym $! splitQualName buf len False
 qconsym buf len = ITqconsym $! splitQualName buf len False
 
+
+errSuffixAt :: PsSpan -> P a
+errSuffixAt span = do
+    input <- getInput
+    failLocMsgP start (go input start) (\srcSpan -> mkPlainErrorMsgEnvelope srcSpan $ PsErrSuffixAT)
+  where
+    start = psRealLoc (psSpanStart span)
+    go inp loc
+      | Just (c, i) <- alexGetChar inp
+      , let next = advanceSrcLoc loc c =
+          if c == ' '
+          then go i next
+          else next
+      | otherwise = loc
+
 -- See Note [Whitespace-sensitive operator parsing]
 varsym :: OpWs -> Action
 varsym opws at OpWsPrefix = sym $ \span exts s ->
@@ -1817,7 +1832,7 @@ varsym opws at OpWsPrefix = sym $ \span exts s ->
          do { warnOperatorWhitespace opws span s
             ; return (ITvarsym s) }
 varsym opws at OpWsSuffix = sym $ \span _ s ->
-  if | s == fsLit "@" -> failMsgP (\srcLoc -> mkPlainErrorMsgEnvelope srcLoc $ PsErrSuffixAT)
+  if | s == fsLit "@" -> errSuffixAt span
      | s == fsLit "." -> return ITdot
      | otherwise ->
          do { warnOperatorWhitespace opws span s


=====================================
testsuite/tests/parser/should_fail/SuffixAtFail.hs
=====================================
@@ -0,0 +1,3 @@
+module Main where
+
+foo x@    () = ()


=====================================
testsuite/tests/parser/should_fail/SuffixAtFail.stderr
=====================================
@@ -0,0 +1,7 @@
+
+SuffixAtFail.hs:3:6: error: [GHC-33856]
+    The symbol '@' occurs as a suffix.
+    For an as-pattern, there must not be any whitespace surrounding '@'.
+  |
+3 | foo x@    () = ()
+  |      ^^^^^


=====================================
testsuite/tests/parser/should_fail/all.T
=====================================
@@ -219,4 +219,5 @@ test('T20609', normal, compile_fail, [''])
 test('T20609a', normal, compile_fail, [''])
 test('T20609b', normal, compile_fail, [''])
 test('T20609c', normal, compile_fail, [''])
-test('T20609d', normal, compile_fail, [''])
\ No newline at end of file
+test('T20609d', normal, compile_fail, [''])
+test('SuffixAtFail', normal, compile_fail, ['-fdiagnostics-show-caret'])



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/628c5bb7cc616c95be9ffcd1698b73e9f8752f68

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/628c5bb7cc616c95be9ffcd1698b73e9f8752f68
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20240203/5bb795e1/attachment-0001.html>


More information about the ghc-commits mailing list