From e4e3f6085a519bb8f2acbe16ad55cbfc6b604882 Mon Sep 17 00:00:00 2001 From: Mike Pilgrem Date: Sun, 6 Oct 2024 17:00:00 +0100 Subject: [PATCH] Avoid partial last in autogen Paths_.hs --- Cabal/src/Distribution/Simple/Build/PathsModule/Z.hs | 10 +++++++--- changelog.d/pr-10432 | 10 ++++++++++ templates/Paths_pkg.template.hs | 10 +++++++--- 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 changelog.d/pr-10432 diff --git a/Cabal/src/Distribution/Simple/Build/PathsModule/Z.hs b/Cabal/src/Distribution/Simple/Build/PathsModule/Z.hs index 25c924720ec..ad979c42951 100644 --- a/Cabal/src/Distribution/Simple/Build/PathsModule/Z.hs +++ b/Cabal/src/Distribution/Simple/Build/PathsModule/Z.hs @@ -69,7 +69,6 @@ render z_root = execWriter $ do return () tell "\n" tell "import qualified Control.Exception as Exception\n" - tell "import qualified Data.List as List\n" tell "import Data.Version (Version(..))\n" tell "import System.Environment (getEnv)\n" tell "import Prelude\n" @@ -306,9 +305,14 @@ render z_root = execWriter $ do tell "joinFileName \"\" fname = fname\n" tell "joinFileName \".\" fname = fname\n" tell "joinFileName dir \"\" = dir\n" - tell "joinFileName dir fname\n" - tell " | isPathSeparator (List.last dir) = dir ++ fname\n" + tell "joinFileName dir@(c:cs) fname\n" + tell " | isPathSeparator (lastChar c cs) = dir ++ fname\n" tell " | otherwise = dir ++ pathSeparator : fname\n" + tell " where\n" + tell " -- We do not use Data.List.NonEmpty.last, as that would limit the module to\n" + tell " -- base >= 4.9.0.0 (GHC >= 8.0.1).\n" + tell " lastChar x [] = x\n" + tell " lastChar _ (x:xs) = lastChar x xs\n" tell "\n" tell "pathSeparator :: Char\n" if (zIsWindows z_root) diff --git a/changelog.d/pr-10432 b/changelog.d/pr-10432 new file mode 100644 index 00000000000..1bb76adb0a1 --- /dev/null +++ b/changelog.d/pr-10432 @@ -0,0 +1,10 @@ +synopsis: Avoid partial `Data.List.last` in autogenerated `Paths_.hs` +packages: Cabal +prs: #10432 +significance: + +description: { + +- Autogenerated `Paths_.hs` now avoids use of the partial function `Data.List.last`. + +} diff --git a/templates/Paths_pkg.template.hs b/templates/Paths_pkg.template.hs index 8e1e03d27e4..bea7d6813e3 100644 --- a/templates/Paths_pkg.template.hs +++ b/templates/Paths_pkg.template.hs @@ -26,7 +26,6 @@ import Foreign.C {% endif %} import qualified Control.Exception as Exception -import qualified Data.List as List import Data.Version (Version(..)) import System.Environment (getEnv) import Prelude @@ -175,9 +174,14 @@ joinFileName :: String -> String -> FilePath joinFileName "" fname = fname joinFileName "." fname = fname joinFileName dir "" = dir -joinFileName dir fname - | isPathSeparator (List.last dir) = dir ++ fname +joinFileName dir@(c:cs) fname + | isPathSeparator (lastChar c cs) = dir ++ fname | otherwise = dir ++ pathSeparator : fname + where + -- We do not use Data.List.NonEmpty.last, as that would limit the module to + -- base >= 4.9.0.0 (GHC >= 8.0.1). + lastChar x [] = x + lastChar _ (x:xs) = lastChar x xs pathSeparator :: Char {% if isWindows %}