Skip to content

Commit

Permalink
Avoid partial last in autogen Paths_<pkg>.hs
Browse files Browse the repository at this point in the history
  • Loading branch information
mpilgrem authored and Mikolaj committed Oct 9, 2024
1 parent 3619359 commit 14a767f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
10 changes: 7 additions & 3 deletions Cabal/src/Distribution/Simple/Build/PathsModule/Z.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions changelog.d/pr-10432
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
synopsis: Avoid partial `Data.List.last` in autogenerated `Paths_<pkg>.hs`
packages: Cabal
prs: #10432
significance:

description: {

- Autogenerated `Paths_<pkg>.hs` now avoids use of the partial function `Data.List.last`.

}
10 changes: 7 additions & 3 deletions templates/Paths_pkg.template.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 %}
Expand Down

0 comments on commit 14a767f

Please sign in to comment.