From 3ad9b76a4c7800ab596bb2a4f0b00195f76db040 Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Fri, 23 Jul 2021 16:51:56 +0200 Subject: [PATCH 1/2] Allow imports of internal modules changelog_begin - [Daml Compiler] Imports of internal modules from stable packages are no longer illegal. Previously, the compiler raised an error when it encountered imports of internal modules such as `DA.Internal.Template`. Such imports are now accepted by the compiler. Note, however, that internal modules are still not part of the stable API. Fixes https://github.com/digital-asset/daml/issues/10379 changelog_end --- .../daml-lf-conversion/src/DA/Daml/LFConversion.hs | 1 + compiler/damlc/daml-preprocessor/BUILD.bazel | 2 ++ .../daml-preprocessor/src/DA/Daml/Preprocessor.hs | 11 +++++++++-- .../damlc/tests/daml-test-files/InternalImport.daml | 5 +++-- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/compiler/damlc/daml-lf-conversion/src/DA/Daml/LFConversion.hs b/compiler/damlc/daml-lf-conversion/src/DA/Daml/LFConversion.hs index 93d33b8622d7..5dce65bbbdf7 100644 --- a/compiler/damlc/daml-lf-conversion/src/DA/Daml/LFConversion.hs +++ b/compiler/damlc/daml-lf-conversion/src/DA/Daml/LFConversion.hs @@ -75,6 +75,7 @@ module DA.Daml.LFConversion ( convertModule + , convertModuleName , sourceLocToRange , convertRationalBigNumeric -- exposed for festing , runConvertM -- exposed for testing diff --git a/compiler/damlc/daml-preprocessor/BUILD.bazel b/compiler/damlc/daml-preprocessor/BUILD.bazel index 96ec5cb5bda8..3d897af34814 100644 --- a/compiler/damlc/daml-preprocessor/BUILD.bazel +++ b/compiler/damlc/daml-preprocessor/BUILD.bazel @@ -35,6 +35,8 @@ da_haskell_library( "//compiler/daml-lf-ast", "//compiler/daml-lf-proto", "//compiler/daml-lf-tools", + "//compiler/damlc/daml-lf-conversion", + "//compiler/damlc/stable-packages:stable-packages-lib", "//libs-haskell/bazel-runfiles", "//libs-haskell/da-hs-base", ], diff --git a/compiler/damlc/daml-preprocessor/src/DA/Daml/Preprocessor.hs b/compiler/damlc/daml-preprocessor/src/DA/Daml/Preprocessor.hs index 30f343230bdd..a9d3a439c0a2 100644 --- a/compiler/damlc/daml-preprocessor/src/DA/Daml/Preprocessor.hs +++ b/compiler/damlc/daml-preprocessor/src/DA/Daml/Preprocessor.hs @@ -8,9 +8,11 @@ module DA.Daml.Preprocessor , noPreprocessor ) where +import DA.Daml.LFConversion (convertModuleName) import DA.Daml.Preprocessor.Records import DA.Daml.Preprocessor.Generics import DA.Daml.Preprocessor.EnumType +import DA.Daml.StablePackages (stablePackageByModuleName) import Development.IDE.Types.Options import qualified "ghc-lib" GHC @@ -47,6 +49,11 @@ isInternal (GHC.moduleNameString -> x) , "DA.Time.Types" ] +isUnstableInternal :: GHC.ModuleName -> Bool +isUnstableInternal moduleName = + isInternal moduleName && + convertModuleName moduleName `Map.notMember` stablePackageByModuleName + preprocessorExceptions :: Set.Set GHC.ModuleName preprocessorExceptions = Set.fromList $ map GHC.mkModuleName -- These modules need to import internal modules. @@ -163,8 +170,8 @@ checkModuleName (GHC.L _ m) -- | We ban people from importing modules such checkImports :: GHC.ParsedSource -> [(GHC.SrcSpan, String)] checkImports x = - [ (ss, "Import of internal module " ++ GHC.moduleNameString m ++ " is not allowed.") - | GHC.L ss GHC.ImportDecl{ideclName=GHC.L _ m} <- GHC.hsmodImports $ GHC.unLoc x, isInternal m] + [ (ss, "Import of unstable internal module " ++ GHC.moduleNameString m ++ " is not allowed.") + | GHC.L ss GHC.ImportDecl{ideclName=GHC.L _ m} <- GHC.hsmodImports $ GHC.unLoc x, isUnstableInternal m] -- | Emit a warning if the "daml 1.2" version header is present. checkDamlHeader :: GHC.ParsedSource -> [(GHC.SrcSpan, String)] diff --git a/compiler/damlc/tests/daml-test-files/InternalImport.daml b/compiler/damlc/tests/daml-test-files/InternalImport.daml index 630b3ed96877..aede2af35af7 100644 --- a/compiler/damlc/tests/daml-test-files/InternalImport.daml +++ b/compiler/damlc/tests/daml-test-files/InternalImport.daml @@ -1,9 +1,10 @@ -- Copyright (c) 2020, Digital Asset (Switzerland) GmbH and/or its affiliates. -- All rights reserved. --- @ERROR range=9:1-9:28; Import of internal module DA.Internal.Template is not allowed. +-- @ERROR range=10:1-10:39; Import of unstable internal module DA.Internal.RebindableSyntax is not allowed. module InternalImport where -import DA.Internal.Template +import DA.Internal.Template () +import DA.Internal.RebindableSyntax () From b1311772193a8d75067f8c68eafb0149976cb24b Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Mon, 26 Jul 2021 12:52:49 +0200 Subject: [PATCH 2/2] ~unstable~ https://github.com/digital-asset/daml/pull/10397#discussion_r676485891 --- compiler/damlc/daml-preprocessor/src/DA/Daml/Preprocessor.hs | 2 +- compiler/damlc/tests/daml-test-files/InternalImport.daml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/damlc/daml-preprocessor/src/DA/Daml/Preprocessor.hs b/compiler/damlc/daml-preprocessor/src/DA/Daml/Preprocessor.hs index a9d3a439c0a2..9c97621fba73 100644 --- a/compiler/damlc/daml-preprocessor/src/DA/Daml/Preprocessor.hs +++ b/compiler/damlc/daml-preprocessor/src/DA/Daml/Preprocessor.hs @@ -170,7 +170,7 @@ checkModuleName (GHC.L _ m) -- | We ban people from importing modules such checkImports :: GHC.ParsedSource -> [(GHC.SrcSpan, String)] checkImports x = - [ (ss, "Import of unstable internal module " ++ GHC.moduleNameString m ++ " is not allowed.") + [ (ss, "Import of internal module " ++ GHC.moduleNameString m ++ " is not allowed.") | GHC.L ss GHC.ImportDecl{ideclName=GHC.L _ m} <- GHC.hsmodImports $ GHC.unLoc x, isUnstableInternal m] -- | Emit a warning if the "daml 1.2" version header is present. diff --git a/compiler/damlc/tests/daml-test-files/InternalImport.daml b/compiler/damlc/tests/daml-test-files/InternalImport.daml index aede2af35af7..acb603747f0a 100644 --- a/compiler/damlc/tests/daml-test-files/InternalImport.daml +++ b/compiler/damlc/tests/daml-test-files/InternalImport.daml @@ -1,7 +1,7 @@ -- Copyright (c) 2020, Digital Asset (Switzerland) GmbH and/or its affiliates. -- All rights reserved. --- @ERROR range=10:1-10:39; Import of unstable internal module DA.Internal.RebindableSyntax is not allowed. +-- @ERROR range=10:1-10:39; Import of internal module DA.Internal.RebindableSyntax is not allowed. module InternalImport where