LF: clean daml-lf/language module (#11152)

- enable NonUnitStatements, and find a useless statement.

- add missing `final`s,

- replace `{ ... }` by `( ... )` for one statement anonymous function

- drop useless `new` for case class.

- replace `sealed class X { ... object Y extend X` by
  `class X private[Z] { ... val Y = new X` to avoid unecessary class
  alocation.

- move invariant check inside the body of some case class instead of
  the companion object.

- other cosmetic changes.

CHANGELOG_BEGIN
CHANGELOG_END
This commit is contained in:
Remy 2021-10-07 13:56:36 +02:00 committed by GitHub
parent 15712a2a33
commit dd233ef155
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 64 deletions

View File

@ -6,6 +6,7 @@ load(
"da_scala_library",
"da_scala_test",
"lf_scalacopts",
"lf_scalacopts_stricter",
"silencer_plugin",
)
@ -15,7 +16,7 @@ da_scala_library(
scala_deps = [
"@maven//:org_scalaz_scalaz_core",
],
scalacopts = lf_scalacopts,
scalacopts = lf_scalacopts_stricter,
tags = ["maven_coordinates=com.daml:daml-lf-language:__VERSION__"],
visibility = ["//visibility:public"],
deps = [

View File

@ -12,7 +12,7 @@ object Ast {
//
/** Fully applied type constructor. */
case class TypeConApp(tycon: TypeConName, args: ImmArray[Type]) {
final case class TypeConApp(tycon: TypeConName, args: ImmArray[Type]) {
def pretty: String =
args.foldLeft(TTyCon(tycon): Type) { case (arg, acc) => TApp(acc, arg) }.pretty
}
@ -33,7 +33,7 @@ object Ast {
type EnumConName = Name
/* Binding in a let/update/scenario block. */
case class Binding(binder: Option[ExprVarName], typ: Type, bound: Expr)
final case class Binding(binder: Option[ExprVarName], typ: Type, bound: Expr)
//
// Expressions
@ -223,9 +223,7 @@ object Ast {
prec > precTApp,
syn.qualifiedName.name.toString + " " +
args
.map { t =>
prettyType(t, precTApp + 1)
}
.map(t => prettyType(t, precTApp + 1))
.toSeq
.mkString(" "),
)
@ -482,7 +480,7 @@ object Ast {
// Update expressions
//
case class RetrieveByKey(templateId: TypeConName, key: Expr)
final case class RetrieveByKey(templateId: TypeConName, key: Expr)
sealed abstract class Update extends Product with Serializable
@ -560,7 +558,7 @@ object Ast {
final case class CPSome(body: ExprVarName) extends CasePat
// Case alternative
case class CaseAlt(pattern: CasePat, expr: Expr)
final case class CaseAlt(pattern: CasePat, expr: Expr)
//
// Definitions
@ -586,18 +584,18 @@ object Ast {
isTest: Boolean,
) extends GenDefinition[E]
class GenDValueCompanion[E] {
final class GenDValueCompanion[E] private[Ast] {
def apply(typ: Type, noPartyLiterals: Boolean, body: E, isTest: Boolean): GenDValue[E] =
new GenDValue(typ, noPartyLiterals, body, isTest)
GenDValue(typ, noPartyLiterals, body, isTest)
def unapply(arg: GenDValue[E]): Some[(Type, Boolean, E, Boolean)] =
Some((arg.typ, arg.noPartyLiterals, arg.body, arg.isTest))
}
type DValue = GenDValue[Expr]
object DValue extends GenDValueCompanion[Expr]
val DValue = new GenDValueCompanion[Expr]
type DValueSignature = GenDValue[Unit]
object DValueSignature extends GenDValueCompanion[Unit]
val DValueSignature = new GenDValueCompanion[Unit]
type Definition = GenDefinition[Expr]
type DefinitionSignature = GenDefinition[Unit]
@ -612,7 +610,6 @@ object Ast {
final case class DataVariant(variants: ImmArray[(VariantConName, Type)]) extends DataCons {
lazy val constructorInfo: Map[VariantConName, (Type, Int)] =
variants.iterator.zipWithIndex.map { case ((cons, typ), rank) => (cons, (typ, rank)) }.toMap
variants.iterator.zipWithIndex.map { case ((cons, typ), rank) => (cons, (typ, rank)) }.toMap
}
final case class DataEnum(constructors: ImmArray[EnumConName]) extends DataCons {
lazy val constructorRank: Map[EnumConName, Int] = constructors.iterator.zipWithIndex.toMap
@ -626,19 +623,19 @@ object Ast {
maintainers: E,
)
sealed class GenTemplateKeyCompanion[E] {
final class GenTemplateKeyCompanion[E] private[Ast] {
def apply(typ: Type, body: E, maintainers: E): GenTemplateKey[E] =
new GenTemplateKey(typ, body, maintainers)
GenTemplateKey(typ, body, maintainers)
def unapply(arg: GenTemplateKey[E]): Some[(Type, E, E)] =
Some((arg.typ, arg.body, arg.maintainers))
}
type TemplateKey = GenTemplateKey[Expr]
object TemplateKey extends GenTemplateKeyCompanion[Expr]
val TemplateKey = new GenTemplateKeyCompanion[Expr]
type TemplateKeySignature = GenTemplateKey[Unit]
object TemplateKeySignature extends GenTemplateKeyCompanion[Unit]
val TemplateKeySignature = new GenTemplateKeyCompanion[Unit]
final case class DefInterface(
choices: Map[ChoiceName, InterfaceChoice],
@ -652,19 +649,17 @@ object Ast {
): DefInterface = {
val choiceMap = toMapWithoutDuplicate(
choices,
(name: ChoiceName) =>
throw PackageError(s"collision on interface choice name ${name.toString}"),
(name: ChoiceName) => throw PackageError(s"collision on interface choice name $name"),
)
val methodMap = toMapWithoutDuplicate(
methods,
(name: MethodName) =>
throw PackageError(s"collision on interface method name ${name.toString}"),
(name: MethodName) => throw PackageError(s"collision on interface method name $name"),
)
DefInterface(choiceMap, methodMap)
}
}
case class InterfaceChoice(
final case class InterfaceChoice(
name: ChoiceName,
consuming: Boolean,
argType: Type,
@ -672,12 +667,12 @@ object Ast {
// TODO interfaces Should observers or controllers be part of the interface?
)
case class InterfaceMethod(
final case class InterfaceMethod(
name: MethodName,
returnType: Type,
)
case class GenTemplate[E] private[Ast] (
final case class GenTemplate[E](
param: ExprVarName, // Binder for template argument.
precond: E, // Template creation precondition.
signatories: E, // Parties agreeing to the contract.
@ -686,9 +681,9 @@ object Ast {
observers: E, // Observers of the contract.
key: Option[GenTemplateKey[E]],
implements: Map[TypeConName, GenTemplateImplements[E]],
) extends NoCopy
)
sealed class GenTemplateCompanion[E] {
final class GenTemplateCompanion[E] private[Ast] {
def apply(
param: ExprVarName,
@ -703,8 +698,7 @@ object Ast {
val choiceMap = toMapWithoutDuplicate(
choices,
(choiceName: ChoiceName) =>
throw PackageError(s"collision on choice name ${choiceName.toString}"),
(choiceName: ChoiceName) => throw PackageError(s"collision on choice name $choiceName"),
)
val implementsMap = toMapWithoutDuplicate(
@ -713,7 +707,7 @@ object Ast {
throw PackageError(s"repeated interface implementation ${ifaceName.toString}"),
)
new GenTemplate[E](
GenTemplate[E](
param,
precond,
signatories,
@ -764,12 +758,12 @@ object Ast {
}
type Template = GenTemplate[Expr]
object Template extends GenTemplateCompanion[Expr]
val Template = new GenTemplateCompanion[Expr]
type TemplateSignature = GenTemplate[Unit]
object TemplateSignature extends GenTemplateCompanion[Unit]
val TemplateSignature = new GenTemplateCompanion[Unit]
case class GenTemplateChoice[E](
final case class GenTemplateChoice[E](
name: ChoiceName, // Name of the choice.
consuming: Boolean, // Flag indicating whether exercising the choice consumes the contract.
controllers: E, // Parties that can exercise the choice.
@ -780,7 +774,7 @@ object Ast {
update: E, // The choice follow-up.
)
sealed class GenTemplateChoiceCompanion[E] {
final class GenTemplateChoiceCompanion[E] private[Ast] {
def apply(
name: ChoiceName,
consuming: Boolean,
@ -820,17 +814,17 @@ object Ast {
}
type TemplateChoice = GenTemplateChoice[Expr]
object TemplateChoice extends GenTemplateChoiceCompanion[Expr]
val TemplateChoice = new GenTemplateChoiceCompanion[Expr]
type TemplateChoiceSignature = GenTemplateChoice[Unit]
object TemplateChoiceSignature extends GenTemplateChoiceCompanion[Unit]
val TemplateChoiceSignature = new GenTemplateChoiceCompanion[Unit]
case class GenTemplateImplements[E] private[Ast] (
final case class GenTemplateImplements[E](
interface: TypeConName,
methods: Map[MethodName, GenTemplateImplementsMethod[E]],
)
sealed class GenTemplateImplementsCompanion[E] {
final class GenTemplateImplementsCompanion[E] private[Ast] {
def apply(
interface: TypeConName,
methods: Iterable[(MethodName, GenTemplateImplementsMethod[E])],
@ -838,7 +832,7 @@ object Ast {
val methodMap = toMapWithoutDuplicate(
methods,
(methodName: MethodName) =>
throw PackageError(s"repeated method implementation ${methodName.toString}"),
throw PackageError(s"repeated method implementation $methodName"),
)
new GenTemplateImplements[E](interface, methodMap)
}
@ -856,17 +850,17 @@ object Ast {
}
type TemplateImplements = GenTemplateImplements[Expr]
object TemplateImplements extends GenTemplateImplementsCompanion[Expr]
val TemplateImplements = new GenTemplateImplementsCompanion[Expr]
type TemplateImplementsSignature = GenTemplateImplements[Unit]
object TemplateImplementsSignature extends GenTemplateImplementsCompanion[Unit]
val TemplateImplementsSignature = new GenTemplateImplementsCompanion[Unit]
case class GenTemplateImplementsMethod[E] private[Ast] (
final case class GenTemplateImplementsMethod[E](
name: MethodName,
value: E,
)
sealed class GenTemplateImplementsMethodCompanion[E] {
final class GenTemplateImplementsMethodCompanion[E] {
def apply(
name: MethodName,
value: E,
@ -880,14 +874,14 @@ object Ast {
}
type TemplateImplementsMethod = GenTemplateImplementsMethod[Expr]
object TemplateImplementsMethod extends GenTemplateImplementsMethodCompanion[Expr]
val TemplateImplementsMethod = new GenTemplateImplementsMethodCompanion[Expr]
type TemplateImplementsMethodSignature = GenTemplateImplementsMethod[Unit]
object TemplateImplementsMethodSignature extends GenTemplateImplementsMethodCompanion[Unit]
val TemplateImplementsMethodSignature = new GenTemplateImplementsMethodCompanion[Unit]
final case class GenDefException[E](message: E)
sealed class GenDefExceptionCompanion[E] {
final class GenDefExceptionCompanion[E] private[Ast] {
def apply(message: E): GenDefException[E] =
GenDefException(message)
@ -896,12 +890,12 @@ object Ast {
}
type DefException = GenDefException[Expr]
object DefException extends GenDefExceptionCompanion[Expr]
val DefException = new GenDefExceptionCompanion[Expr]
type DefExceptionSignature = GenDefException[Unit]
val DefExceptionSignature = GenDefException(())
case class FeatureFlags(
final case class FeatureFlags(
forbidPartyLiterals: Boolean // If set to true, party literals are not allowed to appear in daml-lf packages.
/*
These flags are present in Daml-LF, but our ecosystem does not support them anymore:
@ -926,14 +920,19 @@ object Ast {
// Modules and packages
//
case class GenModule[E] private[Ast] (
final case class GenModule[E](
name: ModuleName,
definitions: Map[DottedName, GenDefinition[E]],
templates: Map[DottedName, GenTemplate[E]],
exceptions: Map[DottedName, GenDefException[E]],
interfaces: Map[DottedName, DefInterface],
featureFlags: FeatureFlags,
) extends NoCopy
) {
templates.keysIterator.foreach(name =>
if (exceptions.keySet.contains(name))
throw PackageError(s"Collision between exception and template name ${name.toString}")
)
}
private[this] def toMapWithoutDuplicate[Key, Value](
xs: Iterable[(Key, Value)],
@ -947,7 +946,7 @@ object Ast {
}
}
sealed class GenModuleCompanion[E] {
final class GenModuleCompanion[E] private[Ast] {
def apply(
name: ModuleName,
@ -982,10 +981,6 @@ object Ast {
(name: DottedName) => throw PackageError(s"Collision on interface name ${name.toString}"),
)
templateMap.keysIterator.find(exceptionMap.keySet.contains).foreach { name =>
throw PackageError(s"Collision between exception and template name ${name.toString}")
}
GenModule(name, definitionMap, templateMap, exceptionMap, interfaceMap, featureFlags)
}
@ -1004,21 +999,21 @@ object Ast {
}
type Module = GenModule[Expr]
object Module extends GenModuleCompanion[Expr]
val Module = new GenModuleCompanion[Expr]
type ModuleSignature = GenModule[Unit]
object ModuleSignature extends GenModuleCompanion[Unit]
val ModuleSignature = new GenModuleCompanion[Unit]
case class PackageMetadata(name: PackageName, version: PackageVersion)
final case class PackageMetadata(name: PackageName, version: PackageVersion)
case class GenPackage[E](
final case class GenPackage[E](
modules: Map[ModuleName, GenModule[E]],
directDeps: Set[PackageId],
languageVersion: LanguageVersion,
metadata: Option[PackageMetadata],
)
sealed class GenPackageCompanion[E] {
final class GenPackageCompanion[E] private[Ast] {
def apply(
modules: Iterable[GenModule[E]],
@ -1060,7 +1055,7 @@ object Ast {
}
type Package = GenPackage[Expr]
object Package extends GenPackageCompanion[Expr]
val Package = new GenPackageCompanion[Expr]
// [PackageSignature] is a version of the AST that does not contain
// LF expression. This should save memory in the [CompiledPackages]
@ -1073,7 +1068,7 @@ object Ast {
// types of the serializable data of a package. See for instance
// [InterfaceReader]
type PackageSignature = GenPackage[Unit]
object PackageSignature extends GenPackageCompanion[Unit]
val PackageSignature = new GenPackageCompanion[Unit]
val keyFieldName = Name.assertFromString("key")
val valueFieldName = Name.assertFromString("value")

View File

@ -7,7 +7,7 @@ import com.daml.lf.data.{InsertOrdSet, Relation}
object Graphs {
case class Cycle[X](vertices: List[X])
final case class Cycle[X](vertices: List[X])
type Graph[X] = Relation.Relation[X, X]

View File

@ -9,7 +9,7 @@ object StablePackages {
// Based on compiler/damlc/tests/src/stable-packages.sh
// TODO: Use this map to generate the mapping used in the test,
// or generate both from a single source.
private val nameToIdMap = Map[PackageName, PackageId](
private[this] def nameToIdMap: Map[PackageName, PackageId] = Map(
PackageName.assertFromString(
"daml-prim-DA-Exception-ArithmeticError-cb0552debf219cc909f51cbb5c3b41e9981d39f8f645b1f35e2ef5be2e0b858a"
) -> PackageId.assertFromString(
@ -116,5 +116,5 @@ object StablePackages {
"99a2705ed38c1c26cbb8fe7acf36bbf626668e167a33335de932599219e0a235"
),
)
val Ids = nameToIdMap.values.toSet
val Ids: Set[PackageId] = nameToIdMap.values.toSet
}