From 9c064dac3528fda98fd29ee93a03ae24e87d17bb Mon Sep 17 00:00:00 2001 From: Andreas Herrmann <42969706+aherrmann-da@users.noreply.github.com> Date: Mon, 26 Jul 2021 13:45:52 +0200 Subject: [PATCH] Allow imports of internal modules (#10397) * 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 * ~unstable~ https://github.com/digital-asset/daml/pull/10397#discussion_r676485891 Co-authored-by: Andreas Herrmann --- .../damlc/daml-lf-conversion/src/DA/Daml/LFConversion.hs | 1 + compiler/damlc/daml-preprocessor/BUILD.bazel | 2 ++ .../damlc/daml-preprocessor/src/DA/Daml/Preprocessor.hs | 9 ++++++++- compiler/damlc/tests/daml-test-files/InternalImport.daml | 5 +++-- 4 files changed, 14 insertions(+), 3 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..9c97621fba73 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. @@ -164,7 +171,7 @@ checkModuleName (GHC.L _ m) 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] + | 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..acb603747f0a 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 internal module DA.Internal.RebindableSyntax is not allowed. module InternalImport where -import DA.Internal.Template +import DA.Internal.Template () +import DA.Internal.RebindableSyntax ()