Skip to content

Commit

Permalink
Speedy: implement Arithmetic builtin exceptions (#9653)
Browse files Browse the repository at this point in the history
part of #8020

CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
remyhaemmerle-da authored May 19, 2021
1 parent 5f954da commit 0d931b2
Show file tree
Hide file tree
Showing 17 changed files with 164 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,15 @@ final class Conversions(

case SError.DamlEMatchError(reason) =>
setCrash(reason)
case err: SError.DamlEArithmeticError =>
setCrash(err.toString)
case SError.DamlEUnhandledException(exc) =>
exc match {
case SValue.SAnyException(_, sValue) =>
builder.setUnhandledException(convertValue(sValue.toValue))
case SValue.SBuiltinException(error) =>
case error: SValue.SBuiltinException =>
// TODO https://github.com/digital-asset/daml/issues/8020
// We should not crash here.
// We however need conversion primitive for builtin exeception to be implemented.
setCrash(error.toString)
setCrash(error.message)
}
case SError.DamlEUserError(msg) =>
builder.setUserError(msg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private[engine] final class ValueTranslator(compiledPackages: CompiledPackages)
case (BTNumeric, ValueNumeric(d)) =>
typeArg0 match {
case TNat(s) =>
Numeric.fromBigDecimal(s, d).fold(fail, SValue.SNumeric)
Numeric.fromBigDecimal(s, d).fold(fail, SValue.SNumeric(_))
case _ =>
typeMismatch
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,10 @@ private[lf] object Anf {
val body: SExpr = flattenExp(depth, env, body0)(anf => Land(anf.wrapped)).bounce
Bounce(() => transform(depth, SEScopeExercise(body), k))

case x: SEAbs => throw CompilationError(s"flatten: unexpected: $x")
case x: SEDamlException => throw CompilationError(s"flatten: unexpected: $x")
case x: SEAppAtomicFun => throw CompilationError(s"flatten: unexpected: $x")
case x: SEAppAtomicGeneral => throw CompilationError(s"flatten: unexpected: $x")
case x: SEAppAtomicSaturatedBuiltin => throw CompilationError(s"flatten: unexpected: $x")
case x: SELet1Builtin => throw CompilationError(s"flatten: unexpected: $x")
case x: SECaseAtomic => throw CompilationError(s"flatten: unexpected: $x")
case _: SEAbs | _: SEDamlException | _: SEAppAtomicFun | _: SEAppAtomicGeneral |
_: SEAppAtomicSaturatedBuiltin | _: SELet1Builtin | _: SELet1BuiltinArithmetic |
_: SECaseAtomic =>
throw CompilationError(s"flatten: unexpected: $exp")
}

private[this] def atomizeExps[A](depth: DepthA, env: Env, exps: List[SExpr], k: K[AExpr, A])(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1140,12 +1140,6 @@ private[lf] final class Compiler(
val newBody = closureConvert(newRemapsF ++ newRemapsA, body)
SEMakeClo(fvs.map(remap).toArray, arity, newBody)

case x: SELoc =>
throw CompilationError(s"closureConvert: unexpected SELoc: $x")

case x: SEMakeClo =>
throw CompilationError(s"closureConvert: unexpected SEMakeClo: $x")

case SEAppGeneral(fun, args) =>
val newFun = closureConvert(remaps, fun)
val newArgs = args.map(closureConvert(remaps, _))
Expand Down Expand Up @@ -1193,26 +1187,13 @@ private[lf] final class Compiler(
case SELabelClosure(label, expr) =>
SELabelClosure(label, closureConvert(remaps, expr))

case x: SEDamlException =>
throw CompilationError(s"unexpected SEDamlException: $x")

case x: SEImportValue =>
throw CompilationError(s"unexpected SEImportValue: $x")

case x: SEAppAtomicGeneral =>
throw CompilationError(s"closureConvert: unexpected: $x")

case x: SEAppAtomicSaturatedBuiltin =>
throw CompilationError(s"closureConvert: unexpected: $x")

case SELet1General(bound, body) =>
SELet1General(closureConvert(remaps, bound), closureConvert(shift(remaps, 1), body))

case x: SELet1Builtin =>
throw CompilationError(s"closureConvert: unexpected: $x")

case x: SECaseAtomic =>
throw CompilationError(s"closureConvert: unexpected: $x")
case _: SELoc | _: SEMakeClo | _: SELet1Builtin | _: SELet1BuiltinArithmetic |
_: SEDamlException | _: SEImportValue | _: SEAppAtomicGeneral |
_: SEAppAtomicSaturatedBuiltin | _: SECaseAtomic =>
throw CompilationError(s"closureConvert: unexpected $expr")
}
}

Expand Down Expand Up @@ -1260,10 +1241,6 @@ private[lf] final class Compiler(
args.foldLeft(go(fun, bound, free))((acc, arg) => go(arg, bound, acc))
case SEAbs(n, body) =>
go(body, bound + n, free)
case x: SELoc =>
throw CompilationError(s"freeVars: unexpected SELoc: $x")
case x: SEMakeClo =>
throw CompilationError(s"freeVars: unexpected SEMakeClo: $x")
case SECase(scrut, alts) =>
alts.foldLeft(go(scrut, bound, free)) { case (acc, SCaseAlt(pat, body)) =>
go(body, bound + patternNArgs(pat), acc)
Expand All @@ -1281,16 +1258,10 @@ private[lf] final class Compiler(
case SEScopeExercise(body) =>
go(body, bound, free)

case x: SEDamlException =>
throw CompilationError(s"unexpected SEDamlException: $x")
case x: SEImportValue =>
throw CompilationError(s"unexpected SEImportValue: $x")

case x: SEAppAtomicGeneral => throw CompilationError(s"freeVars: unexpected: $x")
case x: SEAppAtomicSaturatedBuiltin => throw CompilationError(s"freeVars: unexpected: $x")
case x: SELet1General => throw CompilationError(s"freeVars: unexpected: $x")
case x: SELet1Builtin => throw CompilationError(s"freeVars: unexpected: $x")
case x: SECaseAtomic => throw CompilationError(s"freeVars: unexpected: $x")
case _: SELoc | _: SEMakeClo | _: SEDamlException | _: SEImportValue |
_: SEAppAtomicGeneral | _: SEAppAtomicSaturatedBuiltin | _: SELet1General |
_: SELet1Builtin | _: SELet1BuiltinArithmetic | _: SECaseAtomic =>
throw CompilationError(s"freeVars: unexpected $expr")
}

go(expr, initiallyBound, Set.empty)
Expand All @@ -1316,7 +1287,7 @@ private[lf] final class Compiler(
case SEnum(_, _, _) => ()
case SAny(_, v) => goV(v)
case SAnyException(_, v) => goV(v)
case SBuiltinException(ContractError | ArithmeticError) => ()
case SBuiltinException(_, _, _) => ()
case _: SPAP | SToken | SStruct(_, _) =>
throw CompilationError("validate: unexpected SEValue")
}
Expand Down Expand Up @@ -1352,10 +1323,6 @@ private[lf] final class Compiler(
case SEAppAtomicFun(fun, args) =>
go(fun)
args.foreach(go)
case x: SEVar =>
throw CompilationError(s"validate: SEVar encountered: $x")
case abs: SEAbs =>
throw CompilationError(s"validate: SEAbs encountered: $abs")
case SEMakeClo(fvs, n, body) =>
fvs.foreach(goLoc)
goBody(0, n, fvs.length)(body)
Expand All @@ -1373,6 +1340,7 @@ private[lf] final class Compiler(
goBody(maxS + bounds.length, maxA, maxF)(body)
case _: SELet1General => goLets(maxS)(expr)
case _: SELet1Builtin => goLets(maxS)(expr)
case _: SELet1BuiltinArithmetic => goLets(maxS)(expr)
case SECatchSubmitMustFail(body) =>
go(body)
case SELocation(_, body) =>
Expand All @@ -1385,10 +1353,8 @@ private[lf] final class Compiler(
case SEScopeExercise(body) =>
go(body)

case x: SEDamlException =>
throw CompilationError(s"unexpected SEDamlException: $x")
case x: SEImportValue =>
throw CompilationError(s"unexpected SEImportValue: $x")
case _: SEVar | _: SEAbs | _: SEDamlException | _: SEImportValue =>
throw CompilationError(s"validate: unexpected $expr")
}
@tailrec
def goLets(maxS: Int)(expr: SExpr): Unit = {
Expand All @@ -1400,6 +1366,9 @@ private[lf] final class Compiler(
case SELet1Builtin(_, args, body) =>
args.foreach(go)
goLets(maxS + 1)(body)
case SELet1BuiltinArithmetic(_, args, body) =>
args.foreach(go)
goLets(maxS + 1)(body)
case expr =>
go(expr)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,13 @@ private[lf] object Pretty {
ex match {
case DamlEFailedAuthorization(nid, fa) =>
text(prettyFailedAuthorization(nid, fa))
case err: DamlEArithmeticError =>
text(err.toString)
case DamlEUnhandledException(exc) =>
text(s"unhandled exception:") & {
exc match {
case SAnyException(_, value) =>
prettyValue(true)(value.toValue)
case SBuiltinException(ContractError) =>
text("ContractError")
case SBuiltinException(ArithmeticError) =>
text("ArithmeticError")
case exception: SBuiltinException =>
text(exception.message)
}
}
case DamlEUserError(message) =>
Expand Down Expand Up @@ -599,7 +595,8 @@ private[lf] object Pretty {
prettySExpr(index)(SELet(List(rhs), body))
case SELet1Builtin(builtin, args, body) =>
prettySExpr(index)(SELet1General(SEAppAtomicSaturatedBuiltin(builtin, args), body))

case SELet1BuiltinArithmetic(builtin, args, body) =>
prettySExpr(index)(SELet1General(SEAppAtomicSaturatedBuiltin(builtin, args), body))
case SETryCatch(body, handler) =>
text("try-catch") + char('(') + prettySExpr(index)(body) + text(", ") +
prettySExpr(index)(handler) + char(')')
Expand Down
Loading

0 comments on commit 0d931b2

Please sign in to comment.