[Git][ghc/ghc][master] EPA: Do not collect comments from end of file

Marge Bot (@marge-bot) gitlab at gitlab.haskell.org
Thu Jan 5 15:49:29 UTC 2023



Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC


Commits:
22bb8998 by Alan Zimmerman at 2023-01-05T10:49:09-05:00
EPA: Do not collect comments from end of file

In Parser.y semis1 production triggers for the virtual semi at the end
of the file. This is detected by it being zero length.

In this case, do not extend the span being used to gather comments, so
any final comments are allocated at the module level instead.

- - - - -


9 changed files:

- compiler/GHC/Parser.y
- testsuite/tests/ghc-api/exactprint/RmDecl4.expected.hs
- testsuite/tests/ghc-api/exactprint/Test20239.stderr
- + testsuite/tests/ghc-api/exactprint/ZeroWidthSemi.hs
- + testsuite/tests/ghc-api/exactprint/ZeroWidthSemi.stderr
- testsuite/tests/ghc-api/exactprint/all.T
- testsuite/tests/parser/should_compile/DumpParsedAstComments.stderr
- testsuite/tests/parser/should_compile/T20718.stderr
- testsuite/tests/printer/Test20297.stdout


Changes:

=====================================
compiler/GHC/Parser.y
=====================================
@@ -1076,8 +1076,10 @@ qcname  :: { LocatedN RdrName }  -- Variable or type constructor
 
 -- One or more semicolons
 semis1  :: { Located [TrailingAnn] }
-semis1  : semis1 ';'  { sLL $1 $> $ if isZeroWidthSpan (gl $2) then (unLoc $1) else (AddSemiAnn (glAA $2) : (unLoc $1)) }
-        | ';'         { sL1 $1 $ msemi $1 }
+semis1  : semis1 ';'  { if isZeroWidthSpan (gl $2) then (sL1 $1 $ unLoc $1) else (sLL $1 $> $ AddSemiAnn (glAA $2) : (unLoc $1)) }
+        | ';'         { case msemi $1 of
+                          [] -> noLoc []
+                          ms -> sL1 $1 $ ms }
 
 -- Zero or more semicolons
 semis   :: { [TrailingAnn] }


=====================================
testsuite/tests/ghc-api/exactprint/RmDecl4.expected.hs
=====================================
@@ -7,5 +7,4 @@ ff y = y + zz + xx
 
 zz = 1
 
-
 -- EOF


=====================================
testsuite/tests/ghc-api/exactprint/Test20239.stderr
=====================================
@@ -24,7 +24,14 @@
        { Test20239.hs:7:34-63 })))
     (EpaCommentsBalanced
      []
-     []))
+     [(L
+       (Anchor
+        { Test20239.hs:7:34-63 }
+        (UnchangedAnchor))
+       (EpaComment
+        (EpaLineComment
+         "-- ^ Run any arbitrary IO code")
+        { Test20239.hs:6:86 }))]))
    (VirtualBraces
     (1))
    (Nothing)
@@ -50,15 +57,7 @@
                     (EpaComment
                      (EpaLineComment
                       "-- | Leading Haddock Comment")
-                     { Test20239.hs:1:18-22 }))
-                  ,(L
-                    (Anchor
-                     { Test20239.hs:7:34-63 }
-                     (UnchangedAnchor))
-                    (EpaComment
-                     (EpaLineComment
-                      "-- ^ Run any arbitrary IO code")
-                     { Test20239.hs:6:86 }))])) { Test20239.hs:(4,1)-(6,86) })
+                     { Test20239.hs:1:18-22 }))])) { Test20239.hs:(4,1)-(6,86) })
     (InstD
      (NoExtField)
      (DataFamInstD


=====================================
testsuite/tests/ghc-api/exactprint/ZeroWidthSemi.hs
=====================================
@@ -0,0 +1,8 @@
+module ZeroWidthSemi where
+
+-- leading comments
+
+-- Function comment
+a = 0
+
+-- Trailing comment, should be in HsModule extension point


=====================================
testsuite/tests/ghc-api/exactprint/ZeroWidthSemi.stderr
=====================================
@@ -0,0 +1,134 @@
+
+==================== Parser AST ====================
+
+(L
+ { ZeroWidthSemi.hs:1:1 }
+ (HsModule
+  (XModulePs
+   (EpAnn
+    (Anchor
+     { ZeroWidthSemi.hs:1:1 }
+     (UnchangedAnchor))
+    (AnnsModule
+     [(AddEpAnn AnnModule (EpaSpan { ZeroWidthSemi.hs:1:1-6 }))
+     ,(AddEpAnn AnnWhere (EpaSpan { ZeroWidthSemi.hs:1:22-26 }))]
+     (AnnList
+      (Nothing)
+      (Nothing)
+      (Nothing)
+      []
+      [])
+     (Just
+      ((,)
+       { ZeroWidthSemi.hs:9:1 }
+       { ZeroWidthSemi.hs:8:1-58 })))
+    (EpaCommentsBalanced
+     [(L
+       (Anchor
+        { ZeroWidthSemi.hs:3:1-19 }
+        (UnchangedAnchor))
+       (EpaComment
+        (EpaLineComment
+         "-- leading comments")
+        { ZeroWidthSemi.hs:1:22-26 }))]
+     [(L
+       (Anchor
+        { ZeroWidthSemi.hs:8:1-58 }
+        (UnchangedAnchor))
+       (EpaComment
+        (EpaLineComment
+         "-- Trailing comment, should be in HsModule extension point")
+        { ZeroWidthSemi.hs:6:5 }))]))
+   (VirtualBraces
+    (1))
+   (Nothing)
+   (Nothing))
+  (Just
+   (L
+    (SrcSpanAnn (EpAnnNotUsed) { ZeroWidthSemi.hs:1:8-20 })
+    {ModuleName: ZeroWidthSemi}))
+  (Nothing)
+  []
+  [(L
+    (SrcSpanAnn (EpAnn
+                 (Anchor
+                  { ZeroWidthSemi.hs:6:1-5 }
+                  (UnchangedAnchor))
+                 (AnnListItem
+                  [])
+                 (EpaComments
+                  [(L
+                    (Anchor
+                     { ZeroWidthSemi.hs:5:1-19 }
+                     (UnchangedAnchor))
+                    (EpaComment
+                     (EpaLineComment
+                      "-- Function comment")
+                     { ZeroWidthSemi.hs:3:1-19 }))])) { ZeroWidthSemi.hs:6:1-5 })
+    (ValD
+     (NoExtField)
+     (FunBind
+      (NoExtField)
+      (L
+       (SrcSpanAnn (EpAnnNotUsed) { ZeroWidthSemi.hs:6:1 })
+       (Unqual
+        {OccName: a}))
+      (MG
+       (FromSource)
+       (L
+        (SrcSpanAnn (EpAnnNotUsed) { ZeroWidthSemi.hs:6:1-5 })
+        [(L
+          (SrcSpanAnn (EpAnnNotUsed) { ZeroWidthSemi.hs:6:1-5 })
+          (Match
+           (EpAnn
+            (Anchor
+             { ZeroWidthSemi.hs:6:1-5 }
+             (UnchangedAnchor))
+            []
+            (EpaComments
+             []))
+           (FunRhs
+            (L
+             (SrcSpanAnn (EpAnnNotUsed) { ZeroWidthSemi.hs:6:1 })
+             (Unqual
+              {OccName: a}))
+            (Prefix)
+            (NoSrcStrict))
+           []
+           (GRHSs
+            (EpaComments
+             [])
+            [(L
+              (SrcSpanAnn
+               (EpAnnNotUsed)
+               { ZeroWidthSemi.hs:6:3-5 })
+              (GRHS
+               (EpAnn
+                (Anchor
+                 { ZeroWidthSemi.hs:6:3-5 }
+                 (UnchangedAnchor))
+                (GrhsAnn
+                 (Nothing)
+                 (AddEpAnn AnnEqual (EpaSpan { ZeroWidthSemi.hs:6:3 })))
+                (EpaComments
+                 []))
+               []
+               (L
+                (SrcSpanAnn (EpAnnNotUsed) { ZeroWidthSemi.hs:6:5 })
+                (HsOverLit
+                 (EpAnn
+                  (Anchor
+                   { ZeroWidthSemi.hs:6:5 }
+                   (UnchangedAnchor))
+                  (NoEpAnns)
+                  (EpaComments
+                   []))
+                 (OverLit
+                  (NoExtField)
+                  (HsIntegral
+                   (IL
+                    (SourceText 0)
+                    (False)
+                    (0))))))))]
+            (EmptyLocalBinds
+             (NoExtField)))))])))))]))


=====================================
testsuite/tests/ghc-api/exactprint/all.T
=====================================
@@ -37,3 +37,4 @@ test('RmTypeSig2',    ignore_stderr, makefile_test, ['RmTypeSig2'])
 test('AddHiding1',    ignore_stderr, makefile_test, ['AddHiding1'])
 test('AddHiding2',    ignore_stderr, makefile_test, ['AddHiding2'])
 test('Test20239',  normal, compile_fail, ['-dsuppress-uniques -ddump-parsed-ast -dkeep-comments'])
+test('ZeroWidthSemi',  normal, compile, ['-dsuppress-uniques -ddump-parsed-ast -dkeep-comments'])


=====================================
testsuite/tests/parser/should_compile/DumpParsedAstComments.stderr
=====================================
@@ -82,15 +82,7 @@
                     (EpaComment
                      (EpaLineComment
                       "-- comment 2 for foo")
-                     { DumpParsedAstComments.hs:9:1-20 }))
-                  ,(L
-                    (Anchor
-                     { DumpParsedAstComments.hs:15:1-20 }
-                     (UnchangedAnchor))
-                    (EpaComment
-                     (EpaLineComment
-                      "-- | Haddock comment")
-                     { DumpParsedAstComments.hs:13:3
+                     { DumpParsedAstComments.hs:9:1-20
                         }))])) { DumpParsedAstComments.hs:(11,1)-(13,3) })
     (ValD
      (NoExtField)
@@ -219,7 +211,15 @@
                  (AnnListItem
                   [])
                  (EpaComments
-                  [])) { DumpParsedAstComments.hs:16:1-23 })
+                  [(L
+                    (Anchor
+                     { DumpParsedAstComments.hs:15:1-20 }
+                     (UnchangedAnchor))
+                    (EpaComment
+                     (EpaLineComment
+                      "-- | Haddock comment")
+                     { DumpParsedAstComments.hs:13:3
+                        }))])) { DumpParsedAstComments.hs:16:1-23 })
     (ValD
      (NoExtField)
      (FunBind


=====================================
testsuite/tests/parser/should_compile/T20718.stderr
=====================================
@@ -55,7 +55,22 @@
         (EpaLineComment
          "-- before 2")
         { T20718.hs:5:1-11 }))]
-     []))
+     [(L
+       (Anchor
+        { T20718.hs:10:1-8 }
+        (UnchangedAnchor))
+       (EpaComment
+        (EpaLineComment
+         "-- end 1")
+        { T20718.hs:8:5 }))
+     ,(L
+       (Anchor
+        { T20718.hs:11:1-8 }
+        (UnchangedAnchor))
+       (EpaComment
+        (EpaLineComment
+         "-- end 2")
+        { T20718.hs:10:1-8 }))]))
    (VirtualBraces
     (1))
    (Nothing)
@@ -74,22 +89,7 @@
                  (AnnListItem
                   [])
                  (EpaComments
-                  [(L
-                    (Anchor
-                     { T20718.hs:10:1-8 }
-                     (UnchangedAnchor))
-                    (EpaComment
-                     (EpaLineComment
-                      "-- end 1")
-                     { T20718.hs:8:5 }))
-                  ,(L
-                    (Anchor
-                     { T20718.hs:11:1-8 }
-                     (UnchangedAnchor))
-                    (EpaComment
-                     (EpaLineComment
-                      "-- end 2")
-                     { T20718.hs:10:1-8 }))])) { T20718.hs:8:1-5 })
+                  [])) { T20718.hs:8:1-5 })
     (ValD
      (NoExtField)
      (FunBind


=====================================
testsuite/tests/printer/Test20297.stdout
=====================================
@@ -50,14 +50,7 @@
                  (AnnListItem
                   [])
                  (EpaComments
-                  [(L
-                    (Anchor
-                     { Test20297.hs:7:9-19 }
-                     (UnchangedAnchor))
-                    (EpaComment
-                     (EpaLineComment
-                      "-- comment1")
-                     { Test20297.hs:7:3-7 }))])) { Test20297.hs:(5,1)-(7,7) })
+                  [])) { Test20297.hs:(5,1)-(7,7) })
     (ValD
      (NoExtField)
      (FunBind
@@ -150,7 +143,14 @@
                  (AnnListItem
                   [])
                  (EpaComments
-                  [])) { Test20297.hs:(9,1)-(11,26) })
+                  [(L
+                    (Anchor
+                     { Test20297.hs:7:9-19 }
+                     (UnchangedAnchor))
+                    (EpaComment
+                     (EpaLineComment
+                      "-- comment1")
+                     { Test20297.hs:7:3-7 }))])) { Test20297.hs:(9,1)-(11,26) })
     (ValD
      (NoExtField)
      (FunBind



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/22bb89989fc0a907ef6b8f6ae99aa8907f67e438

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/22bb89989fc0a907ef6b8f6ae99aa8907f67e438
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/20230105/1d3de2b3/attachment-0001.html>


More information about the ghc-commits mailing list