Skip to content

Commit

Permalink
Turn off coverage for all assertionFailed in macros (since most of th…
Browse files Browse the repository at this point in the history
…em are errors that we don't expect to reproduce but not fix), refactor defaults a bit
  • Loading branch information
MateuszKubuszok committed Jul 2, 2024
1 parent 4028d7a commit ae098a6
Show file tree
Hide file tree
Showing 19 changed files with 60 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ private[compiletime] trait TypesPlatform extends Types { this: DefinitionsPlatfo
.value
.asInstanceOf[String]
)
.getOrElse(assertionFailed(s"Invalid string literal type: ${prettyPrint(S)}"))
.getOrElse {
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
assertionFailed(s"Invalid string literal type: ${prettyPrint(S)}")
// $COVERAGE-ON$
}

def isTuple[A](A: Type[A]): Boolean = A.tpe.typeSymbol.fullName.startsWith("scala.Tuple")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ trait ProductTypesPlatform extends ProductTypes { this: DefinitionsPlatform =>
} else if (isPOJO[A]) {
val primaryConstructor =
Option(sym).filter(_.isClass).map(_.asClass.primaryConstructor).filter(_.isPublic).getOrElse {
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
assertionFailed(s"Expected public constructor of ${Type.prettyPrint[A]}")
// $COVERAGE-ON$
}
val paramss = paramListsOf(A, primaryConstructor)
val paramNames = paramss.flatMap(_.map(param => param -> getDecodedName(param))).toMap
Expand All @@ -129,19 +131,20 @@ trait ProductTypesPlatform extends ProductTypes { this: DefinitionsPlatform =>
val defaultIdx = idx + 1 // defaults are 1-indexed
val scala2default = caseClassApplyDefaultScala2(defaultIdx)
val scala3default = caseClassApplyDefaultScala3(defaultIdx)
val scala2new = classNewDefaultScala2(defaultIdx)
val newDefault = classNewDefaultScala2(defaultIdx)
val defaults = List(scala2default, scala3default, newDefault)
val foundDefault = companion.typeSignature.decls
.to(List)
.collectFirst {
case method if getDecodedName(method) == scala2default => TermName(scala2default)
case method if getDecodedName(method) == scala3default => TermName(scala3default)
case method if getDecodedName(method) == scala2new => TermName(scala2new)
case method if defaults.contains(getDecodedName(method)) => method
}
.getOrElse(
.getOrElse {
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
assertionFailed(
s"Expected that ${Type.prettyPrint[A]}'s constructor parameter `$param` would have default value: attempted `$scala2default`, `$scala3default` and `$scala2new`, found: ${companion.typeSignature.decls}"
s"Expected that ${Type.prettyPrint[A]}'s constructor parameter `$param` would have default value: attempted `$scala2default`, `$scala3default` and `$newDefault`, found: ${companion.typeSignature.decls}"
)
)
// $COVERAGE-ON$
}
paramNames(param) -> q"$companion.$foundDefault"
}.toMap
val constructorParameters = ListMap.from(paramss.flatMap(_.map { param =>
Expand Down Expand Up @@ -276,7 +279,7 @@ trait ProductTypesPlatform extends ProductTypes { this: DefinitionsPlatform =>
.dropWhile { case (x, y) => x == y }
.takeWhile(_._1 != NoSymbol)
.map(_._1.name.toTermName)
// $COVERAGE-OFF$
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
if (path.isEmpty) assertionFailed(s"Cannot find a companion for ${Type.prettyPrint[A]}")
else c.typecheck(path.foldLeft[Tree](Ident(path.next()))(Select(_, _)), silent = true).symbol
// $COVERAGE-ON$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ private[compiletime] trait TypesPlatform extends Types { this: DefinitionsPlatfo
typeArgumentByName
// unknown
case out =>
// $COVERAGE-OFF$should never happen unless we messed up
assertionFailed(
s"Constructor of ${Type.prettyPrint(fromUntyped[Any](tpe))} has unrecognized/unsupported format of type: $out"
)
// $COVERAGE-ON$
}

/** Applies type arguments from supertype to subtype if there are any */
Expand Down Expand Up @@ -215,7 +217,10 @@ private[compiletime] trait TypesPlatform extends Types { this: DefinitionsPlatfo

def extractStringSingleton[S <: String](S: Type[S]): String = quoted.Type.valueOfConstant[S](using S) match {
case Some(str) => str
case None => assertionFailed(s"Invalid string literal type: ${prettyPrint(S)}")
case None =>
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
assertionFailed(s"Invalid string literal type: ${prettyPrint(S)}")
// $COVERAGE-ON$
}

def isTuple[A](A: Type[A]): Boolean = TypeRepr.of(using A).typeSymbol.fullName.startsWith("scala.Tuple")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ trait ProductTypesPlatform extends ProductTypes { this: DefinitionsPlatform =>

val primaryConstructor =
Option(sym.primaryConstructor).filter(_.isPublic).getOrElse {
// $COVERAGE-OFF$should never happen unless we messed up
assertionFailed(s"Expected public constructor of ${Type.prettyPrint[A]}")
// $COVERAGE-ON$
}
val paramss = paramListsOf(A, primaryConstructor)
val paramNames = paramss.flatMap(_.map(param => param -> param.name)).toMap
Expand All @@ -155,9 +157,11 @@ trait ProductTypesPlatform extends ProductTypes { this: DefinitionsPlatform =>
val scala3default = caseClassApplyDefaultScala3(idx + 1)
val default =
(mod.declaredMethod(scala2default) ++ mod.declaredMethod(scala3default)).headOption.getOrElse {
// $COVERAGE-OFF$should never happen unless we messed up
assertionFailed(
s"Expected that ${Type.prettyPrint[A]}'s constructor parameter `$param` would have default value: attempted `$scala2default` and `$scala3default`, found: ${mod.declaredMethods}"
)
// $COVERAGE-ON$
}
paramNames(param) -> Ref(mod).select(default)
}.toMap
Expand Down Expand Up @@ -262,7 +266,9 @@ trait ProductTypesPlatform extends ProductTypes { this: DefinitionsPlatform =>
val fnType = fnTypeByArity.getOrElse(
paramList.size,
// TODO: handle FunctionXXL
// $COVERAGE-OFF$should never happen unless we messed up
assertionFailed(s"Expected arity between 0 and 22 into ${Type.prettyPrint[A]}, got: ${paramList.size}")
// $COVERAGE-ON$
)
val paramTypes = paramList.view.values.map(p => TypeRepr.of(using p.Underlying)).toVector

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ private[compiletime] trait Exprs { this: Definitions =>

def summonImplicit[A: Type]: Option[Expr[A]]
def summonImplicitUnsafe[A: Type]: Expr[A] = summonImplicit[A].getOrElse {
// $COVERAGE-OFF$should never happen unless we messed up
assertionFailed(s"Implicit not found: ${Type.prettyPrint[A]}")
// $COVERAGE-ON$
}

// Implementations of Expr extension methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,26 @@ trait ProductTypes { this: Definitions =>
): (Product.Arguments, Product.Arguments) = {
val missingArguments = parameters.filter(settersCanBeIgnored).keySet diff arguments.keySet
if (missingArguments.nonEmpty) {
// $COVERAGE-OFF$should never happen unless we messed up
val missing = missingArguments.mkString(", ")
val provided = arguments.keys.mkString(", ")
assertionFailed(
s"Constructor of ${Type.prettyPrint[A]} expected arguments: $missing but they were not provided, what was provided: $provided"
)
// $COVERAGE-ON$
}

parameters.foreach { case (name, param) =>
import param.Underlying as Param
// setter might be absent, so we cannot assume that argument for it is in a map
arguments.get(name).foreach { argument =>
if (!(argument.Underlying <:< Param)) {
// $COVERAGE-OFF$should never happen unless we messed up
assertionFailed(
s"Constructor of ${Type.prettyPrint[A]} expected expr for parameter $param of type ${Type
.prettyPrint[param.Underlying]}, instead got ${Expr.prettyPrint(argument.value)} ${Type.prettyPrint(argument.Underlying)}"
)
// $COVERAGE-ON$
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ final class CodecMacros(val c: blackbox.Context) extends DerivationPlatform with
)

Expr.summonImplicit(transformerConfigurationType).getOrElse {
// $COVERAGE-OFF$
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
reportError("Can't locate implicit TransformerConfiguration!")
// $COVERAGE-ON$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ final class IsoMacros(val c: blackbox.Context) extends DerivationPlatform with G
)

Expr.summonImplicit(transformerConfigurationType).getOrElse {
// $COVERAGE-OFF$
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
reportError("Can't locate implicit TransformerConfiguration!")
// $COVERAGE-ON$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ final class PatcherMacros(val c: blackbox.Context) extends DerivationPlatform wi
)

Expr.summonImplicit(patcherConfigurationType).getOrElse {
// $COVERAGE-OFF$
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
reportError("Can't locate implicit PatcherConfiguration!")
// $COVERAGE-ON$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ final class TransformerMacros(val c: blackbox.Context) extends DerivationPlatfor
)

Expr.summonImplicit(transformerConfigurationType).getOrElse {
// $COVERAGE-OFF$
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
reportError("Can't locate implicit TransformerConfiguration!")
// $COVERAGE-ON$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ final class CodecMacros(q: Quotes) extends DerivationPlatform(q) with Gateway {
val implicitScopeConfig = scala.quoted.Expr
.summon[io.scalaland.chimney.dsl.TransformerConfiguration[? <: runtime.TransformerFlags]]
.getOrElse {
// $COVERAGE-OFF$
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
reportError("Can't locate implicit TransformerConfiguration!")
// $COVERAGE-ON$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ final class IsoMacros(q: Quotes) extends DerivationPlatform(q) with Gateway {
val implicitScopeConfig = scala.quoted.Expr
.summon[io.scalaland.chimney.dsl.TransformerConfiguration[? <: runtime.TransformerFlags]]
.getOrElse {
// $COVERAGE-OFF$
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
reportError("Can't locate implicit TransformerConfiguration!")
// $COVERAGE-ON$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class PatcherMacros(q: Quotes) extends DerivationPlatform(q) with Gateway
val implicitScopeConfig = scala.quoted.Expr
.summon[io.scalaland.chimney.dsl.PatcherConfiguration[? <: runtime.PatcherFlags]]
.getOrElse {
// $COVERAGE-OFF$
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
reportError("Can't locate implicit PatcherConfiguration!")
// $COVERAGE-ON$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ final class TransformerMacros(q: Quotes) extends DerivationPlatform(q) with Gate
val implicitScopeConfig = scala.quoted.Expr
.summon[io.scalaland.chimney.dsl.TransformerConfiguration[? <: runtime.TransformerFlags]]
.getOrElse {
// $COVERAGE-OFF$
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
reportError("Can't locate implicit TransformerConfiguration!")
// $COVERAGE-ON$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private[compiletime] trait Configurations { this: Derivation =>
} else if (Type[Flag] =:= ChimneyType.PatcherFlags.Flags.MacrosLogging) {
copy(displayMacrosLogging = value)
} else {
// $COVERAGE-OFF$
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
reportError(s"Invalid internal PatcherFlags type shape: ${Type[Flag]}!")
// $COVERAGE-ON$
}
Expand Down Expand Up @@ -79,16 +79,18 @@ private[compiletime] trait Configurations { this: Derivation =>
import flag.Underlying as Flag, flags.Underlying as Flags2
extractTransformerFlags[Flags2](defaultFlags).setBoolFlag[Flag](value = false)
case _ =>
// $COVERAGE-OFF$
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
reportError(s"Invalid internal PatcherFlags type shape: ${Type.prettyPrint[Flags]}!")
// $COVERAGE-ON$
}

private def extractPatcherConfig[Tail <: runtime.PatcherOverrides: Type](): PatcherConfiguration =
Type[Tail] match {
case empty if empty =:= ChimneyType.PatcherOverrides.Empty => PatcherConfiguration()
case _ =>
case _ =>
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
reportError(s"Invalid internal PatcherOverrides type shape: ${Type.prettyPrint[Tail]}!!")
// $COVERAGE-ON$
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ private[compiletime] trait Derivation
)
}
case _ =>
// $COVERAGE-OFF$should never happen unless we messed up
assertionFailed(
s"Expected both types to be options, got ${Type.prettyPrint[PatchGetter]} and ${Type.prettyPrint[TargetParam]}"
)
// $COVERAGE-ON$
}
} else if (PatchGetter <:< TargetParam) {
DerivationResult.pure(Some(patchGetterExpr))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ private[compiletime] trait Configurations { this: Derivation =>
Some(dsls.PreferPartialTransformer)
)
else {
// $COVERAGE-OFF$
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
reportError("Invalid ImplicitTransformerPreference type!!")
// $COVERAGE-ON$
}
Expand Down Expand Up @@ -417,7 +417,7 @@ private[compiletime] trait Configurations { this: Derivation =>
extractTransformerFlags[Flags2](defaultFlags).setBoolFlag[Flag](value = false)
}
case _ =>
// $COVERAGE-OFF$
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
reportError(s"Invalid internal TransformerFlags type shape: ${Type.prettyPrint[Flags]}!")
// $COVERAGE-ON$
}
Expand Down Expand Up @@ -498,7 +498,7 @@ private[compiletime] trait Configurations { this: Derivation =>
TransformerOverride.RenamedTo(extractPath[ToPath])
)
case _ =>
// $COVERAGE-OFF$
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
reportError(s"Invalid internal TransformerOverrides type shape: ${Type.prettyPrint[Tail]}!!")
// $COVERAGE-ON$
}
Expand All @@ -525,7 +525,7 @@ private[compiletime] trait Configurations { this: Derivation =>
import init.Underlying as PathType2
extractPath[PathType2].everyMapValue
case _ =>
// $COVERAGE-OFF$
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
reportError(s"Invalid internal Path shape: ${Type.prettyPrint[PathType]}!!")
// $COVERAGE-ON$
}
Expand Down Expand Up @@ -553,7 +553,7 @@ private[compiletime] trait Configurations { this: Derivation =>
.reverse // ...and this is: "foo.bar.baz$", "foo.bar$baz$", "foo$bar$baz$"
.collectFirst { case Comparison(value) => value } // attempts: top-level object, object in object, etc
.getOrElse {
// $COVERAGE-OFF$
// $COVERAGE-OFF$should never happen unless someone mess around with type-level representation
reportError(
s"Invalid TransformerNamesComparison type - only (case) objects are allowed, and only the ones defined as top-level or in top-level objects, got: ${Type
.prettyPrint[Comparison]}!!!"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,9 @@ private[compiletime] trait TransformProductToProductRuleModule { this: Derivatio
import res1.{Underlying as Res1, value as result1Expr}, res2.{Underlying as Res2, value as result2Expr}
ctx match {
case TransformationContext.ForTotal(_) =>
// $COVERAGE-OFF$should never happen unless we messed up
assertionFailed("Expected partial while got total")
// $COVERAGE-ON$
case TransformationContext.ForPartial(_, failFast) =>
TransformationExpr.fromPartial(
ChimneyExpr.PartialResult.map2(
Expand Down Expand Up @@ -667,7 +669,9 @@ private[compiletime] trait TransformProductToProductRuleModule { this: Derivatio

ctx match {
case TransformationContext.ForTotal(_) =>
// $COVERAGE-OFF$should never happen unless we messed up
assertionFailed("Expected partial, got total")
// $COVERAGE-ON$
case TransformationContext.ForPartial(_, failFast) =>
// Finally, we are combining:
// if (${ failFast }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ private[compiletime] trait TransformationRules { this: Derivation =>
final def isPartial: Boolean = fold(_ => false)(_ => true)

final def ensureTotal: Expr[A] = fold(identity) { expr =>
// $COVERAGE-OFF$should never happen unless we messed up
assertionFailed(
s"Derived partial.Result expression where total Transformer expects direct value: ${Expr.prettyPrint(expr)}"
)
// $COVERAGE-ON$
}
final def ensurePartial: Expr[partial.Result[A]] = fold { expr =>
implicit val A: Type[A] = Expr.typeOf(expr)
Expand Down

0 comments on commit ae098a6

Please sign in to comment.