Skip to content

Commit

Permalink
Implement name collision check for type synonyms. (#9723)
Browse files Browse the repository at this point in the history
Part of #3616

changelog_begin
changelog_end
  • Loading branch information
sofiafaro-da authored May 18, 2021
1 parent c7cc1bf commit 2a2c745
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions compiler/daml-lf-tools/src/DA/Daml/LF/TypeChecker/NameCollision.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ data Name
| NRecordType ModuleName TypeConName
| NVariantType ModuleName TypeConName
| NEnumType ModuleName TypeConName
| NTypeSynonym ModuleName TypeConName
| NTypeSynonym ModuleName TypeSynName
| NVariantCon ModuleName TypeConName VariantConName
| NEnumCon ModuleName TypeConName VariantConName
| NField ModuleName TypeConName FieldName
Expand All @@ -58,7 +58,7 @@ displayName = \case
T.concat ["variant ", dot m, ":", dot t]
NEnumType (ModuleName m) (TypeConName t) ->
T.concat ["enum ", dot m, ":", dot t]
NTypeSynonym (ModuleName m) (TypeConName t) ->
NTypeSynonym (ModuleName m) (TypeSynName t) ->
T.concat ["synonym ", dot m, ":", dot t]
NVariantCon (ModuleName m) (TypeConName t) (VariantConName v) ->
T.concat ["variant constructor ", dot m, ":", dot t, ".", v]
Expand Down Expand Up @@ -112,7 +112,7 @@ fullyResolve = FRName . map T.toLower . \case
m ++ t
NEnumType (ModuleName m) (TypeConName t) ->
m ++ t
NTypeSynonym (ModuleName m) (TypeConName t) ->
NTypeSynonym (ModuleName m) (TypeSynName t) ->
m ++ t
NVariantCon (ModuleName m) (TypeConName t) (VariantConName v) ->
m ++ t ++ [v]
Expand Down Expand Up @@ -193,6 +193,10 @@ checkTemplate moduleName Template{..} = do
forM_ tplChoices $ \TemplateChoice{..} ->
checkName (NChoice moduleName tplTypeCon chcName)

checkSynonym :: ModuleName -> DefTypeSyn -> NCMonad ()
checkSynonym moduleName DefTypeSyn{..} =
checkName (NTypeSynonym moduleName synName)

checkModuleName :: Module -> NCMonad ()
checkModuleName m =
checkName (NModule (moduleName m))
Expand All @@ -207,6 +211,8 @@ checkModuleBody m = do
checkDataType (moduleName m) dataType
forM_ (moduleTemplates m) $ \tpl ->
checkTemplate (moduleName m) tpl
forM_ (moduleSynonyms m) $ \synonym ->
checkSynonym (moduleName m) synonym

checkModule :: Module -> NCMonad ()
checkModule m = do
Expand Down Expand Up @@ -238,13 +244,12 @@ isAscendant (ModuleName xs) (ModuleName ys) =
-- name collision condition.
checkModuleDeps :: World -> Module -> NCMonad ()
checkModuleDeps world mod0 = do
-- TODO #3616: check for collisions with TypeSynonyms
let package = getWorldSelf world
modules = NM.toList (packageModules package)
name0 = moduleName mod0
ascendants = filter (flip isAscendant name0 . moduleName) modules
descendants = filter (isAscendant name0 . moduleName) modules
mapM_ checkModuleBody ascendants -- only need type names
mapM_ checkModuleBody ascendants -- only need type and synonym names
mapM_ checkModuleName descendants -- only need module names
mapM_ checkVirtualModuleName (concatMap virtualModuleNames descendants)
checkModule mod0
Expand Down

0 comments on commit 2a2c745

Please sign in to comment.