Skip to content

Commit

Permalink
Avoid using assert in Scala, use assertInJvm to elide it in produ…
Browse files Browse the repository at this point in the history
…ction (#11027)

Fixes #11022 and in general fixes #5787 by avoiding calls to Scala's `assert` function and using regular one written in Java that uses `assert` keyword and is thus sensitive to `-ea` command line option of the JVM. Use `assertInJvm` in newly written Scala code to get typical JVM behavior on `assert`.
  • Loading branch information
JaroslavTulach authored Sep 16, 2024
1 parent d795518 commit a6fbdb1
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 26 deletions.
14 changes: 14 additions & 0 deletions engine/common/src/main/java/org/enso/common/Asserts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.enso.common;

public final class Asserts {
private Asserts() {
}

public static void assertInJvm(boolean check) {
assert check;
}

public static void assertInJvm(boolean check, String msg) {
assert check : msg;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class LocalScope(
symbolsProvider: () => FrameVariableNames = null
): LocalScope = {
val sp = if (flattenToParent) {
assert(symbolsProvider == null)
org.enso.common.Asserts.assertInJvm(symbolsProvider == null)
this.symbolsProvider
} else {
symbolsProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,12 @@ object BindingsMap {
exportedAs: Option[String],
symbols: List[String]
) {
assert(
org.enso.common.Asserts.assertInJvm(
symbols.forall(!_.contains(".")),
"Not expected fully qualified names as symbols"
)
if (exportedAs.isDefined) {
assert(
org.enso.common.Asserts.assertInJvm(
!exportedAs.get.contains("."),
"Not expected fully qualified name as `exportedAs`"
)
Expand Down Expand Up @@ -492,8 +492,8 @@ object BindingsMap {
exports: List[ir.module.scope.Export.Module],
targets: List[ImportTarget]
) {
assert(targets.nonEmpty)
assert(
org.enso.common.Asserts.assertInJvm(targets.nonEmpty)
org.enso.common.Asserts.assertInJvm(
areTargetsConsistent(),
"All targets must be either static methods or conversion methods"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,11 @@ case object FramePointerAnalysis extends IRPass {
scope: Graph.Scope,
defOcc: GraphOccurrence.Def
): Int = {
assert(
org.enso.common.Asserts.assertInJvm(
graph.scopeFor(defOcc.id).contains(scope),
"Def occurrence must be in the given scope"
)
assert(
org.enso.common.Asserts.assertInJvm(
scope.allDefinitions.contains(defOcc),
"The given scope must contain the given Def occurrence"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ sealed class Graph(
* @param sym the symbol occurrence
*/
def addGlobalSymbol(sym: GraphOccurrence.Global): Unit = {
assert(!frozen)
org.enso.common.Asserts.assertInJvm(!frozen)
if (!globalSymbols.contains(sym.symbol)) {
globalSymbols = globalSymbols + (sym.symbol -> sym)
}
Expand Down Expand Up @@ -116,7 +116,8 @@ sealed class Graph(
}

private def addSourceTargetLink(link: Graph.Link): Unit = {
assert(!frozen)
// commented out: used from DebugEvalNode
// org.enso.common.Asserts.assertInJvm(!frozen)
sourceLinks = sourceLinks.updatedWith(link.source)(v =>
v.map(s => s + link).orElse(Some(Set(link)))
)
Expand Down Expand Up @@ -382,7 +383,7 @@ object Graph {
* @return this scope with parent scope set
*/
def withParent(parentScope: Scope): this.type = {
assert(parent.isEmpty)
org.enso.common.Asserts.assertInJvm(parent.isEmpty)
this.parent = Some(parentScope)
this
}
Expand Down Expand Up @@ -697,7 +698,7 @@ object Graph {
/** Disassociates this Scope from its parent.
*/
def removeScopeFromParent(): Unit = {
assert(this.parent.nonEmpty)
org.enso.common.Asserts.assertInJvm(this.parent.nonEmpty)
this.parent.foreach(_.removeScopeFromParent(this))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ case object FunctionBinding extends IRPass {
errors.Conversion.MissingSourceType(args.head.name.name)
)
} else {
assert(!isPrivate, "Should be handled by previous match")
org.enso.common.Asserts
.assertInJvm(!isPrivate, "Should be handled by previous match")
val firstArg :: restArgs = args
val firstArgumentType = firstArg.ascribedType.get
val firstArgumentName = firstArg.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ case object MethodDefinitions extends IRPass {
) match {
case Some(Resolution(ResolvedType(_, tp)))
if canGenerateStaticWrappers(tp) =>
assert(method.body.isInstanceOf[Function.Lambda])
org.enso.common.Asserts
.assertInJvm(method.body.isInstanceOf[Function.Lambda])
val dup = method.duplicate()
// This is the self argument that will receive the `SelfType.type` value upon dispatch, it is added to avoid modifying the dispatch mechanism.
val syntheticModuleSelfArg = DefinitionArgument.Specified(
Expand Down Expand Up @@ -197,7 +198,10 @@ case object MethodDefinitions extends IRPass {
errors.Resolution.ResolverError(err)
)
case Right(resolvedItems) =>
assert(resolvedItems.size == 1, "Expected a single resolution")
org.enso.common.Asserts.assertInJvm(
resolvedItems.size == 1,
"Expected a single resolution"
)
resolvedItems.head match {
case _: BindingsMap.ResolvedConstructor =>
errors.Resolution(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ object Patterns extends IRPass {
bindingsMap.resolveQualifiedName(parts) match {
case Left(err) => Left(err)
case Right(resolvedNames) =>
assert(resolvedNames.size == 1, "Expected a single resolution")
org.enso.common.Asserts
.assertInJvm(resolvedNames.size == 1, "Expected a single resolution")
Right(resolvedNames.head)
}
}
Expand All @@ -104,7 +105,8 @@ object Patterns extends IRPass {
bindingsMap.resolveQualifiedNameIn(scope, submoduleNames, finalItem) match {
case Left(err) => Left(err)
case Right(resolvedNames) =>
assert(resolvedNames.size == 1, "Expected a single resolution")
org.enso.common.Asserts
.assertInJvm(resolvedNames.size == 1, "Expected a single resolution")
Right(resolvedNames.head)
}
}
Expand All @@ -118,7 +120,8 @@ object Patterns extends IRPass {
bindingsMap.resolveName(name) match {
case Left(err) => Left(err)
case Right(resolvedNames) =>
assert(resolvedNames.size == 1, "Expected a single resolution")
org.enso.common.Asserts
.assertInJvm(resolvedNames.size == 1, "Expected a single resolution")
Right(resolvedNames.head)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,15 +489,15 @@ private class DefaultPackageRepository(
syntheticModule: Module,
refs: List[QualifiedName]
): Unit = {
assert(syntheticModule.isSynthetic)
org.enso.common.Asserts.assertInJvm(syntheticModule.isSynthetic)
if (!loadedModules.contains(syntheticModule.getName.toString)) {
loadedModules.put(
syntheticModule.getName.toString,
syntheticModule.asCompilerModule()
)
} else {
val loaded = loadedModules(syntheticModule.getName.toString)
assert(!loaded.isSynthetic)
org.enso.common.Asserts.assertInJvm(!loaded.isSynthetic)
loaded
.asInstanceOf[TruffleCompilerContext.Module]
.unsafeModule()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ class IrToTruffle(
asAssociatedType(actualModule),
method.name
)
assert(
org.enso.common.Asserts.assertInJvm(
fun != null,
s"exported symbol `${method.name}` needs to be registered first in the module "
)
Expand All @@ -1106,7 +1106,7 @@ class IrToTruffle(
case Right(List(BindingsMap.ResolvedType(modWithTp, _))) =>
val tpScope = asScope(modWithTp.unsafeAsModule())
val tp = tpScope.getType(staticMethod.tpName, true)
assert(
org.enso.common.Asserts.assertInJvm(
tp != null,
s"Type should be defined in module ${modWithTp.getName}"
)
Expand All @@ -1118,7 +1118,7 @@ class IrToTruffle(
eigenTp,
staticMethod.methodName
)
assert(
org.enso.common.Asserts.assertInJvm(
fun != null,
s"exported symbol (static method) `${staticMethod.name}` needs to be registered first in the module "
)
Expand Down Expand Up @@ -1147,7 +1147,7 @@ class IrToTruffle(
val targetTpScope = asScope(modWithTargetTp.unsafeAsModule())
val targetTp =
targetTpScope.getType(conversionMethod.targetTpName, true)
assert(
org.enso.common.Asserts.assertInJvm(
targetTp != null,
s"Target type should be defined in module ${module.getName}"
)
Expand All @@ -1163,7 +1163,7 @@ class IrToTruffle(
conversionMethod.sourceTpName,
true
)
assert(
org.enso.common.Asserts.assertInJvm(
sourceTp != null,
s"Source type should be defined in module ${module.getName}"
)
Expand All @@ -1172,7 +1172,7 @@ class IrToTruffle(
sourceTp,
targetTp
)
assert(
org.enso.common.Asserts.assertInJvm(
conversionFun != null,
s"Conversion method `$conversionMethod` should be defined in module ${module.getName}"
)
Expand Down Expand Up @@ -2570,7 +2570,7 @@ class IrToTruffle(

// Note [Handling Suspended Defaults]
val defaultedValue = if (arg.suspended && defaultExpression != null) {
assert(arg.defaultValue.isDefined)
org.enso.common.Asserts.assertInJvm(arg.defaultValue.isDefined)
val defaultRootNode = ClosureRootNode.build(
language,
scope,
Expand Down

0 comments on commit a6fbdb1

Please sign in to comment.