Skip to content

Commit acc460d

Browse files
authored
Merge pull request #4057 from Ericson2314/unqual-component-name
[WIP] Use UnqualComponentName newtype instead of String for component names
2 parents f26d20e + be6a94a commit acc460d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+471
-347
lines changed

Cabal/Distribution/Backpack/ComponentsGraph.hs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,22 @@ toComponentsGraph enabled pkg_descr =
5151
where
5252
-- The dependencies for the given component
5353
componentDeps component =
54-
[ CExeName toolname | Dependency pkgname _
55-
<- buildTools bi
56-
, let toolname = unPackageName pkgname
57-
, toolname `elem` map exeName
58-
(executables pkg_descr) ]
54+
[ CExeName toolname
55+
| Dependency pkgname _ <- buildTools bi
56+
, let toolname = packageNameToUnqualComponentName pkgname
57+
, toolname `elem` map exeName (executables pkg_descr) ]
5958

6059
++ [ if pkgname == packageName pkg_descr
61-
then CLibName
62-
else CSubLibName toolname
63-
| Dependency pkgname _
64-
<- targetBuildDepends bi
65-
, pkgname `elem` internalPkgDeps
66-
, let toolname = unPackageName pkgname ]
60+
then CLibName
61+
else CSubLibName toolname
62+
| Dependency pkgname _ <- targetBuildDepends bi
63+
, let toolname = packageNameToUnqualComponentName pkgname
64+
, toolname `elem` internalPkgDeps ]
6765
where
6866
bi = componentBuildInfo component
6967
internalPkgDeps = map (conv . libName) (allLibraries pkg_descr)
70-
conv Nothing = packageName pkg_descr
71-
conv (Just s) = mkPackageName s
68+
conv Nothing = packageNameToUnqualComponentName $ packageName pkg_descr
69+
conv (Just s) = s
7270

7371
-- | Error message when there is a cycle; takes the SCC of components.
7472
componentCycleMsg :: [ComponentName] -> Doc

Cabal/Distribution/Backpack/ConfiguredComponent.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ mkConfiguredComponent this_pid this_cid lib_deps exe_deps component =
104104

105105
type ConfiguredComponentMap =
106106
(Map PackageName (ComponentId, PackageId), -- libraries
107-
Map String ComponentId) -- executables
107+
Map UnqualComponentName ComponentId) -- executables
108108

109109
-- Executable map must be different because an executable can
110110
-- have the same name as a library. Ew.
@@ -141,7 +141,7 @@ toConfiguredComponent pkg_descr this_cid
141141
= Map.toList external_lib_map
142142
exe_deps = [ cid
143143
| Dependency pkgname _ <- buildTools bi
144-
, let name = unPackageName pkgname
144+
, let name = packageNameToUnqualComponentName pkgname
145145
, Just cid <- [ Map.lookup name exe_map ] ]
146146

147147
-- | Also computes the 'ComponentId', and sets cc_public if necessary.
@@ -183,7 +183,7 @@ extendConfiguredComponentMap cc (lib_map, exe_map) =
183183
Map.insert (pkgName (cc_pkgid cc))
184184
(cc_cid cc, cc_pkgid cc) lib_map
185185
CSubLibName str ->
186-
Map.insert (mkPackageName str)
186+
Map.insert (unqualComponentNameToPackageName str)
187187
(cc_cid cc, cc_pkgid cc) lib_map
188188
_ -> lib_map
189189
exe_map'

Cabal/Distribution/Backpack/Id.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ computeComponentId mb_ipid mb_cid pid cname mb_details =
6363
NoFlag -> mkComponentId $ actual_base
6464
++ (case componentNameString cname of
6565
Nothing -> ""
66-
Just s -> "-" ++ s)
66+
Just s -> "-" ++ unUnqualComponentName s)
6767

6868
-- | Computes the package name for a library. If this is the public
6969
-- library, it will just be the original package name; otherwise,
@@ -102,7 +102,8 @@ computeCompatPackageName pkg_name CLibName = pkg_name
102102
computeCompatPackageName pkg_name cname
103103
= mkPackageName $ "z-" ++ zdashcode (display pkg_name)
104104
++ (case componentNameString cname of
105-
Just cname_str -> "-z-" ++ zdashcode cname_str
105+
Just cname_u -> "-z-" ++ zdashcode cname_str
106+
where cname_str = unUnqualComponentName cname_u
106107
Nothing -> "")
107108

108109
zdashcode :: String -> String

Cabal/Distribution/Package.hs

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
module Distribution.Package (
2020
-- * Package ids
21+
UnqualComponentName, unUnqualComponentName, mkUnqualComponentName,
2122
PackageName, unPackageName, mkPackageName,
23+
packageNameToUnqualComponentName, unqualComponentNameToPackageName,
2224
PackageIdentifier(..),
2325
PackageId,
2426

@@ -69,6 +71,54 @@ import Distribution.ModuleName
6971

7072
import Text.PrettyPrint ((<+>), text)
7173

74+
-- | An unqualified component name, for any kind of component.
75+
--
76+
-- This is distinguished from a 'ComponentName' and 'ComponentId'. The former
77+
-- also states which of a library, executable, etc the name refers too. The
78+
-- later uniquely identifiers a component and its closure.
79+
--
80+
-- @since 2.0
81+
newtype UnqualComponentName = UnqualComponentName ShortText
82+
deriving (Generic, Read, Show, Eq, Ord, Typeable, Data,
83+
Semigroup, Monoid) -- TODO: bad enabler of bad monoids
84+
85+
-- | Convert 'UnqualComponentName' to 'String'
86+
--
87+
-- @since 2.0
88+
unUnqualComponentName :: UnqualComponentName -> String
89+
unUnqualComponentName (UnqualComponentName s) = fromShortText s
90+
91+
-- | Construct a 'UnqualComponentName' from a 'String'
92+
--
93+
-- 'mkUnqualComponentName' is the inverse to 'unUnqualComponentName'
94+
--
95+
-- Note: No validations are performed to ensure that the resulting
96+
-- 'UnqualComponentName' is valid
97+
--
98+
-- @since 2.0
99+
mkUnqualComponentName :: String -> UnqualComponentName
100+
mkUnqualComponentName = UnqualComponentName . toShortText
101+
102+
instance Binary UnqualComponentName
103+
104+
parsePackageName :: Parse.ReadP r String
105+
parsePackageName = do
106+
ns <- Parse.sepBy1 component (Parse.char '-')
107+
return $ intercalate "-" ns
108+
where
109+
component = do
110+
cs <- Parse.munch1 isAlphaNum
111+
if all isDigit cs then Parse.pfail else return cs
112+
-- each component must contain an alphabetic character, to avoid
113+
-- ambiguity in identifiers like foo-1 (the 1 is the version number).
114+
115+
instance Text UnqualComponentName where
116+
disp = Disp.text . unUnqualComponentName
117+
parse = mkUnqualComponentName <$> parsePackageName
118+
119+
instance NFData UnqualComponentName where
120+
rnf (UnqualComponentName pkg) = rnf pkg
121+
72122
-- | A package name.
73123
--
74124
-- Use 'mkPackageName' and 'unPackageName' to convert from/to a
@@ -95,19 +145,32 @@ unPackageName (PackageName s) = fromShortText s
95145
mkPackageName :: String -> PackageName
96146
mkPackageName = PackageName . toShortText
97147

148+
-- | Converts a package name to an unqualified component name
149+
--
150+
-- Useful in legacy situations where a package name may refer to an internal
151+
-- component, if one is defined with that name.
152+
--
153+
-- @since 2.0
154+
packageNameToUnqualComponentName :: PackageName -> UnqualComponentName
155+
packageNameToUnqualComponentName (PackageName s) = UnqualComponentName s
156+
157+
-- | Converts an unqualified component name to a package name
158+
--
159+
-- `packageNameToUnqualComponentName` is the inverse of
160+
-- `unqualComponentNameToPackageName`.
161+
--
162+
-- Useful in legacy situations where a package name may refer to an internal
163+
-- component, if one is defined with that name.
164+
--
165+
-- @since 2.0
166+
unqualComponentNameToPackageName :: UnqualComponentName -> PackageName
167+
unqualComponentNameToPackageName (UnqualComponentName s) = PackageName s
168+
98169
instance Binary PackageName
99170

100171
instance Text PackageName where
101172
disp = Disp.text . unPackageName
102-
parse = do
103-
ns <- Parse.sepBy1 component (Parse.char '-')
104-
return (mkPackageName (intercalate "-" ns))
105-
where
106-
component = do
107-
cs <- Parse.munch1 isAlphaNum
108-
if all isDigit cs then Parse.pfail else return cs
109-
-- each component must contain an alphabetic character, to avoid
110-
-- ambiguity in identifiers like foo-1 (the 1 is the version number).
173+
parse = mkPackageName <$> parsePackageName
111174

112175
instance NFData PackageName where
113176
rnf (PackageName pkg) = rnf pkg

Cabal/Distribution/PackageDescription/Check.hs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,15 @@ checkSanity pkg =
185185
++ "Only the non-internal library can have the same name as the package."
186186

187187
, check (not (null duplicateNames)) $
188-
PackageBuildImpossible $ "Duplicate sections: " ++ commaSep duplicateNames
188+
PackageBuildImpossible $ "Duplicate sections: "
189+
++ commaSep (map unUnqualComponentName duplicateNames)
189190
++ ". The name of every library, executable, test suite,"
190191
++ " and benchmark section in"
191192
++ " the package must be unique."
192193

193194
-- NB: but it's OK for executables to have the same name!
194-
, check (any (== display (packageName pkg)) subLibNames) $
195+
-- TODO shouldn't need to compare on the string level
196+
, check (any (== display (packageName pkg)) (display <$> subLibNames)) $
195197
PackageBuildImpossible $ "Illegal internal library name "
196198
++ display (packageName pkg)
197199
++ ". Internal libraries cannot have the same name as the package."
@@ -239,7 +241,7 @@ checkLibrary pkg lib =
239241
PackageDistSuspiciousWarn $
240242
"Library " ++ (case libName lib of
241243
Nothing -> ""
242-
Just n -> n
244+
Just n -> display n
243245
) ++ "does not expose any modules"
244246

245247
-- check use of signatures sections
@@ -273,7 +275,7 @@ checkExecutable pkg exe =
273275

274276
check (null (modulePath exe)) $
275277
PackageBuildImpossible $
276-
"No 'main-is' field found for executable " ++ exeName exe
278+
"No 'main-is' field found for executable " ++ display (exeName exe)
277279

278280
, check (not (null (modulePath exe))
279281
&& (not $ fileExtensionSupportedLanguage $ modulePath exe)) $
@@ -291,14 +293,14 @@ checkExecutable pkg exe =
291293

292294
, check (not (null moduleDuplicates)) $
293295
PackageBuildImpossible $
294-
"Duplicate modules in executable '" ++ exeName exe ++ "': "
296+
"Duplicate modules in executable '" ++ display (exeName exe) ++ "': "
295297
++ commaSep (map display moduleDuplicates)
296298

297299
-- check that all autogen-modules appear on other-modules
298300
, check
299301
(not $ and $ map (flip elem (exeModules exe)) (exeModulesAutogen exe)) $
300302
PackageBuildImpossible $
301-
"On executable '" ++ exeName exe ++ "' an 'autogen-module' is not "
303+
"On executable '" ++ display (exeName exe) ++ "' an 'autogen-module' is not "
302304
++ "on 'other-modules'"
303305

304306
]
@@ -325,7 +327,7 @@ checkTestSuite pkg test =
325327

326328
, check (not $ null moduleDuplicates) $
327329
PackageBuildImpossible $
328-
"Duplicate modules in test suite '" ++ testName test ++ "': "
330+
"Duplicate modules in test suite '" ++ display (testName test) ++ "': "
329331
++ commaSep (map display moduleDuplicates)
330332

331333
, check mainIsWrongExt $
@@ -346,7 +348,7 @@ checkTestSuite pkg test =
346348
(testModulesAutogen test)
347349
) $
348350
PackageBuildImpossible $
349-
"On test suite '" ++ testName test ++ "' an 'autogen-module' is not "
351+
"On test suite '" ++ display (testName test) ++ "' an 'autogen-module' is not "
350352
++ "on 'other-modules'"
351353
]
352354
where
@@ -380,7 +382,7 @@ checkBenchmark _pkg bm =
380382

381383
, check (not $ null moduleDuplicates) $
382384
PackageBuildImpossible $
383-
"Duplicate modules in benchmark '" ++ benchmarkName bm ++ "': "
385+
"Duplicate modules in benchmark '" ++ display (benchmarkName bm) ++ "': "
384386
++ commaSep (map display moduleDuplicates)
385387

386388
, check mainIsWrongExt $
@@ -395,7 +397,7 @@ checkBenchmark _pkg bm =
395397
(benchmarkModulesAutogen bm)
396398
) $
397399
PackageBuildImpossible $
398-
"On benchmark '" ++ benchmarkName bm ++ "' an 'autogen-module' is "
400+
"On benchmark '" ++ display (benchmarkName bm) ++ "' an 'autogen-module' is "
399401
++ "not on 'other-modules'"
400402
]
401403
where
@@ -552,7 +554,7 @@ checkFields pkg =
552554
, isNoVersion vr ]
553555

554556
internalLibraries =
555-
map (maybe (packageName pkg) mkPackageName . libName)
557+
map (maybe (packageName pkg) (unqualComponentNameToPackageName) . libName)
556558
(allLibraries pkg)
557559
buildDependsRangeOnInternalLibrary =
558560
[ dep

Cabal/Distribution/PackageDescription/Configuration.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ constrainBy left extra =
452452

453453
-- | Collect up the targets in a TargetSet of tagged targets, storing the
454454
-- dependencies as we go.
455-
flattenTaggedTargets :: TargetSet PDTagged -> (Maybe Library, [(String, Component)])
455+
flattenTaggedTargets :: TargetSet PDTagged -> (Maybe Library, [(UnqualComponentName, Component)])
456456
flattenTaggedTargets (TargetSet targets) = foldr untag (Nothing, []) targets
457457
where
458458
untag (_, Lib _) (Just _, _) = userBug "Only one library expected"
@@ -464,7 +464,7 @@ flattenTaggedTargets (TargetSet targets) = foldr untag (Nothing, []) targets
464464
}
465465
untag (deps, SubComp n c) (mb_lib, comps)
466466
| any ((== n) . fst) comps =
467-
userBug $ "There exist several components with the same name: '" ++ n ++ "'"
467+
userBug $ "There exist several components with the same name: '" ++ unUnqualComponentName n ++ "'"
468468

469469
| otherwise = (mb_lib, (n, c') : comps)
470470
where
@@ -484,7 +484,7 @@ flattenTaggedTargets (TargetSet targets) = foldr untag (Nothing, []) targets
484484
--
485485

486486
data PDTagged = Lib Library
487-
| SubComp String Component
487+
| SubComp UnqualComponentName Component
488488
| PDNull
489489
deriving Show
490490

0 commit comments

Comments
 (0)