From e916fc9215e66b15c7e2387cc087a9d1cc57bf77 Mon Sep 17 00:00:00 2001
From: Alan Zimmerman <alan.zimm@gmail.com>
Date: Mon, 22 Apr 2024 20:26:57 +0100
Subject: [PATCH] EPA: Keep comments in a CaseAlt match

The comments now live in the surrounding location, not inside the
Match. Make sure we keep them.

Closes #24707
---
 compiler/GHC/Parser.y                      | 2 +-
 testsuite/tests/printer/CaseAltComments.hs | 7 +++++++
 testsuite/tests/printer/Makefile           | 5 +++++
 testsuite/tests/printer/all.T              | 1 +
 4 files changed, 14 insertions(+), 1 deletion(-)
 create mode 100644 testsuite/tests/printer/CaseAltComments.hs

diff --git a/compiler/GHC/Parser.y b/compiler/GHC/Parser.y
index 2e036d021b88..492e04456f4f 100644
--- a/compiler/GHC/Parser.y
+++ b/compiler/GHC/Parser.y
@@ -3342,7 +3342,7 @@ alts1(PATS) :: { forall b. DisambECP b => PV (Located ([AddEpAnn],[LMatch GhcPs
 
 alt(PATS) :: { forall b. DisambECP b => PV (LMatch GhcPs (LocatedA b)) }
         : PATS alt_rhs { $2 >>= \ $2 ->
-                         acsA (sLLAsl $1 $> ()) (\loc cs -> L (locA loc)
+                         amsA' (sLLAsl $1 $>
                                          (Match { m_ext = []
                                                 , m_ctxt = CaseAlt -- for \case and \cases, this will be changed during post-processing
                                                 , m_pats = $1
diff --git a/testsuite/tests/printer/CaseAltComments.hs b/testsuite/tests/printer/CaseAltComments.hs
new file mode 100644
index 000000000000..031701b8aa5d
--- /dev/null
+++ b/testsuite/tests/printer/CaseAltComments.hs
@@ -0,0 +1,7 @@
+{-# LANGUAGE PatternGuards #-}
+module CaseAltComments where
+
+nfCom = case expr of
+      x :*: y  -- comment
+         | x' <= y'  -> x' :*: y'
+      _ -> blah
diff --git a/testsuite/tests/printer/Makefile b/testsuite/tests/printer/Makefile
index c8e7cde7c589..c1172cf6849f 100644
--- a/testsuite/tests/printer/Makefile
+++ b/testsuite/tests/printer/Makefile
@@ -826,3 +826,8 @@ Test24533:
 PprLetIn:
 	$(CHECK_PPR)   $(LIBDIR) PprLetIn.hs
 	$(CHECK_EXACT) $(LIBDIR) PprLetIn.hs
+
+.PHONY: CaseAltComments
+CaseAltComments:
+	$(CHECK_PPR)   $(LIBDIR) CaseAltComments.hs
+	$(CHECK_EXACT) $(LIBDIR) CaseAltComments.hs
diff --git a/testsuite/tests/printer/all.T b/testsuite/tests/printer/all.T
index 19b070c97100..b68510122400 100644
--- a/testsuite/tests/printer/all.T
+++ b/testsuite/tests/printer/all.T
@@ -198,3 +198,4 @@ test('ListTuplePuns', extra_files(['ListTuplePuns.hs']), ghci_script, ['ListTupl
 test('AnnotationNoListTuplePuns', [ignore_stderr, req_ppr_deps], makefile_test, ['AnnotationNoListTuplePuns'])
 test('Test24533', [ignore_stderr, req_ppr_deps], makefile_test, ['Test24533'])
 test('PprLetIn', [ignore_stderr, req_ppr_deps], makefile_test, ['PprLetIn'])
+test('CaseAltComments', [ignore_stderr, req_ppr_deps], makefile_test, ['CaseAltComments'])
-- 
GitLab