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

Preserve empty variant constructor in data-deps. #7303

Merged
merged 3 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,9 @@ generateSrcFromLf env = noLoc mod
fields' <- mapM (uncurry (mkConDeclField env)) fields
pure [ mkConDecl occName (RecCon (noLoc fields')) ]
LF.DataVariant cons -> do
let hasExactlyOneConstructor = length cons == 1
sequence
[ mkConDecl (occNameFor conName) <$> convConDetails ty
[ mkConDecl (occNameFor conName) <$> convConDetails hasExactlyOneConstructor ty
| (conName, ty) <- cons
]
LF.DataEnum cons -> do
Expand All @@ -477,8 +478,15 @@ generateSrcFromLf env = noLoc mod
, con_args = details
}

convConDetails :: LF.Type -> Gen (HsConDeclDetails GhcPs)
convConDetails = \case
convConDetails :: Bool -> LF.Type -> Gen (HsConDeclDetails GhcPs)
convConDetails hasExactlyOneConstructor = \case
-- nullary variant constructor (see issue #7207)
--
-- We translate a variant constructor `C ()` to `C` in DAML. But
-- if it's the only constructor, we leave it as `C ()` to distinguish
-- it from an enum type.
LF.TUnit | not hasExactlyOneConstructor ->
pure $ PrefixCon []

-- variant record constructor
LF.TConApp LF.Qualified{..} _
Expand Down
50 changes: 50 additions & 0 deletions compiler/damlc/tests/src/DA/Test/Packaging.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,56 @@ dataDependencyTests Tools{damlc,repl,validate,davlDar,oldProjDar} = testGroup "D
withCurrentDirectory (tmpDir </> "proj") $
callProcessSilent damlc ["build"]

, testCaseSteps "Empty variant constructors" $ \step -> withTempDir $ \tmpDir -> do
-- This test checks that variant constructors without argument
-- are preserved. This is a regression test for issue #7207.
step "building project with type definition"
createDirectoryIfMissing True (tmpDir </> "type")
writeFileUTF8 (tmpDir </> "type" </> "daml.yaml") $ unlines
[ "sdk-version: " <> sdkVersion
, "name: type"
, "source: ."
, "version: 0.1.0"
, "dependencies: [daml-prim, daml-stdlib]"
]
writeFileUTF8 (tmpDir </> "type" </> "Foo.daml") $ unlines
[ "module Foo where"
, "data A = B | C Int"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a third case D () and check that it behaves like D when imported as data-dependency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. D () is something we're deprecating, and the fact that it's treated the same as D in the current compiler is mostly a mistake.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally agree but would feel more comfortable to have a test as long as users can still write it, even though they get warned about it.

Copy link
Contributor Author

@sofiafaro-da sofiafaro-da Sep 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test for D () in the single constructor case, which is something we had talked about adding as a special case (since we can't distinguish single-constructor variants from enums any other way, and there's no ambiguity in the single constructor case). (This actually also came up in one of the other tests, but better to add one here as well.)

, "data D = D ()" -- single-constructor case uses explicit unit
]
withCurrentDirectory (tmpDir </> "type") $
callProcessSilent damlc ["build", "-o", "type.dar"]

step "building project that uses it via data-dependencies"
createDirectoryIfMissing True (tmpDir </> "proj")
writeFileUTF8 (tmpDir </> "proj" </> "daml.yaml") $ unlines
[ "sdk-version: " <> sdkVersion
, "name: proj"
, "source: ."
, "version: 0.1.0"
, "dependencies: [daml-prim, daml-stdlib]"
, "data-dependencies: "
, " - " <> (tmpDir </> "type" </> "type.dar")
]
writeFileUTF8 (tmpDir </> "proj" </> "Main.daml") $ unlines
[ "module Main where"
, "import Foo"
, "mkA : A"
, "mkA = B"
, "matchA : A -> Int"
, "matchA a ="
, " case a of"
, " B -> 0"
, " C n -> n"
, "mkD : D"
, "mkD = D ()"
, "matchD : D -> ()"
, "matchD d ="
, " case d of"
, " D () -> ()"
]
withCurrentDirectory (tmpDir </> "proj") $
callProcessSilent damlc ["build"]
]

-- | Check that the given file exists in the dar in the given directory.
Expand Down