From cf2b387c3bd72e9c342acf9ba1b3169463cd3a1c Mon Sep 17 00:00:00 2001 From: Paul Brauner <141240651+paulbrauner-da@users.noreply.github.com> Date: Wed, 3 Apr 2024 16:24:22 +0200 Subject: [PATCH] Disallow keys in LF 2.1 in the compiler (#18886) * Disable keys in LfConversion. * fix UnusedMatchTests.daml in integration-v21 * fix //compiler/damlc/tests:upgrades * stop compiling daml tests that use keys with 2.1 * migrate the upgrade examples to 2.dev as they use keys * only compile daml-script/tests dar using keys to 2.dev * do not compile daml-script/test daml files that use keys to 2.1 * fix //docs:bindings-java-daml-test * add TODOs everywhere tests need to be split * add a tests that checks that contract keys are rejected for LF<2.dev * remove keys from compatibility tests * Add the qualified template name to the keys not supported error message --- .../example/UpgradeFromCoinV1.daml | 4 - .../src/DA/Daml/LFConversion.hs | 3 + sdk/compiler/damlc/tests/BUILD.bazel | 1 + .../ContractKeysNotSupported.daml | 12 ++ .../UnusedMatchTests.EXPECTED.desugared-daml | 27 ---- .../daml-test-files/UnusedMatchTests.daml | 2 - ...MatchTestsWithKeys.EXPECTED.desugared-daml | 144 ++++++++++++++++++ .../UnusedMatchTestsWithKeys.daml | 55 +++++++ .../damlc/tests/src/DA/Test/DamlcUpgrades.hs | 51 ++++++- sdk/daml-lf/engine/BUILD.bazel | 5 +- .../engine/src/test/daml/BasicTests.daml | 6 +- .../engine/src/test/daml/Demonstrator.daml | 4 + sdk/daml-lf/tests/BUILD.bazel | 31 ++-- sdk/daml-lf/tests/Exceptions.daml | 4 + sdk/daml-script/test/BUILD.bazel | 45 +++--- sdk/docs/BUILD.bazel | 21 ++- .../upgrade/example/carbon-1.0.0/daml.yaml | 7 +- .../upgrade/example/carbon-2.0.0/daml.yaml | 5 + .../example/carbon-initiate-upgrade/daml.yaml | 7 +- .../upgrade/example/carbon-label/daml.yaml | 5 + .../upgrade/example/carbon-upgrade/daml.yaml | 5 + 21 files changed, 358 insertions(+), 86 deletions(-) create mode 100644 sdk/compiler/damlc/tests/daml-test-files/ContractKeysNotSupported.daml create mode 100644 sdk/compiler/damlc/tests/daml-test-files/UnusedMatchTestsWithKeys.EXPECTED.desugared-daml create mode 100644 sdk/compiler/damlc/tests/daml-test-files/UnusedMatchTestsWithKeys.daml diff --git a/sdk/compatibility/bazel_tools/data_dependencies/example/UpgradeFromCoinV1.daml b/sdk/compatibility/bazel_tools/data_dependencies/example/UpgradeFromCoinV1.daml index 174ccea5d970..c1b973e11a9a 100644 --- a/sdk/compatibility/bazel_tools/data_dependencies/example/UpgradeFromCoinV1.daml +++ b/sdk/compatibility/bazel_tools/data_dependencies/example/UpgradeFromCoinV1.daml @@ -15,8 +15,6 @@ template UpgradeCoinProposal where signatory issuer observer owner - key (issuer, owner) : (Party, Party) - maintainer key._1 choice Accept : ContractId UpgradeCoinAgreement controller owner do create UpgradeCoinAgreement with .. @@ -29,8 +27,6 @@ template UpgradeCoinAgreement owner : Party where signatory issuer, owner - key (issuer, owner) : (Party, Party) - maintainer key._1 nonconsuming choice Upgrade : ContractId CoinWithAmount with coinId : ContractId Coin diff --git a/sdk/compiler/damlc/daml-lf-conversion/src/DA/Daml/LFConversion.hs b/sdk/compiler/damlc/daml-lf-conversion/src/DA/Daml/LFConversion.hs index 0b8b1d094f8d..e9ff7aab6d79 100644 --- a/sdk/compiler/damlc/daml-lf-conversion/src/DA/Daml/LFConversion.hs +++ b/sdk/compiler/damlc/daml-lf-conversion/src/DA/Daml/LFConversion.hs @@ -1116,6 +1116,9 @@ convertTemplate env mc tplTypeCon tbinds@TemplateBinds{..} convertTemplateKey :: SdkVersioned => Env -> LF.TypeConName -> TemplateBinds -> ConvertM (Maybe TemplateKey) convertTemplateKey env tname TemplateBinds{..} + | Just fKey <- tbKey + , not (envLfVersion env `supports` featureContractKeys) = + unsupported "Contract keys." (T.unpack $ T.intercalate "." $ unTypeConName tname) | Just keyTy <- tbKeyType , Just fKey <- tbKey , Just fMaintainer <- tbMaintainer diff --git a/sdk/compiler/damlc/tests/BUILD.bazel b/sdk/compiler/damlc/tests/BUILD.bazel index 5e58113f2e26..a224efdf7b3f 100644 --- a/sdk/compiler/damlc/tests/BUILD.bazel +++ b/sdk/compiler/damlc/tests/BUILD.bazel @@ -459,6 +459,7 @@ da_haskell_test( "filepath", "process", "regex-tdfa", + "safe", "tasty", "tasty-hunit", "text", diff --git a/sdk/compiler/damlc/tests/daml-test-files/ContractKeysNotSupported.daml b/sdk/compiler/damlc/tests/daml-test-files/ContractKeysNotSupported.daml new file mode 100644 index 000000000000..249d952d4eb6 --- /dev/null +++ b/sdk/compiler/damlc/tests/daml-test-files/ContractKeysNotSupported.daml @@ -0,0 +1,12 @@ +-- @DOES-NOT-SUPPORT-LF-FEATURE DAML_CONTRACT_KEYS +-- @ERROR Contract keys + +module ContractKeysNotSupported where + +template TemplateWithKey + with + p: Party + where + signatory p + key p: Party + maintainer key \ No newline at end of file diff --git a/sdk/compiler/damlc/tests/daml-test-files/UnusedMatchTests.EXPECTED.desugared-daml b/sdk/compiler/damlc/tests/daml-test-files/UnusedMatchTests.EXPECTED.desugared-daml index 71ae8569f9c0..1355db3aa4e8 100644 --- a/sdk/compiler/damlc/tests/daml-test-files/UnusedMatchTests.EXPECTED.desugared-daml +++ b/sdk/compiler/damlc/tests/daml-test-files/UnusedMatchTests.EXPECTED.desugared-daml @@ -103,33 +103,6 @@ _choice$_T$Revoke \ self this@T {..} arg@Revoke -> let _ = self in let _ = this in let _ = arg in do pure (revokeRetVal this)) -instance DA.Internal.Desugar.HasExerciseByKey T (Party, - Text) DA.Internal.Desugar.Archive (()) where - _exerciseByKey = GHC.Types.primitive @"UExerciseByKey" -instance DA.Internal.Desugar.HasExerciseByKey T (Party, - Text) Revoke (()) where - _exerciseByKey = GHC.Types.primitive @"UExerciseByKey" -instance DA.Internal.Desugar.HasKey T (Party, Text) where - key this@T {..} - = userWrittenTuple (sig this, ident this) - where - _ = this -instance DA.Internal.Desugar.HasMaintainer T (Party, Text) where - _maintainer _ key - = DA.Internal.Desugar.toParties - ((DA.Internal.Record.getField @"_1" key)) - where - _ = key -instance DA.Internal.Desugar.HasFetchByKey T (Party, Text) where - fetchByKey = GHC.Types.primitive @"UFetchByKey" -instance DA.Internal.Desugar.HasLookupByKey T (Party, Text) where - lookupByKey = GHC.Types.primitive @"ULookupByKey" -instance DA.Internal.Desugar.HasToAnyContractKey T (Party, - Text) where - _toAnyContractKey = GHC.Types.primitive @"EToAnyContractKey" -instance DA.Internal.Desugar.HasFromAnyContractKey T (Party, - Text) where - _fromAnyContractKey = GHC.Types.primitive @"EFromAnyContractKey" revokeRetVal : T -> () revokeRetVal _ = () assertion : T -> Bool diff --git a/sdk/compiler/damlc/tests/daml-test-files/UnusedMatchTests.daml b/sdk/compiler/damlc/tests/daml-test-files/UnusedMatchTests.daml index 8cd716c4aa89..793a339721d5 100644 --- a/sdk/compiler/damlc/tests/daml-test-files/UnusedMatchTests.daml +++ b/sdk/compiler/damlc/tests/daml-test-files/UnusedMatchTests.daml @@ -28,8 +28,6 @@ template T signatory sig this observer obs this ensure assertion this - key (sig this, ident this): (Party, Text) - maintainer key._1 choice Revoke: () with controller p do diff --git a/sdk/compiler/damlc/tests/daml-test-files/UnusedMatchTestsWithKeys.EXPECTED.desugared-daml b/sdk/compiler/damlc/tests/daml-test-files/UnusedMatchTestsWithKeys.EXPECTED.desugared-daml new file mode 100644 index 000000000000..f2ae5de1de13 --- /dev/null +++ b/sdk/compiler/damlc/tests/daml-test-files/UnusedMatchTestsWithKeys.EXPECTED.desugared-daml @@ -0,0 +1,144 @@ +module UnusedMatchTestsWithKeys where +import (implicit) qualified DA.Internal.Record +import (implicit) qualified GHC.Types +import (implicit) qualified DA.Internal.Desugar +import (implicit) DA.Internal.RebindableSyntax +f x = 12 +data GHC.Types.DamlTemplate => T + = T {p : Party, q : Party} + deriving (DA.Internal.Desugar.Eq, DA.Internal.Desugar.Show) +instance DA.Internal.Record.GetField "p" T Party where + getField = DA.Internal.Record.getFieldPrim @"p" @T @Party +instance DA.Internal.Record.SetField "p" T Party where + setField = DA.Internal.Record.setFieldPrim @"p" @T @Party +instance DA.Internal.Record.GetField "q" T Party where + getField = DA.Internal.Record.getFieldPrim @"q" @T @Party +instance DA.Internal.Record.SetField "q" T Party where + setField = DA.Internal.Record.setFieldPrim @"q" @T @Party +data Revoke + = Revoke {} + deriving (DA.Internal.Desugar.Eq, DA.Internal.Desugar.Show) +instance DA.Internal.Desugar.HasSignatory T where + signatory this@T {..} + = DA.Internal.Desugar.toParties (sig this) + where + _ = this +instance DA.Internal.Desugar.HasObserver T where + observer this@T {..} + = DA.Internal.Desugar.toParties (obs this) + where + _ = this +instance DA.Internal.Desugar.HasEnsure T where + ensure this@T {..} + = assertion this + where + _ = this +instance DA.Internal.Desugar.HasArchive T where + archive cid + = DA.Internal.Desugar.exercise cid DA.Internal.Desugar.Archive + where + _ = cid +instance DA.Internal.Desugar.HasCreate T where + create = GHC.Types.primitive @"UCreate" +instance DA.Internal.Desugar.HasFetch T where + fetch = GHC.Types.primitive @"UFetch" +instance DA.Internal.Desugar.HasToAnyTemplate T where + _toAnyTemplate = GHC.Types.primitive @"EToAnyTemplate" +instance DA.Internal.Desugar.HasFromAnyTemplate T where + _fromAnyTemplate = GHC.Types.primitive @"EFromAnyTemplate" +instance DA.Internal.Desugar.HasTemplateTypeRep T where + _templateTypeRep = GHC.Types.primitive @"ETemplateTypeRep" +instance DA.Internal.Desugar.HasIsInterfaceType T where + _isInterfaceType _ = DA.Internal.Desugar.False +instance DA.Internal.Desugar.HasExercise T DA.Internal.Desugar.Archive (()) where + exercise = GHC.Types.primitive @"UExercise" +instance DA.Internal.Desugar.HasToAnyChoice T DA.Internal.Desugar.Archive (()) where + _toAnyChoice = GHC.Types.primitive @"EToAnyChoice" +instance DA.Internal.Desugar.HasFromAnyChoice T DA.Internal.Desugar.Archive (()) where + _fromAnyChoice = GHC.Types.primitive @"EFromAnyChoice" +instance DA.Internal.Desugar.HasChoiceController T DA.Internal.Desugar.Archive where + _choiceController = GHC.Types.primitive @"EChoiceController" +instance DA.Internal.Desugar.HasChoiceObserver T DA.Internal.Desugar.Archive where + _choiceObserver = GHC.Types.primitive @"EChoiceObserver" +instance DA.Internal.Desugar.HasExercise T Revoke (()) where + exercise = GHC.Types.primitive @"UExercise" +instance DA.Internal.Desugar.HasToAnyChoice T Revoke (()) where + _toAnyChoice = GHC.Types.primitive @"EToAnyChoice" +instance DA.Internal.Desugar.HasFromAnyChoice T Revoke (()) where + _fromAnyChoice = GHC.Types.primitive @"EFromAnyChoice" +instance DA.Internal.Desugar.HasChoiceController T Revoke where + _choiceController = GHC.Types.primitive @"EChoiceController" +instance DA.Internal.Desugar.HasChoiceObserver T Revoke where + _choiceObserver = GHC.Types.primitive @"EChoiceObserver" +_choice$_T$Archive : + (DA.Internal.Desugar.Consuming T, + T -> DA.Internal.Desugar.Archive -> [DA.Internal.Desugar.Party], + DA.Internal.Desugar.Optional (T + -> DA.Internal.Desugar.Archive -> [DA.Internal.Desugar.Party]), + DA.Internal.Desugar.Optional (T + -> DA.Internal.Desugar.Archive -> [DA.Internal.Desugar.Party]), + DA.Internal.Desugar.ContractId T + -> T + -> DA.Internal.Desugar.Archive -> DA.Internal.Desugar.Update (())) +_choice$_T$Archive + = (DA.Internal.Desugar.Consuming, + \ this _ -> DA.Internal.Desugar.signatory this, + DA.Internal.Desugar.None, DA.Internal.Desugar.None, + \ _ _ _ -> pure ()) +_choice$_T$Revoke : + (DA.Internal.Desugar.Consuming T, + T -> Revoke -> [DA.Internal.Desugar.Party], + DA.Internal.Desugar.Optional (T + -> Revoke -> [DA.Internal.Desugar.Party]), + DA.Internal.Desugar.Optional (T + -> Revoke -> [DA.Internal.Desugar.Party]), + DA.Internal.Desugar.ContractId T + -> T -> Revoke -> DA.Internal.Desugar.Update (())) +_choice$_T$Revoke + = (DA.Internal.Desugar.Consuming, + \ this@T {..} arg@Revoke + -> let _ = this in + let _ = arg in DA.Internal.Desugar.toParties (p), + DA.Internal.Desugar.None, DA.Internal.Desugar.None, + \ self this@T {..} arg@Revoke + -> let _ = self in + let _ = this in let _ = arg in do pure (revokeRetVal this)) +instance DA.Internal.Desugar.HasExerciseByKey T (Party, + Text) DA.Internal.Desugar.Archive (()) where + _exerciseByKey = GHC.Types.primitive @"UExerciseByKey" +instance DA.Internal.Desugar.HasExerciseByKey T (Party, + Text) Revoke (()) where + _exerciseByKey = GHC.Types.primitive @"UExerciseByKey" +instance DA.Internal.Desugar.HasKey T (Party, Text) where + key this@T {..} + = userWrittenTuple (sig this, ident this) + where + _ = this +instance DA.Internal.Desugar.HasMaintainer T (Party, Text) where + _maintainer _ key + = DA.Internal.Desugar.toParties + ((DA.Internal.Record.getField @"_1" key)) + where + _ = key +instance DA.Internal.Desugar.HasFetchByKey T (Party, Text) where + fetchByKey = GHC.Types.primitive @"UFetchByKey" +instance DA.Internal.Desugar.HasLookupByKey T (Party, Text) where + lookupByKey = GHC.Types.primitive @"ULookupByKey" +instance DA.Internal.Desugar.HasToAnyContractKey T (Party, + Text) where + _toAnyContractKey = GHC.Types.primitive @"EToAnyContractKey" +instance DA.Internal.Desugar.HasFromAnyContractKey T (Party, + Text) where + _fromAnyContractKey = GHC.Types.primitive @"EFromAnyContractKey" +revokeRetVal : T -> () +revokeRetVal _ = () +assertion : T -> Bool +assertion _ = True +sig : T -> Party +sig T {p} = p +obs : T -> Party +obs T {q} = q +plainEnglish : T -> Text +plainEnglish _ = "Chop wood, carry water." +ident : T -> Text +ident _ = "123" diff --git a/sdk/compiler/damlc/tests/daml-test-files/UnusedMatchTestsWithKeys.daml b/sdk/compiler/damlc/tests/daml-test-files/UnusedMatchTestsWithKeys.daml new file mode 100644 index 000000000000..d99711c92156 --- /dev/null +++ b/sdk/compiler/damlc/tests/daml-test-files/UnusedMatchTestsWithKeys.daml @@ -0,0 +1,55 @@ +-- Copyright (c) 2020, Digital Asset (Switzerland) GmbH and/or its +-- affiliates. All rights reserved. + +-- @SUPPORTS-LF-FEATURE DAML_CONTRACT_KEYS +-- @WARN range=20:3-20:4; Defined but not used + +{-# OPTIONS_GHC -Wunused-matches #-} +{-# OPTIONS_GHC -Wunused-foralls #-} +{-# OPTIONS_GHC -Wunused-imports #-} +{-# OPTIONS_GHC -Wunused-pattern-binds #-} +{-# OPTIONS_GHC -Wunused-top-binds #-} +{-# OPTIONS_GHC -Wunused-type-patterns #-} +-- We know this will fail and why. +-- {-# OPTIONS_GHC -Wunused-local-binds #-} +module UnusedMatchTestsWithKeys where + +-- It should be OK to enable -Wunused-* and not get warnings from +-- template desugared code. + +f x = 12 -- Defined but not used 'x'; prove unused match detection. + +template T + with + p : Party + q : Party + where + -- None of the below should generate defined but not used + -- warnings. + signatory sig this + observer obs this + ensure assertion this + key (sig this, ident this): (Party, Text) + maintainer key._1 + choice Revoke: () with + controller p + do + pure (revokeRetVal this) + +revokeRetVal : T -> () +revokeRetVal _ = () + +assertion : T -> Bool +assertion _ = True + +sig : T -> Party +sig T {p} = p + +obs : T -> Party +obs T {q} = q + +plainEnglish : T -> Text +plainEnglish _ = "Chop wood, carry water." + +ident : T -> Text +ident _ = "123" diff --git a/sdk/compiler/damlc/tests/src/DA/Test/DamlcUpgrades.hs b/sdk/compiler/damlc/tests/src/DA/Test/DamlcUpgrades.hs index 1a5d488b13b4..cf2806f7d8d5 100644 --- a/sdk/compiler/damlc/tests/src/DA/Test/DamlcUpgrades.hs +++ b/sdk/compiler/damlc/tests/src/DA/Test/DamlcUpgrades.hs @@ -7,6 +7,7 @@ module DA.Test.DamlcUpgrades (main) where import Control.Monad.Extra import DA.Bazel.Runfiles +import qualified DA.Daml.LF.Ast.Version as LF import Data.Foldable import System.Directory.Extra import System.FilePath @@ -17,6 +18,7 @@ import Test.Tasty.HUnit import SdkVersion (SdkVersioned, sdkVersion, withSdkVersions) import Text.Regex.TDFA import qualified Data.Text as T +import Safe (fromJustNote) main :: IO () main = withSdkVersions $ do @@ -30,6 +32,7 @@ tests damlc = [ test "Warns when template changes signatories" (SucceedWithWarning "\ESC\\[0;93mwarning while type checking template MyLib.A signatories:\n The upgraded template A has changed the definition of its signatories.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -53,6 +56,7 @@ tests damlc = , test "Warns when template changes observers" (SucceedWithWarning "\ESC\\[0;93mwarning while type checking template MyLib.A observers:\n The upgraded template A has changed the definition of its observers.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -80,6 +84,7 @@ tests damlc = , test "Warns when template changes ensure" (SucceedWithWarning "\ESC\\[0;93mwarning while type checking template MyLib.A precondition:\n The upgraded template A has changed the definition of its precondition.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -107,6 +112,7 @@ tests damlc = , test "Warns when template changes key expression" (SucceedWithWarning "\ESC\\[0;93mwarning while type checking template MyLib.A key:\n The upgraded template A has changed the expression for computing its key.") + contractKeysMinVersion [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -136,6 +142,7 @@ tests damlc = , test "Warns when template changes key maintainers" (SucceedWithWarning "\ESC\\[0;93mwarning while type checking template MyLib.A key:\n The upgraded template A has changed the maintainers for its key.") + contractKeysMinVersion [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -165,6 +172,7 @@ tests damlc = , test "Fails when template changes key type" (FailWithError "\ESC\\[0;91merror type checking template MyLib.A key:\n The upgraded template A cannot change its key type.") + contractKeysMinVersion [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -194,6 +202,7 @@ tests damlc = , test "Fails when template removes key type" (FailWithError "\ESC\\[0;91merror type checking template MyLib.A key:\n The upgraded template A cannot remove its key.") + contractKeysMinVersion [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -221,6 +230,7 @@ tests damlc = , test "Fails when template adds key type" (FailWithError "\ESC\\[0;91merror type checking template MyLib.A key:\n The upgraded template A cannot add a key where it didn't have one previously.") + contractKeysMinVersion [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -248,6 +258,7 @@ tests damlc = , test "Fails when new field is added to template without Optional type" (FailWithError "\ESC\\[0;91merror type checking template MyLib.A :\n The upgraded template A has added new fields, but those fields are not Optional.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -274,6 +285,7 @@ tests damlc = , test "Fails when old field is deleted from template" (FailWithError "\ESC\\[0;91merror type checking template MyLib.A :\n The upgraded template A is missing some of its original fields.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -298,6 +310,7 @@ tests damlc = , test "Fails when existing field in template is changed" (FailWithError "\ESC\\[0;91merror type checking template MyLib.A :\n The upgraded template A has changed the types of some of its original fields.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -323,6 +336,7 @@ tests damlc = , test "Succeeds when new field with optional type is added to template" Succeed + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -349,6 +363,7 @@ tests damlc = , test "Fails when new field is added to template choice without Optional type" (FailWithError "\ESC\\[0;91merror type checking template MyLib.A choice C:\n The upgraded input type of choice C on template A has added new fields, but those fields are not Optional.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -385,6 +400,7 @@ tests damlc = , test "Fails when old field is deleted from template choice" (FailWithError "\ESC\\[0;91merror type checking template MyLib.A choice C:\n The upgraded input type of choice C on template A is missing some of its original fields.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -419,6 +435,7 @@ tests damlc = , test "Fails when existing field in template choice is changed" (FailWithError "\ESC\\[0;91merror type checking template MyLib.A choice C:\n The upgraded input type of choice C on template A has changed the types of some of its original fields.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -454,6 +471,7 @@ tests damlc = , test "Warns when controllers of template choice are changed" (SucceedWithWarning "\ESC\\[0;93mwarning while type checking template MyLib.A choice C:\n The upgraded choice C has changed the definition of controllers.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -485,6 +503,7 @@ tests damlc = , test "Warns when observers of template choice are changed" (SucceedWithWarning "\ESC\\[0;93mwarning while type checking template MyLib.A choice C:\n The upgraded choice C has changed the definition of observers.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -518,6 +537,7 @@ tests damlc = , test "Fails when template choice changes its return type" (FailWithError "\ESC\\[0;91merror type checking template MyLib.A choice C:\n The upgraded choice C cannot change its return type.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -553,6 +573,7 @@ tests damlc = , test "Succeeds when template choice returns a template which has changed" Succeed + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -583,6 +604,7 @@ tests damlc = , test "Succeeds when template choice input argument has changed" Succeed + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -617,6 +639,7 @@ tests damlc = , test "Succeeds when new field with optional type is added to template choice" Succeed + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -653,6 +676,7 @@ tests damlc = , test "Fails when a top-level record adds a non-optional field" (FailWithError "\ESC\\[0;91merror type checking data type MyLib.A:\n The upgraded data type A has added new fields, but those fields are not Optional.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -670,6 +694,7 @@ tests damlc = , test "Succeeds when a top-level record adds an optional field at the end" Succeed + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -687,6 +712,7 @@ tests damlc = , test "Fails when a top-level record adds an optional field before the end" (FailWithError "\ESC\\[0;91merror type checking data type MyLib.A:\n The upgraded data type A has changed the order of its fields - any new fields must be added at the end of the record.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -704,6 +730,7 @@ tests damlc = , test "Succeeds when a top-level variant adds a variant" Succeed + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -721,6 +748,7 @@ tests damlc = , test "Fails when a top-level variant removes a variant" (FailWithError "\ESC\\[0;91merror type checking :\n Data type A.Z appears in package that is being upgraded, but does not appear in this package.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -738,6 +766,7 @@ tests damlc = , test "Fail when a top-level variant changes changes the order of its variants" (FailWithError "\ESC\\[0;91merror type checking data type MyLib.A:\n The upgraded data type A has changed the order of its variants - any new variant must be added at the end of the variant.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -755,6 +784,7 @@ tests damlc = , test "Fails when a top-level variant adds a field to a variant's type" (FailWithError "\ESC\\[0;91merror type checking data type MyLib.A:\n The upgraded variant constructor Y from variant A has added a field.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -772,6 +802,7 @@ tests damlc = , test "Succeeds when a top-level variant adds an optional field to a variant's type" Succeed + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -789,6 +820,7 @@ tests damlc = , test "Succeed when a top-level enum adds a field" Succeed + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -806,6 +838,7 @@ tests damlc = , test "Fail when a top-level enum changes changes the order of its variants" (FailWithError "\ESC\\[0;91merror type checking data type MyLib.A:\n The upgraded data type A has changed the order of its variants - any new variant must be added at the end of the enum.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -823,6 +856,7 @@ tests damlc = , test "Succeeds when a top-level type synonym changes" Succeed + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -844,6 +878,7 @@ tests damlc = , test "Succeeds when two deeply nested type synonyms resolve to the same datatypes" Succeed + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -872,6 +907,7 @@ tests damlc = , test "Fails when two deeply nested type synonyms resolve to different datatypes" (FailWithError "\ESC\\[0;91merror type checking template MyLib.A :\n The upgraded template A has changed the types of some of its original fields.") + LF.versionDefault [ ( "daml/MyLib.daml" , unlines [ "module MyLib where" @@ -900,20 +936,26 @@ tests damlc = ] ] where + contractKeysMinVersion :: LF.Version + contractKeysMinVersion = + fromJustNote + "Expected at least one LF 2.x version to support contract keys." + (LF.featureMinVersion LF.featureContractKeys LF.V2) test :: String -> Expectation + -> LF.Version -> [(FilePath, String)] -> [(FilePath, String)] -> TestTree - test name expectation oldVersion newVersion = + test name expectation lfVersion oldVersion newVersion = testCase name $ withTempDir $ \dir -> do let depDir = dir "oldVersion" let dar = dir "out.dar" let depDar = dir "oldVersion" "dep.dar" - writeFiles dir (projectFile "mylib-v2" (Just depDar) : newVersion) - writeFiles depDir (projectFile "mylib-v1" Nothing : oldVersion) + writeFiles dir (projectFile lfVersion "mylib-v2" (Just depDar) : newVersion) + writeFiles depDir (projectFile lfVersion "mylib-v1" Nothing : oldVersion) callProcessSilent damlc ["build", "--project-root", depDir, "-o", depDar] case expectation of Succeed -> @@ -934,7 +976,7 @@ tests damlc = createDirectoryIfMissing True (takeDirectory $ dir file) writeFileUTF8 (dir file) content - projectFile name upgradedFile = + projectFile lfVersion name upgradedFile = ( "daml.yaml" , unlines $ [ "sdk-version: " <> sdkVersion @@ -946,6 +988,7 @@ tests damlc = , " - daml-stdlib" , "typecheck-upgrades: true" , "build-options:" + , " - --target=" <> LF.renderVersion lfVersion ] ++ ["upgrades: '" <> path <> "'" | Just path <- pure upgradedFile] ) diff --git a/sdk/daml-lf/engine/BUILD.bazel b/sdk/daml-lf/engine/BUILD.bazel index 76dfdb64e209..6d1e0da13001 100644 --- a/sdk/daml-lf/engine/BUILD.bazel +++ b/sdk/daml-lf/engine/BUILD.bazel @@ -96,10 +96,11 @@ da_scala_test_suite( major = major, ) for name in [ - "//daml-lf/tests:Exceptions", + # TODO(https://github.com/digital-asset/daml/issues/18457): split + # //daml-lf/tests:Exceptions into templates that use keys and those + # that don't. Split the corresponding test, and add it back here. "//daml-lf/tests:Interfaces", "//daml-lf/tests:InterfaceViews", - "//daml-lf/tests:MultiKeys", "//daml-lf/tests:ReinterpretTests", ] for major in SUPPORTED_LF_MAJOR_VERSIONS diff --git a/sdk/daml-lf/engine/src/test/daml/BasicTests.daml b/sdk/daml-lf/engine/src/test/daml/BasicTests.daml index 08251d9d683a..ebe92b5b057d 100644 --- a/sdk/daml-lf/engine/src/test/daml/BasicTests.daml +++ b/sdk/daml-lf/engine/src/test/daml/BasicTests.daml @@ -1,7 +1,7 @@ -- Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. -- SPDX-License-Identifier: Apache-2.0 - +{-# LANGUAGE CPP #-} module BasicTests where @@ -70,6 +70,7 @@ template CallablePayout controller receiver do create this with receiver = newReceiver +#ifdef DAML_CONTRACT_KEYS template WithKey with p: Party k: Int @@ -132,6 +133,7 @@ template ExerciseByKey controller p do exerciseByKey @WithKey (p, 42) SumToK with n = 0 +#endif -- Tests for the dynamic computation of fetch actors template Fetched @@ -345,6 +347,7 @@ template TimeGetter with controller p do pure $ product [1, 2, 3] +#ifdef DAML_CONTRACT_KEYS template ComputeContractKeyAfterEnsureClause with owner: Party where @@ -376,3 +379,4 @@ template NoMaintainer controller sig do pure () +#endif \ No newline at end of file diff --git a/sdk/daml-lf/engine/src/test/daml/Demonstrator.daml b/sdk/daml-lf/engine/src/test/daml/Demonstrator.daml index 2a00ed2e1ad8..1991263abf09 100644 --- a/sdk/daml-lf/engine/src/test/daml/Demonstrator.daml +++ b/sdk/daml-lf/engine/src/test/daml/Demonstrator.daml @@ -1,6 +1,8 @@ -- Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. -- SPDX-License-Identifier: Apache-2.0 +{-# LANGUAGE CPP #-} + module Demonstrator where template RegistrarRole @@ -48,5 +50,7 @@ template CancelledTransfer signatory operator observer investor +#ifdef DAML_CONTRACT_KEYS key operator : Party maintainer key +#endif diff --git a/sdk/daml-lf/tests/BUILD.bazel b/sdk/daml-lf/tests/BUILD.bazel index 07c2d6ebd315..4baea4693357 100644 --- a/sdk/daml-lf/tests/BUILD.bazel +++ b/sdk/daml-lf/tests/BUILD.bazel @@ -15,15 +15,18 @@ load( ) load("@os_info//:os_info.bzl", "is_intel") -[ - daml_compile( - name = "Exceptions-v{}".format(major), - srcs = ["Exceptions.daml"], - target = lf_version_default_or_latest(major), - visibility = ["//daml-lf:__subpackages__"], - ) - for major in SUPPORTED_LF_MAJOR_VERSIONS -] +# TODO(https://github.com/digital-asset/daml/issues/18457): split +# Exceptions.daml into templates that use keys and those that don't. Split the +# corresponding test, and re-enable this target. +# [ +# daml_compile( +# name = "Exceptions-v{}".format(major), +# srcs = ["Exceptions.daml"], +# target = lf_version_default_or_latest(major), +# visibility = ["//daml-lf:__subpackages__"], +# ) +# for major in LF_MAJOR_VERSIONS +# ] [ daml_compile( @@ -77,16 +80,6 @@ daml_compile( visibility = ["//daml-lf:__subpackages__"], ) if is_intel else None -[ - daml_compile( - name = "MultiKeys-v{}".format(major), - srcs = ["MultiKeys.daml"], - target = lf_version_default_or_latest(major), - visibility = ["//daml-lf:__subpackages__"], - ) - for major in SUPPORTED_LF_MAJOR_VERSIONS -] - [ daml_compile( name = "MultiKeys-{}".format(mangle_for_damlc(version)), diff --git a/sdk/daml-lf/tests/Exceptions.daml b/sdk/daml-lf/tests/Exceptions.daml index 6d43f530ab0c..0679be6ad310 100644 --- a/sdk/daml-lf/tests/Exceptions.daml +++ b/sdk/daml-lf/tests/Exceptions.daml @@ -1,6 +1,10 @@ -- Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. -- SPDX-License-Identifier: Apache-2.0 +-- TODO(https://github.com/digital-asset/daml/issues/18457): split this daml +-- file into tempates that use keys and those that don't. Split the +-- corresponding test. + module Exceptions where import DA.Assert diff --git a/sdk/daml-script/test/BUILD.bazel b/sdk/daml-script/test/BUILD.bazel index 7313fe4fc876..596bfd3c9ac9 100644 --- a/sdk/daml-script/test/BUILD.bazel +++ b/sdk/daml-script/test/BUILD.bazel @@ -14,6 +14,7 @@ load("@os_info//:os_info.bzl", "is_windows") load("//rules_daml:daml.bzl", "daml_compile") load( "//daml-lf/language:daml-lf.bzl", + "LF_DEFAULT_DEV_VERSION", "SUPPORTED_LF_DEV_VERSIONS", "SUPPORTED_LF_MAJOR_VERSIONS", "lf_version_default_or_latest", @@ -84,10 +85,15 @@ EOF tools = ["//compiler/damlc"], visibility = ["//visibility:public"], ) - for (target, name) in [( - lf_version_default_or_latest(major), - major, - ) for major in SUPPORTED_LF_MAJOR_VERSIONS] + [ + for (target, name) in + # TODO(https://github.com/digital-asset/daml/issues/18457): re-enable once + # script tests have been split into those that use keys and those that + # don't. + #[( + # lf_version_default_or_latest(major), + # major, + #) for major in SUPPORTED_LF_MAJOR_VERSIONS] + + [ (target, target) for target in SUPPORTED_LF_DEV_VERSIONS ] @@ -141,7 +147,7 @@ genrule( name = "script-test-no-ledger", srcs = glob(["**/*.daml"]) + [ - "//daml-script/daml3:daml3-script.dar", + "//daml-script/daml3:daml3-script-{}.dar".format(LF_DEFAULT_DEV_VERSION), "//docs:source/daml-script/template-root/src/ScriptExample.daml", ], outs = ["script-test-no-ledger.dar"], @@ -150,7 +156,7 @@ genrule( TMP_DIR=$$(mktemp -d) mkdir -p $$TMP_DIR/daml cp -L $(location :daml/ScriptTest.daml) $$TMP_DIR/daml - cp -L $(location //daml-script/daml3:daml3-script.dar) $$TMP_DIR/ + cp -L $(location //daml-script/daml3:daml3-script-{target}.dar) $$TMP_DIR/ cat << EOF > $$TMP_DIR/daml.yaml sdk-version: {sdk} name: script-test-no-ledger @@ -161,13 +167,13 @@ build-options: dependencies: - daml-stdlib - daml-prim - - daml3-script.dar + - daml3-script-{target}.dar EOF $(location //compiler/damlc) build --project-root=$$TMP_DIR --ghc-option=-Werror -o $$PWD/$(location script-test-no-ledger.dar) rm -rf $$TMP_DIR """.format( sdk = sdk_version, - target = lf_version_default_or_latest("2"), + target = LF_DEFAULT_DEV_VERSION, ), tools = ["//compiler/damlc"], visibility = ["//visibility:public"], @@ -369,17 +375,20 @@ da_scala_test_suite( ":script3-test-v2.dev.dar", "//compiler/damlc", "//daml-script/runner:daml-script-binary", - ] + [ - ":script{scriptVersion}-test-v{major}.dar".format( - major = major, - scriptVersion = scriptVersion, - ) - for major in SUPPORTED_LF_MAJOR_VERSIONS - for scriptVersion in [ - "", - "3", - ] ], + # TODO(https://github.com/digital-asset/daml/issues/18457): re-enable once + # script tests have been split into those that use keys and those that + # don't. + #+ [ + # ":script{scriptVersion}-test-v{major}.dar".format( + # major = major, + # scriptVersion = scriptVersion, + # ) + # for major in SUPPORTED_LF_MAJOR_VERSIONS + # for scriptVersion in [ + # "", + # "3", + # ] resources = glob(["src/main/resources/**/*"]), scala_deps = [ "@maven//:org_apache_pekko_pekko_actor", diff --git a/sdk/docs/BUILD.bazel b/sdk/docs/BUILD.bazel index 99123dfadb25..5d8c05748f39 100644 --- a/sdk/docs/BUILD.bazel +++ b/sdk/docs/BUILD.bazel @@ -400,8 +400,9 @@ java_binary( daml_test( name = "ledger-api-daml-test", srcs = glob(["source/app-dev/code-snippets/**/*.daml"]), - # TODO(https://github.com/digital-asset/daml/issues/18457): remove contract keys from the - # docs, delete the explicit target, and revert the daml-script.dar to the non-dev version. + # TODO(https://github.com/digital-asset/daml/issues/18457): remove contract + # keys from the docs, delete the explicit target, and revert the + # daml-script.dar to the non-dev version. target = "2.dev", deps = ["//daml-script/daml3:daml3-script-2.dev.dar"], ) @@ -410,9 +411,9 @@ daml_test( name = "bindings-java-daml-test", srcs = glob(["source/app-dev/bindings-java/code-snippets/**/*.daml"]), enable_interfaces = True, - # FIXME: https://github.com/digital-asset/daml/issues/12051 - # remove target, once interfaces are stable. - target = lf_version_configuration.get("latest"), + # TODO(https://github.com/digital-asset/daml/issues/18457): remove contract + # keys from the docs and delete the explicit target. + target = "2.dev", ) daml_test( @@ -577,7 +578,10 @@ daml_build_test( daml_build_test( name = "daml-upgrade-example-v1", dar_dict = { - "//daml-script/daml:daml-script.dar": "daml-script.dar", + # TODO(https://github.com/digital-asset/daml/issues/18457): remove + # contract keys from the docs and revert daml-script.dar to the non-dev + # version. + "//daml-script/daml3:daml3-script-2.dev.dar": "daml3-script.dar", }, project_dir = "source/upgrade/example/carbon-1.0.0", ) @@ -610,7 +614,10 @@ daml_build_test( ":daml-upgrade-example-v1": "path/to/carbon-1.0.0.dar", ":daml-upgrade-example-v2": "path/to/carbon-2.0.0.dar", ":daml-upgrade-example-upgrade": "path/to/carbon-upgrade-1.0.0.dar", - "//daml-script/daml:daml-script.dar": "daml-script.dar", + # TODO(https://github.com/digital-asset/daml/issues/18457): remove + # contract keys from the docs and revert daml-script.dar to the non-dev + # version. + "//daml-script/daml3:daml3-script-2.dev.dar": "daml3-script.dar", }, project_dir = "source/upgrade/example/carbon-initiate-upgrade", ) diff --git a/sdk/docs/source/upgrade/example/carbon-1.0.0/daml.yaml b/sdk/docs/source/upgrade/example/carbon-1.0.0/daml.yaml index 07606757565a..970b3d5d94c6 100644 --- a/sdk/docs/source/upgrade/example/carbon-1.0.0/daml.yaml +++ b/sdk/docs/source/upgrade/example/carbon-1.0.0/daml.yaml @@ -1,13 +1,18 @@ # Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. # SPDX-License-Identifier: Apache-2.0 +# TODO(https://github.com/digital-asset/daml/issues/18457): remove contract keys +# from the docs and delete the explicit target. + sdk-version: 0.0.0 # BEGIN name: carbon +build-options: + - --target=2.dev version: 1.0.0 dependencies: - daml-prim - daml-stdlib - - daml-script + - daml3-script source: . # END diff --git a/sdk/docs/source/upgrade/example/carbon-2.0.0/daml.yaml b/sdk/docs/source/upgrade/example/carbon-2.0.0/daml.yaml index 9f06da26a8f3..c60963623e4e 100644 --- a/sdk/docs/source/upgrade/example/carbon-2.0.0/daml.yaml +++ b/sdk/docs/source/upgrade/example/carbon-2.0.0/daml.yaml @@ -1,10 +1,15 @@ # Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. # SPDX-License-Identifier: Apache-2.0 +# TODO(https://github.com/digital-asset/daml/issues/18457): remove contract keys +# from the docs and delete the explicit target. + sdk-version: 0.0.0 # BEGIN name: carbon version: 2.0.0 +build-options: + - --target=2.dev dependencies: - daml-prim - daml-stdlib diff --git a/sdk/docs/source/upgrade/example/carbon-initiate-upgrade/daml.yaml b/sdk/docs/source/upgrade/example/carbon-initiate-upgrade/daml.yaml index 40932f8b0abf..668b91bbf11e 100644 --- a/sdk/docs/source/upgrade/example/carbon-initiate-upgrade/daml.yaml +++ b/sdk/docs/source/upgrade/example/carbon-initiate-upgrade/daml.yaml @@ -1,14 +1,19 @@ # Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. # SPDX-License-Identifier: Apache-2.0 +# TODO(https://github.com/digital-asset/daml/issues/18457): remove contract keys +# from the docs and delete the explicit target. + sdk-version: 0.0.0 # BEGIN name: carbon-initiate-upgrade version: 1.0.0 +build-options: + - --target=2.dev dependencies: - daml-prim - daml-stdlib - - daml-script + - daml3-script data-dependencies: - path/to/carbon-upgrade-1.0.0.dar - path/to/carbon-1.0.0.dar diff --git a/sdk/docs/source/upgrade/example/carbon-label/daml.yaml b/sdk/docs/source/upgrade/example/carbon-label/daml.yaml index fda701630a2c..71df69ea6817 100644 --- a/sdk/docs/source/upgrade/example/carbon-label/daml.yaml +++ b/sdk/docs/source/upgrade/example/carbon-label/daml.yaml @@ -1,10 +1,15 @@ # Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. # SPDX-License-Identifier: Apache-2.0 +# TODO(https://github.com/digital-asset/daml/issues/18457): remove contract keys +# from the docs and delete the explicit target. + sdk-version: 0.0.0 # DAML_YAML_BEGIN name: carbon-label version: 1.0.0 +build-options: + - --target=2.dev dependencies: - daml-prim - daml-stdlib diff --git a/sdk/docs/source/upgrade/example/carbon-upgrade/daml.yaml b/sdk/docs/source/upgrade/example/carbon-upgrade/daml.yaml index 118586cf5297..fc4ceeb97e75 100644 --- a/sdk/docs/source/upgrade/example/carbon-upgrade/daml.yaml +++ b/sdk/docs/source/upgrade/example/carbon-upgrade/daml.yaml @@ -1,10 +1,15 @@ # Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. # SPDX-License-Identifier: Apache-2.0 +# TODO(https://github.com/digital-asset/daml/issues/18457): remove contract keys +# from the docs and delete the explicit target. + sdk-version: 0.0.0 # BEGIN name: carbon-upgrade version: 1.0.0 +build-options: + - --target=2.dev dependencies: - daml-prim - daml-stdlib