summaryrefslogtreecommitdiff
path: root/compiler/GHC/Parser.y
diff options
context:
space:
mode:
authorAlan Zimmerman <alan.zimm@gmail.com>2021-08-22 21:14:46 +0100
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-09-06 22:57:55 -0400
commit407d3b3a3a219dab56d0a36598c637aa45da61e1 (patch)
tree4e03381c68302e2a995c573b4b57a4250f12dbf5 /compiler/GHC/Parser.y
parent5b41353355022c1247e0516d541b7f7fb49f0e29 (diff)
downloadhaskell-407d3b3a3a219dab56d0a36598c637aa45da61e1.tar.gz
EPA: order of semicolons and comments for top-level decls is wrong
A comment followed by a semicolon at the top level resulted in the preceding comments being attached to the following declaration. Capture the comments as belonging to the declaration preceding the semicolon instead. Closes #20258
Diffstat (limited to 'compiler/GHC/Parser.y')
-rw-r--r--compiler/GHC/Parser.y17
1 files changed, 11 insertions, 6 deletions
diff --git a/compiler/GHC/Parser.y b/compiler/GHC/Parser.y
index 1c0c65bb96..732a03f7d5 100644
--- a/compiler/GHC/Parser.y
+++ b/compiler/GHC/Parser.y
@@ -1066,9 +1066,9 @@ qcname :: { LocatedN RdrName } -- Variable or type constructor
-- top handles the fact that these may be optional.
-- One or more semicolons
-semis1 :: { [TrailingAnn] }
-semis1 : semis1 ';' { if isZeroWidthSpan (gl $2) then $1 else (AddSemiAnn (glAA $2) : $1) }
- | ';' { msemi $1 }
+semis1 :: { Located [TrailingAnn] }
+semis1 : semis1 ';' { sLL $1 $> $ if isZeroWidthSpan (gl $2) then (unLoc $1) else (AddSemiAnn (glAA $2) : (unLoc $1)) }
+ | ';' { sL1 $1 $ msemi $1 }
-- Zero or more semicolons
semis :: { [TrailingAnn] }
@@ -1085,7 +1085,7 @@ importdecls
importdecls_semi :: { [LImportDecl GhcPs] }
importdecls_semi
: importdecls_semi importdecl semis1
- {% do { i <- amsA $2 $3
+ {% do { i <- amsAl $2 (comb2 (reLoc $2) $3) (unLoc $3)
; return (i : $1)} }
| {- empty -} { [] }
@@ -1187,7 +1187,7 @@ topdecls :: { OrdList (LHsDecl GhcPs) }
-- May have trailing semicolons, can be empty
topdecls_semi :: { OrdList (LHsDecl GhcPs) }
- : topdecls_semi topdecl semis1 {% do { t <- amsA $2 $3
+ : topdecls_semi topdecl semis1 {% do { t <- amsAl $2 (comb2 (reLoc $2) $3) (unLoc $3)
; return ($1 `snocOL` t) }}
| {- empty -} { nilOL }
@@ -1200,7 +1200,7 @@ topdecls_cs :: { OrdList (LHsDecl GhcPs) }
-- May have trailing semicolons, can be empty
topdecls_cs_semi :: { OrdList (LHsDecl GhcPs) }
- : topdecls_cs_semi topdecl_cs semis1 {% do { t <- amsA $2 $3
+ : topdecls_cs_semi topdecl_cs semis1 {% do { t <- amsAl $2 (comb2 (reLoc $2) $3) (unLoc $3)
; return ($1 `snocOL` t) }}
| {- empty -} { nilOL }
@@ -4254,6 +4254,11 @@ amsA (L l a) bs = do
cs <- getCommentsFor (locA l)
return (L (addAnnsA l bs cs) a)
+amsAl :: MonadP m => LocatedA a -> SrcSpan -> [TrailingAnn] -> m (LocatedA a)
+amsAl (L l a) loc bs = do
+ cs <- getCommentsFor loc
+ return (L (addAnnsA l bs cs) a)
+
amsrc :: MonadP m => Located a -> AnnContext -> m (LocatedC a)
amsrc a@(L l _) bs = do
cs <- getCommentsFor l