Skip to content

Commit

Permalink
Allow imports of internal modules (#10397)
Browse files Browse the repository at this point in the history
* 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 #10379
changelog_end

* ~unstable~

#10397 (comment)

Co-authored-by: Andreas Herrmann <[email protected]>
  • Loading branch information
aherrmann-da and aherrmann authored Jul 26, 2021
1 parent b9518ce commit 9c064da
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@

module DA.Daml.LFConversion
( convertModule
, convertModuleName
, sourceLocToRange
, convertRationalBigNumeric -- exposed for festing
, runConvertM -- exposed for testing
Expand Down
2 changes: 2 additions & 0 deletions compiler/damlc/daml-preprocessor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
9 changes: 8 additions & 1 deletion compiler/damlc/daml-preprocessor/src/DA/Daml/Preprocessor.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)]
Expand Down
5 changes: 3 additions & 2 deletions compiler/damlc/tests/daml-test-files/InternalImport.daml
Original file line number Diff line number Diff line change
@@ -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 ()

0 comments on commit 9c064da

Please sign in to comment.