From ed37542627dc767dd7468f439c222bb29489703c Mon Sep 17 00:00:00 2001
From: sheaf <sam.derbyshire@gmail.com>
Date: Sat, 30 Jul 2022 13:14:35 +0200
Subject: [PATCH] Avoid redundant pattern warning in Resource.hsc

With GHC MR !8478, GHC is able to spot a redundant pattern match
when RLIM_SAVED_CUR == RLIM_SAVED_MAX, which it wasn't able to
detect before. So we use considerAccessible to avoid a pattern match
check. This unfortunately means we must change the SafeHaskell status
of that module to TrustWorth, as considerAccessible is from GHC.Exts,
which isn't safe.

Alternatives:

  - we can't perform the equality test RLIM_SAVED_CUR == RLIM_SAVED_MAX
    using CPP macros, because one of the values might expand out to
    have casts;
  - turning off pattern match warnings impacts warnings across the whole
    module, instead of the single affected function,
  - adding a dummy equation such as "id True" to the first pattern match
    would work, but seems more ad-hoc.
---
 System/Posix/Resource.hsc | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/System/Posix/Resource.hsc b/System/Posix/Resource.hsc
index a909a34..8618ba7 100644
--- a/System/Posix/Resource.hsc
+++ b/System/Posix/Resource.hsc
@@ -1,5 +1,6 @@
 {-# LANGUAGE CApiFFI #-}
-{-# LANGUAGE Safe #-}
+{-# LANGUAGE Trustworthy #-}
+
 -----------------------------------------------------------------------------
 -- |
 -- Module      :  System.Posix.Resource
@@ -31,6 +32,9 @@ import Foreign.C
 import System.IO.Error ( ioeSetLocation )
 import GHC.IO.Exception ( unsupportedOperation )
 #endif
+#if __GLASGOW_HASKELL__ >= 905
+import GHC.Exts ( considerAccessible )
+#endif
 
 -- -----------------------------------------------------------------------------
 -- Resource limits
@@ -115,12 +119,20 @@ unpackRLimit :: CRLim -> ResourceLimit
 unpackRLimit (#const RLIM_INFINITY)  = ResourceLimitInfinity
 unpackRLimit other
 #if defined(RLIM_SAVED_MAX)
-    | ((#const RLIM_SAVED_MAX) :: CRLim) /= (#const RLIM_INFINITY) &&
-      other == (#const RLIM_SAVED_MAX) = ResourceLimitUnknown
+    | ((#const RLIM_SAVED_MAX) :: CRLim) /= (#const RLIM_INFINITY)
+    , other == (#const RLIM_SAVED_MAX)
+    = ResourceLimitUnknown
 #endif
 #if defined(RLIM_SAVED_CUR)
-    | ((#const RLIM_SAVED_CUR) :: CRLim) /= (#const RLIM_INFINITY) &&
-      other == (#const RLIM_SAVED_CUR) = ResourceLimitUnknown
+    | ((#const RLIM_SAVED_CUR) :: CRLim) /= (#const RLIM_INFINITY)
+    , other == (#const RLIM_SAVED_CUR)
+#if __GLASGOW_HASKELL__ >= 905
+    , considerAccessible
+#endif
+    = ResourceLimitUnknown
+    -- (*) This pattern match is redundant if RLIM_SAVED_MAX and RLIM_SAVED_CUR
+    -- are both defined and are equal. This redundancy is only detected by GHC
+    -- starting from version 9.5, so we use 'considerAccessible'.
 #endif
     | otherwise = ResourceLimit (fromIntegral other)
 
-- 
GitLab