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

Implement name collision check for type synonyms. #9723

Merged
merged 1 commit into from
May 18, 2021
Merged
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
Implement name collision check for type synonyms.
Part of #3616

changelog_begin
changelog_end
  • Loading branch information
sofiafaro-da committed May 18, 2021
commit c0648e7228414b3355f00bb670ff8543c0e15ee3
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