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

LF: Drop Builtin Exceptions completly #9790

Merged
merged 5 commits into from
May 26, 2021
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
1 change: 0 additions & 1 deletion compiler/daml-lf-ast/src/DA/Daml/LF/Ast/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ data BuiltinExpr
-- Exceptions
| BEError -- :: ∀a. Text -> a
| BEAnyExceptionMessage -- :: AnyException -> Text
| BEAnyExceptionIsArithmeticError -- :: AnyException -> Bool

-- Polymorphic functions
| BEEqualGeneric -- :: ∀t. t -> t -> Bool
Expand Down
1 change: 0 additions & 1 deletion compiler/daml-lf-ast/src/DA/Daml/LF/Ast/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ instance Pretty BuiltinExpr where
BERoundingMode r -> keyword_ $ prettyRounding r
BEError -> "ERROR"
BEAnyExceptionMessage -> "ANY_EXCEPTION_MESSAGE"
BEAnyExceptionIsArithmeticError -> "ANY_EXCEPTION_IS_ARITHMETIC_ERROR"
BEEqualGeneric -> "EQUAL"
BELessGeneric -> "LESS"
BELessEqGeneric -> "LESS_EQ"
Expand Down
1 change: 0 additions & 1 deletion compiler/daml-lf-proto/src/DA/Daml/LF/Proto3/DecodeV1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,6 @@ decodeBuiltinFunction = pure . \case

LF1.BuiltinFunctionERROR -> BEError
LF1.BuiltinFunctionANY_EXCEPTION_MESSAGE -> BEAnyExceptionMessage
LF1.BuiltinFunctionANY_EXCEPTION_IS_ARITHMETIC_ERROR -> BEAnyExceptionIsArithmeticError

LF1.BuiltinFunctionTEXTMAP_EMPTY -> BETextMapEmpty
LF1.BuiltinFunctionTEXTMAP_INSERT -> BETextMapInsert
Expand Down
1 change: 0 additions & 1 deletion compiler/daml-lf-proto/src/DA/Daml/LF/Proto3/EncodeV1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,6 @@ encodeBuiltinExpr = \case

BEError -> builtin P.BuiltinFunctionERROR
BEAnyExceptionMessage -> builtin P.BuiltinFunctionANY_EXCEPTION_MESSAGE
BEAnyExceptionIsArithmeticError -> builtin P.BuiltinFunctionANY_EXCEPTION_IS_ARITHMETIC_ERROR

BETextMapEmpty -> builtin P.BuiltinFunctionTEXTMAP_EMPTY
BETextMapInsert -> builtin P.BuiltinFunctionTEXTMAP_INSERT
Expand Down
1 change: 0 additions & 1 deletion compiler/daml-lf-tools/src/DA/Daml/LF/Simplifier.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ safetyStep = \case
BERoundingMode _ -> Safe 0
BEError -> Safe 0
BEAnyExceptionMessage -> Safe 0 -- evaluates user-defined code which may throw
BEAnyExceptionIsArithmeticError -> Safe 1
BEEqualGeneric -> Safe 1 -- may crash if values are incomparable
BELessGeneric -> Safe 1 -- may crash if values are incomparable
BELessEqGeneric -> Safe 1 -- may crash if values are incomparable
Expand Down
1 change: 0 additions & 1 deletion compiler/daml-lf-tools/src/DA/Daml/LF/TypeChecker/Check.hs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ typeOfBuiltin = \case
BERoundingMode _ -> pure TRoundingMode
BEError -> pure $ TForall (alpha, KStar) (TText :-> tAlpha)
BEAnyExceptionMessage -> pure $ TAnyException :-> TText
BEAnyExceptionIsArithmeticError -> pure $ TAnyException :-> TBool
BEEqualGeneric -> pure $ TForall (alpha, KStar) (tAlpha :-> tAlpha :-> TBool)
BELessGeneric -> pure $ TForall (alpha, KStar) (tAlpha :-> tAlpha :-> TBool)
BELessEqGeneric -> pure $ TForall (alpha, KStar) (tAlpha :-> tAlpha :-> TBool)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,6 @@ convertPrim version "EToAnyContractKey"
-- Exceptions
convertPrim _ "BEAnyExceptionMessage" (TBuiltin BTAnyException :-> TText) =
EBuiltin BEAnyExceptionMessage
convertPrim _ "BEAnyExceptionIsArithmeticError" (TBuiltin BTAnyException :-> TBool) =
EBuiltin BEAnyExceptionIsArithmeticError

convertPrim _ "EThrow" (ty1 :-> ty2) =
ETmLam (mkVar "x", ty1) (EThrow ty2 ty1 (EVar (mkVar "x")))
Expand Down
6 changes: 1 addition & 5 deletions compiler/damlc/daml-stdlib-src/DA/Internal/Exception.daml
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,7 @@ instance HasToAnyException ArithmeticError where
toAnyException = primitive @"EToAnyException"

instance HasFromAnyException ArithmeticError where
fromAnyException e
| primitive @"BEAnyExceptionIsArithmeticError" e =
Some (ArithmeticError (message e))
| otherwise =
primitive @"EFromAnyException" e
fromAnyException = primitive @"EFromAnyException"

--------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

-- @SINCE-LF-FEATURE DAML_EXCEPTIONS
-- @ERROR range=130:1-130:23; Unhandled exception: ExceptionSemantics:E
-- @ERROR range=145:1-145:25; Unhandled exception: ArithmeticError while evaluating (DIV_INT64 1 0).
-- @ERROR range=145:1-145:25; Unhandled exception: DA.Exception.ArithmeticError:ArithmeticError@f1cf1ff41057ce327248684089b106d0a1f27c2f092d30f663c919addf173981 with message = "ArithmeticError while evaluating (DIV_INT64 1 0)."
-- @ERROR range=182:1-182:11; Attempt to exercise a consumed contract
module ExceptionSemantics where

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,7 @@ prettyScenarioErrorError (Just err) = do
case err of
ScenarioErrorErrorCrash reason -> pure $ text "CRASH:" <-> ltext reason
ScenarioErrorErrorUserError reason -> pure $ text "Aborted: " <-> ltext reason
ScenarioErrorErrorUnhandledException (ScenarioError_Exception maybeException) ->
case maybeException of
Nothing -> pure $ text "<missing unhandled exception details>"
Just (ScenarioError_ExceptionErrorBuiltin message) -> pure $ text $ "Unhandled exception: " <> TL.toStrict message
Just (ScenarioError_ExceptionErrorUser value) -> pure $ text "Unhandled exception: " <-> prettyValue' True 0 world value
ScenarioErrorErrorUnhandledException exc -> pure $ text "Unhandled exception: " <-> prettyValue' True 0 world exc
ScenarioErrorErrorTemplatePrecondViolated ScenarioError_TemplatePreconditionViolated{..} -> do
pure $
"Template precondition violated in:"
Expand Down
9 changes: 1 addition & 8 deletions compiler/scenario-service/protos/scenario_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,6 @@ message ScenarioError {
sfixed64 effective_at = 2;
}

message Exception {
oneof error {
Value user = 1;
string builtin = 2;
}
}

// The state of the ledger at the time of the error
repeated ScenarioStep scenario_steps = 1;
repeated Node nodes = 2;
Expand Down Expand Up @@ -248,7 +241,7 @@ message ScenarioError {
string user_error = 13;

// Unhandled exceptions
Exception unhandled_exception = 26;
Value unhandled_exception = 26;

// Errors related to update interpretation.
TemplatePreconditionViolated template_precond_violated = 14;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,15 @@ final class Conversions(

err match {
case SError.SErrorCrash(reason) => setCrash(reason)

case SError.SRequiresOnLedger(operation) => setCrash(operation)

case SError.DamlEMatchError(reason) =>
setCrash(reason)
case SError.DamlEUnhandledException(exc) =>
val exceptionBuilder = proto.ScenarioError.Exception.newBuilder
exc match {
case error: SValue.SArithmeticError =>
exceptionBuilder.setBuiltin(error.message)
case SValue.SAnyException(_, sValue) =>
exceptionBuilder.setUser(convertValue(sValue.toValue))
}
builder.setUnhandledException(exceptionBuilder)
case SError.DamlEUserError(msg) =>
builder.setUserError(msg)
case SError.DamlEMatchError(reason) => setCrash(reason)

case SError.DamlEUnhandledException(excep) =>
builder.setUnhandledException(convertValue(excep.value.toValue))

case SError.DamlEUserError(msg) => builder.setUserError(msg)

case SError.DamlETransactionError(reason) =>
setCrash(reason)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,6 @@ enum BuiltinFunction {

ERROR = 25;
ANY_EXCEPTION_MESSAGE = 147; // *Available in versions >= 1.dev*
ANY_EXCEPTION_IS_ARITHMETIC_ERROR = 148; // *Available in versions >= 1.dev*

LEQ_INT64 = 33; // *Available in versions < 1.11*
LEQ_DECIMAL = 34; // *Available in versions < 1.7*
Expand Down Expand Up @@ -584,7 +583,7 @@ enum BuiltinFunction {
NUMERIC_TO_BIGNUMERIC = 145; // *Available in versions >= 1.13*
BIGNUMERIC_TO_TEXT = 146; // *Available in versions >= 1.13*

// Next id is 150. 149 is ANY_EXCEPTION_IS_CONTRACT_ERROR.
// Next id is 148. 147 is ANY_EXCEPTION_MESSAGE.

// EXPERIMENTAL TEXT PRIMITIVES -- these do not yet have stable numbers.
TEXT_TO_UPPER = 9901; // *Available in versions >= 1.dev*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1843,11 +1843,6 @@ private[lf] object DecodeV1 {
BuiltinFunctionInfo(NUMERIC_TO_BIGNUMERIC, BNumericToBigNumeric, minVersion = bigNumeric),
BuiltinFunctionInfo(BIGNUMERIC_TO_TEXT, BBigNumericToText, minVersion = bigNumeric),
BuiltinFunctionInfo(ANY_EXCEPTION_MESSAGE, BAnyExceptionMessage, minVersion = exceptions),
BuiltinFunctionInfo(
ANY_EXCEPTION_IS_ARITHMETIC_ERROR,
BAnyExceptionIsArithmeticError,
minVersion = exceptions,
),
BuiltinFunctionInfo(TEXT_TO_UPPER, BTextToUpper, minVersion = unstable),
BuiltinFunctionInfo(TEXT_TO_LOWER, BTextToLower, minVersion = unstable),
BuiltinFunctionInfo(TEXT_SLICE, BTextSlice, minVersion = unstable),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -844,8 +844,6 @@ class DecodeV1Spec
"exception builtins" -> "expected output",
DamlLf1.BuiltinFunction.ANY_EXCEPTION_MESSAGE ->
Ast.EBuiltin(Ast.BAnyExceptionMessage),
DamlLf1.BuiltinFunction.ANY_EXCEPTION_IS_ARITHMETIC_ERROR ->
Ast.EBuiltin(Ast.BAnyExceptionIsArithmeticError),
)

forEveryVersion { version =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,6 @@ private[lf] final class Compiler(
throw CompilationError(s"unexpected $bf")

case BAnyExceptionMessage => SBAnyExceptionMessage
case BAnyExceptionIsArithmeticError => SBAnyExceptionIsArithmeticError
})
}

Expand Down Expand Up @@ -1286,7 +1285,6 @@ private[lf] final class Compiler(
case SEnum(_, _, _) => ()
case SAny(_, v) => goV(v)
case SAnyException(_, v) => goV(v)
case SArithmeticError(_, _) => ()
case _: SPAP | SToken | SStruct(_, _) =>
throw CompilationError("validate: unexpected SEValue")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,8 @@ private[lf] object Pretty {
ex match {
case DamlEFailedAuthorization(nid, fa) =>
text(prettyFailedAuthorization(nid, fa))
case DamlEUnhandledException(exc) =>
text(s"unhandled exception:") & {
exc match {
case SAnyException(_, value) =>
prettyValue(true)(value.toValue)
case exception: SArithmeticError =>
text(exception.message)
}
}
case DamlEUnhandledException(SAnyException(_, value)) =>
text(s"Unhandled exception:") & prettyValue(true)(value.toValue)
case DamlEUserError(message) =>
text(s"User abort: $message")
case DamlETransactionError(reason) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,14 @@ private[speedy] sealed abstract class SBuiltin(val arity: Int) {
)
}

final protected def getSException(args: util.ArrayList[SValue], i: Int): SException =
final protected def getSAnyException(args: util.ArrayList[SValue], i: Int): SAnyException =
args.get(i) match {
case exception: SException => exception
case exception: SAnyException => exception
case otherwise =>
throw SErrorCrash(
s"${getClass.getSimpleName}: type mismatch of argument $i: expect SException but got $otherwise"
)
}

}

private[speedy] sealed abstract class SBuiltinPure(arity: Int) extends SBuiltin(arity) {
Expand Down Expand Up @@ -425,9 +424,8 @@ private[lf] object SBuiltin {
case SBigNumeric(x) => Numeric.toUnscaledString(x)
case SNumeric(x) => Numeric.toUnscaledString(x)
case STNat(n) => s"@$n"
case _: SContractId | SToken | _: SAny | _: SAnyException | _: SArithmeticError | _: SEnum |
_: SList | _: SMap | _: SOptional | _: SPAP | _: SRecord | _: SStruct | _: STypeRep |
_: SVariant =>
case _: SContractId | SToken | _: SAny | _: SAnyException | _: SEnum | _: SList | _: SMap |
_: SOptional | _: SPAP | _: SRecord | _: SStruct | _: STypeRep | _: SVariant =>
crash(s"litToText: unexpected $x")
}

Expand Down Expand Up @@ -1516,8 +1514,8 @@ private[lf] object SBuiltin {
args: util.ArrayList[SValue],
machine: Machine,
): Unit = {
val payload: SValue = args.get(0)
unwindToHandler(machine, payload)
val excep = getSAnyException(args, 0)
unwindToHandler(machine, excep)
}
}

Expand All @@ -1527,7 +1525,7 @@ private[lf] object SBuiltin {
args: util.ArrayList[SValue],
machine: Machine,
): Unit = {
val payload = args.get(1)
val excep = getSAnyException(args, 1)
val token = args.get(2)
checkToken(token)
args.get(0) match {
Expand All @@ -1536,14 +1534,9 @@ private[lf] object SBuiltin {
opt match {
case None =>
onLedger.ptx = onLedger.ptx.abortTry
unwindToHandler(machine, payload) //re-throw
unwindToHandler(machine, excep) //re-throw
case Some(handler) =>
payload match {
case ex: SException =>
onLedger.ptx = onLedger.ptx.rollbackTry(ex)
case _ =>
crash(s"SBTryHandler, expected payload to be SAnyException: $payload")
}
onLedger.ptx = onLedger.ptx.rollbackTry(excep)
machine.enterApplication(handler, Array(SEValue(token)))
}
}
Expand All @@ -1566,8 +1559,6 @@ private[lf] object SBuiltin {
args.get(0) match {
case SAnyException(actualTy, v) =>
SOptional(if (actualTy == expectedTy) Some(v) else None)
case SArithmeticError(_, _) =>
SOptional(None)
case v => crash(s"FromAnyException applied to non-AnyException: $v")
}
}
Expand All @@ -1582,7 +1573,10 @@ private[lf] object SBuiltin {
args: util.ArrayList[SValue],
machine: Machine,
): Unit = {
getSException(args, 0) match {
getSAnyException(args, 0) match {
case SArithmeticError(value) =>
// short cut for Arithmetic error
machine.returnValue = value
case SAnyException(Ast.TTyCon(tyCon), innerValue) =>
if (!machine.compiledPackages.packageIds.contains(tyCon.packageId))
throw SpeedyHungry(
Expand All @@ -1595,26 +1589,12 @@ private[lf] object SBuiltin {
)
)
exceptionMessage(tyCon, innerValue, machine)
case exception: SArithmeticError =>
machine.returnValue = SText(exception.message)
case SAnyException(ty, _) =>
crash(s"$ty is not a valid exception type")
}
}
}

/** $any-exception-is-arithmetic-error :: AnyException -> Bool */
final case object SBAnyExceptionIsArithmeticError extends SBuiltinPure(1) {
override private[speedy] final def executePure(args: util.ArrayList[SValue]): SValue = {
getSException(args, 0) match {
case SArithmeticError(_, _) =>
SValue.SValue.True
case _ =>
SValue.SValue.False
}
}
}

/** $to_any
* :: t
* -> Any (where t = ty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.daml.lf.transaction.{GlobalKey, NodeId, Transaction => Tx}
import com.daml.lf.value.Value
import com.daml.lf.value.Value.ContractId
import com.daml.lf.scenario.ScenarioLedger
import com.daml.lf.speedy.SValue.SException
import com.daml.lf.speedy.SValue.SAnyException

object SError {

Expand Down Expand Up @@ -43,7 +43,7 @@ object SError {
sealed abstract class SErrorDamlException extends SError

/** Unhandled exceptions */
final case class DamlEUnhandledException(exception: SException) extends SErrorDamlException {
final case class DamlEUnhandledException(exception: SAnyException) extends SErrorDamlException {
override def toString: String = s"Unhandled exception: $exception"
}

Expand Down
Loading