Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid partial Data.List.last in autogenerated Paths_<pkg>.hs #10432

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Avoid partial last in autogen Paths_<pkg>.hs
  • Loading branch information
mpilgrem committed Oct 7, 2024
commit e4e3f6085a519bb8f2acbe16ad55cbfc6b604882
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
Loading