Skip to content

Commit

Permalink
LF: Fix encoder for Exception (#9904)
Browse files Browse the repository at this point in the history
* LF: Fix encoder for Exception

CHANGELOG_BEGIN
CHANGELOG_END

* Update daml-lf/encoder/src/main/scala/com/digitalasset/daml/lf/archive/testing/EncodeV1.scala

Co-authored-by: Sofia Faro <[email protected]>

Co-authored-by: Sofia Faro <[email protected]>
  • Loading branch information
remyhaemmerle-da and sofiafaro-da authored Jun 3, 2021
1 parent b22c046 commit 3dee3d0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,14 @@ private[daml] class EncodeV1(minor: LV.Minor) {
.setExceptionType(excTy)
.setExceptionExpr(exc)
)
case EToAnyException(ty, body) =>
assertSince(LV.Features.exceptions, "Expr.ToAnyException")
builder.setToAnyException(PLF.Expr.ToAnyException.newBuilder().setType(ty).setExpr(body))
case EFromAnyException(ty, body) =>
assertSince(LV.Features.exceptions, "Expr.FromAnyException")
builder.setFromAnyException(
PLF.Expr.FromAnyException.newBuilder().setType(ty).setExpr(body)
)
case EExperimental(name, ty) =>
assertSince(LV.v1_dev, "Expr.experimental")
builder.setExperimental(PLF.Expr.Experimental.newBuilder().setName(name).setType(ty))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

module BuiltinMod {

val equal: forall (a: *). a -> a -> Bool =
EQUAL;
val less_eq: forall (a: *). a -> a -> Bool =
LESS_EQ;
val less: forall (a: *). a -> a -> Bool =
LESS;
val greater_eq: forall (a: *). a -> a -> Bool =
GREATER_EQ;
val greater: forall (a: *). a -> a -> Bool =
GREATER;
module BigNumericMod {

val scaleBigNumeric: BigNumeric -> Int64 =
SCALE_BIGNUMERIC;
Expand Down
27 changes: 27 additions & 0 deletions daml-lf/encoder/src/test/lf/Exception_1.dev_.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

module ExceptionMod {

record @serializable Exception = { message: Text } ;
exception Exception = {
message \(e: ExceptionMod:Exception) ->
ExceptionMod:Exception {message} e
};

val anException: ExceptionMod:Exception =
ExceptionMod:Exception {message = "oops"};

val throwAndCatch : Update Int64 =
try @Int64 (upure @Int64 (throw @Int64 @ExceptionMod:Exception ExceptionMod:anException))
catch e -> Some @(Update Int64) (upure @Int64 77);

val anAnyException: AnyException =
to_any_exception @ExceptionMod:Exception ExceptionMod:anException;

val anUnwrappedAnyException: Option ExceptionMod:Exception =
from_any_exception @ExceptionMod:Exception ExceptionMod:anAnyException;

val aMessage: Text = ANY_EXCEPTION_MESSAGE ExceptionMod:anAnyException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ class DamlLfEncoderTest
val modules_1_7 = modules_1_6 + "NumericMod" + "AnyMod"
val modules_1_8 = modules_1_7 + "SynonymMod"
val modules_1_11 = modules_1_8 + "GenMapMod"
val modules_1_dev = modules_1_11
val modules_1_13 = modules_1_11 + "BigNumericMod"
val modules_1_dev = modules_1_13 + "ExceptionMod"

val versions = Table(
"versions" -> "modules",
"1.6" -> modules_1_6,
"1.7" -> modules_1_7,
"1.8" -> modules_1_8,
"1.11" -> modules_1_11,
"1.13" -> modules_1_13,
"1.dev" -> modules_1_dev,
)

Expand All @@ -68,7 +70,9 @@ class DamlLfEncoderTest

val findModules = dar.toOption.toList.flatMap(getNonEmptyModules).toSet

findModules shouldBe expectedModules
findModules diff expectedModules shouldBe Set()
expectedModules diff findModules shouldBe Set()

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ class EncodeV1Spec extends AnyWordSpec with Matchers with TableDrivenPropertyChe
throw @(Update Unit) @Mod:MyException (Mod:MyException {message = "oops"})
catch e -> Some @(Update Unit) (upure @Unit ())
in upure @Unit ();
val myAnyException: AnyException =
to_any_exception @Mod:MyException (Mod:MyException {message = "oops"});
val maybeException: Option Mod:MyException =
from_any_exception @Mod:MyException Mod:myAnyException;
}
"""
Expand Down

0 comments on commit 3dee3d0

Please sign in to comment.