daml-lf/interfaces: Drop speedy typereps (#13403)

* Add SBGuardMatchTemplateId builtin

* drop speedy typeRep args

* Update expected exceptions in daml-lf InterfacesTest

changelog_begin
changelog_end
This commit is contained in:
Moisés Ackerman 2022-03-31 13:36:33 +02:00 committed by GitHub
parent a010f7cf5b
commit 907bc383e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 48 deletions

View File

@ -206,7 +206,7 @@ class InterfacesTest
val command = ApiCommand.Exercise(idT2, cid1, "C2", ValueRecord(None, ImmArray.empty))
inside(runApi(command)) { case Left(Error.Interpretation(err, _)) =>
err shouldBe Error.Interpretation.DamlException(
IE.WronglyTypedContract(cid1, idT2, idT1)
IE.ContractDoesNotImplementInterface(idI2, cid1, idT1)
)
}
}
@ -280,7 +280,7 @@ class InterfacesTest
ReplayCommand.ExerciseByInterface(idI2, idT2, cid1, "C2", ValueRecord(None, ImmArray.empty))
inside(runReplay(command)) { case Left(Error.Interpretation(err, _)) =>
err shouldBe Error.Interpretation.DamlException(
IE.WronglyTypedContract(cid1, idT2, idT1)
IE.ContractDoesNotImplementInterface(idI2, cid1, idT1)
)
}
}

View File

@ -223,8 +223,6 @@ private[lf] final class Compiler(
private val Env3 = Env2.pushVar
private val Pos4 = Env3.nextPosition
private val Env4 = Env3.pushVar
private val Pos5 = Env4.nextPosition
private val Env5 = Env4.pushVar
private[this] def fun1(body: (Position, Env) => s.SExpr) =
s.SEAbs(1, body(Pos1, Env1))
@ -235,8 +233,8 @@ private[lf] final class Compiler(
private[this] def fun3(body: (Position, Position, Position, Env) => s.SExpr) =
s.SEAbs(3, body(Pos1, Pos2, Pos3, Env3))
private[this] def fun5(body: (Position, Position, Position, Position, Position, Env) => s.SExpr) =
s.SEAbs(5, body(Pos1, Pos2, Pos3, Pos4, Pos5, Env5))
private[this] def fun4(body: (Position, Position, Position, Position, Env) => s.SExpr) =
s.SEAbs(4, body(Pos1, Pos2, Pos3, Pos4, Env4))
private[this] def topLevelFunction1[SDefRef <: t.SDefinitionRef: LabelModule.Allowed](
ref: SDefRef
@ -264,12 +262,12 @@ private[lf] final class Compiler(
): (SDefRef, SDefinition) =
topLevelFunction(ref)(fun3(body))
private[this] def topLevelFunction5[SDefRef <: t.SDefinitionRef: LabelModule.Allowed](
private[this] def topLevelFunction4[SDefRef <: t.SDefinitionRef: LabelModule.Allowed](
ref: SDefRef
)(
body: (Position, Position, Position, Position, Position, Env) => s.SExpr
body: (Position, Position, Position, Position, Env) => s.SExpr
): (SDefRef, SDefinition) =
topLevelFunction(ref)(fun5(body))
topLevelFunction(ref)(fun4(body))
val phaseOne = {
val config1 =
@ -469,14 +467,12 @@ private[lf] final class Compiler(
choiceArgPos: Position,
cidPos: Position,
tokenPos: Position,
typeRepPos: Position,
guardPos: Position,
) =
let(
env,
SBCastAnyInterface(ifaceId)(
env.toSEVar(cidPos),
env.toSEVar(typeRepPos),
SBFetchAny(env.toSEVar(cidPos), s.SEValue.None),
),
) { (payloadPos, _env) =>
@ -520,16 +516,13 @@ private[lf] final class Compiler(
): (t.SDefinitionRef, SDefinition) =
topLevelFunction3(t.ChoiceDefRef(ifaceId, choice.name)) {
(cidPos, choiceArgPos, tokenPos, env) =>
let(env, s.SEValue(SOptional(None))) { (typeRepPos, env) =>
let(env, s.SEBuiltin(SBGuardConstTrue)) { (guardPos, env) =>
translateInterfaceChoiceBody(env, ifaceId, param, choice)(
choiceArgPos,
cidPos,
tokenPos,
typeRepPos,
guardPos,
)
}
let(env, s.SEBuiltin(SBGuardConstTrue)) { (guardPos, env) =>
translateInterfaceChoiceBody(env, ifaceId, param, choice)(
choiceArgPos,
cidPos,
tokenPos,
guardPos,
)
}
}
@ -538,13 +531,12 @@ private[lf] final class Compiler(
param: ExprVarName,
choice: TemplateChoice,
): (t.SDefinitionRef, SDefinition) =
topLevelFunction5(t.GuardedChoiceDefRef(ifaceId, choice.name)) {
(cidPos, choiceArgPos, typeRepPos, guardPos, tokenPos, env) =>
topLevelFunction4(t.GuardedChoiceDefRef(ifaceId, choice.name)) {
(cidPos, choiceArgPos, guardPos, tokenPos, env) =>
translateInterfaceChoiceBody(env, ifaceId, param, choice)(
choiceArgPos,
cidPos,
tokenPos,
typeRepPos,
guardPos,
)
}
@ -642,14 +634,13 @@ private[lf] final class Compiler(
private[this] def compileFetchInterfaceBody(
env: Env,
ifaceId: Identifier,
tmplId: Option[TypeConName],
cidPos: Position,
typeRepPos: Position,
) =
let(
env,
SBCastAnyInterface(ifaceId)(
SBCastAnyInterface(ifaceId, tmplId)(
env.toSEVar(cidPos),
env.toSEVar(typeRepPos),
SBFetchAny(env.toSEVar(cidPos), s.SEValue.None),
),
) { (payloadPos, env) =>
@ -665,9 +656,7 @@ private[lf] final class Compiler(
ifaceId: Identifier
): (t.SDefinitionRef, SDefinition) =
topLevelFunction2(t.FetchDefRef(ifaceId)) { (cidPos, _, env) =>
let(env, s.SEValue(SOptional(None))) { (typeRepPos, env) =>
compileFetchInterfaceBody(env, ifaceId, cidPos, typeRepPos)
}
compileFetchInterfaceBody(env, ifaceId, None, cidPos)
}
private[this] def compileInterfacePrecond(
@ -893,8 +882,7 @@ private[lf] final class Compiler(
t.GuardedChoiceDefRef(interfaceId, choiceId)(
s.SEValue(contractId),
s.SEValue(argument),
s.SEValue(SOptional(Some(STypeRep(TTyCon(templateId))))),
s.SEBuiltin(SBGuardConstTrue),
s.SEApp(s.SEBuiltin(SBGuardMatchTemplateId(templateId)), List(s.SEValue(contractId))),
env.toSEVar(tokenPos),
)
}
@ -907,9 +895,7 @@ private[lf] final class Compiler(
): s.SExpr =
unaryFunction(env) { (_, env) =>
let(env, s.SEValue(contractId)) { (cidPos, env) =>
let(env, s.SEValue(SOptional(Some(STypeRep(TTyCon(templateId)))))) { (typeRepPos, env) =>
compileFetchInterfaceBody(env, interfaceId, cidPos, typeRepPos)
}
compileFetchInterfaceBody(env, interfaceId, Some(templateId), cidPos)
}
}

View File

@ -723,9 +723,6 @@ private[lf] final class PhaseOne(
t.GuardedChoiceDefRef(ifaceId, chId)(
cid,
arg,
SEValue(
SOptional(None)
), // TODO https://github.com/digital-asset/daml/issues/13277
guard,
)
)

View File

@ -1036,21 +1036,22 @@ private[lf] object SBuiltin {
}
}
// SBCastAnyInterface: ContractId ifaceId -> Option TypRep -> Any -> ifaceId
final case class SBCastAnyInterface(ifaceId: TypeConName) extends SBuiltin(3) {
// SBCastAnyInterface: ContractId ifaceId -> Any -> ifaceId
final case class SBCastAnyInterface(
ifaceId: TypeConName,
optExpectedTmplId: Option[TypeConName] = None,
) extends SBuiltin(2) {
override private[speedy] def execute(args: util.ArrayList[SValue], machine: Machine): Unit = {
def coid = getSContractId(args, 0)
val (actualTmplId, _) = getSAnyContract(args, 2)
getSOptional(args, 1).foreach {
case STypeRep(Ast.TTyCon(expectedTmplId)) =>
if (actualTmplId != expectedTmplId)
throw SErrorDamlException(IE.WronglyTypedContract(coid, expectedTmplId, actualTmplId))
case otherwise =>
crash(s"expected a type constructor, but got $otherwise")
val (actualTmplId, _) = getSAnyContract(args, 1)
optExpectedTmplId.foreach { expectedTmplId: TypeConName =>
if (actualTmplId != expectedTmplId)
throw SErrorDamlException(IE.WronglyTypedContract(coid, expectedTmplId, actualTmplId))
}
if (machine.compiledPackages.getDefinition(ImplementsDefRef(actualTmplId, ifaceId)).isEmpty)
throw SErrorDamlException(IE.ContractDoesNotImplementInterface(ifaceId, coid, actualTmplId))
machine.returnValue = args.get(2)
machine.returnValue = args.get(1)
}
}
@ -1124,6 +1125,20 @@ private[lf] object SBuiltin {
}
}
final case class SBGuardMatchTemplateId(
expectedTmplId: TypeConName
) extends SBuiltinPure(2) {
override private[speedy] def executePure(
args: util.ArrayList[SValue]
): SBool = {
val contractId = getSContractId(args, 0)
val (actualTmplId, record @ _) = getSAnyContract(args, 1)
if (actualTmplId != expectedTmplId)
throw SErrorDamlException(IE.WronglyTypedContract(contractId, expectedTmplId, actualTmplId))
SBool(true)
}
}
final case class SBResolveSBUBeginExercise(
choiceName: ChoiceName,
consuming: Boolean,