Skip to content

Commit

Permalink
Merge pull request #391 from sjrd/finalize-constant
Browse files Browse the repository at this point in the history
Make Constants.Constant final.
  • Loading branch information
sjrd authored Nov 20, 2023
2 parents 88257fd + 15dff42 commit 01f46c9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
7 changes: 7 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,24 @@ lazy val tastyQuery =
mimaBinaryIssueFilters ++= {
import com.typesafe.tools.mima.core.*
Seq(
// !!! Compatibility breach, but there was no way someone could have maningfully extended `Constant`
ProblemFilters.exclude[FinalClassProblem]("tastyquery.Constants$Constant"),
// Everything in tastyquery.reader is private[tastyquery] at most
ProblemFilters.exclude[Problem]("tastyquery.reader.*"),
)
},

tastyMiMaPreviousArtifacts := mimaPreviousArtifacts.value,
tastyMiMaConfig ~= { prev =>
import tastymima.intf._
prev
.withMoreArtifactPrivatePackages(java.util.Arrays.asList(
"tastyquery",
))
.withMoreProblemFilters(java.util.Arrays.asList(
// !!! Compatibility breach, but there was no way someone could have maningfully extended `Constant`
ProblemMatcher.make(ProblemKind.RestrictedOpenLevelChange, "tastyquery.Constants.Constant"),
))
},
)
.jvmSettings(
Expand Down
51 changes: 38 additions & 13 deletions tasty-query/shared/src/main/scala/tastyquery/Constants.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tastyquery

import scala.annotation.switch

import scala.compiletime.asMatchable

import tastyquery.Contexts.*
Expand All @@ -20,7 +22,30 @@ object Constants {
final val NullTag = 11
final val ClazzTag = 12

class Constant(val value: Matchable, val tag: Int) {
final class Constant private (val value: Matchable, val tag: Int, internal: Boolean) {
@deprecated("this constructor is unsafe; use the `apply` methods in the companion instead", since = "1.1.0")
def this(value: Matchable, tag: Int) =
this(value, tag, internal = true)

// Check that the tag matches the value:
val valid = (tag: @switch) match
case UnitTag => value == ()
case BooleanTag => value.isInstanceOf[Boolean]
case CharTag => value.isInstanceOf[Char]
case ByteTag => value.isInstanceOf[Byte]
case ShortTag => value.isInstanceOf[Short]
case IntTag => value.isInstanceOf[Int]
case LongTag => value.isInstanceOf[Long]
case FloatTag => value.isInstanceOf[Float]
case DoubleTag => value.isInstanceOf[Double]
case StringTag => value.isInstanceOf[String]
case NullTag => value == null
case ClazzTag => value.isInstanceOf[Type]
case _ => false

require(valid, s"Illegal combination of value and tag for Constant($value, $tag)")
end this

def wideType(using Context): Type = tag match
case UnitTag => defn.UnitType
case BooleanTag => defn.BooleanType
Expand Down Expand Up @@ -160,18 +185,18 @@ object Constants {
}

object Constant {
def apply(x: Null): Constant = new Constant(x, NullTag)
def apply(x: Unit): Constant = new Constant(x, UnitTag)
def apply(x: Boolean): Constant = new Constant(x, BooleanTag)
def apply(x: Byte): Constant = new Constant(x, ByteTag)
def apply(x: Short): Constant = new Constant(x, ShortTag)
def apply(x: Int): Constant = new Constant(x, IntTag)
def apply(x: Long): Constant = new Constant(x, LongTag)
def apply(x: Float): Constant = new Constant(x, FloatTag)
def apply(x: Double): Constant = new Constant(x, DoubleTag)
def apply(x: String): Constant = new Constant(x, StringTag)
def apply(x: Char): Constant = new Constant(x, CharTag)
def apply(x: Type): Constant = new Constant(x, ClazzTag)
def apply(x: Null): Constant = new Constant(x, NullTag, internal = true)
def apply(x: Unit): Constant = new Constant(x, UnitTag, internal = true)
def apply(x: Boolean): Constant = new Constant(x, BooleanTag, internal = true)
def apply(x: Byte): Constant = new Constant(x, ByteTag, internal = true)
def apply(x: Short): Constant = new Constant(x, ShortTag, internal = true)
def apply(x: Int): Constant = new Constant(x, IntTag, internal = true)
def apply(x: Long): Constant = new Constant(x, LongTag, internal = true)
def apply(x: Float): Constant = new Constant(x, FloatTag, internal = true)
def apply(x: Double): Constant = new Constant(x, DoubleTag, internal = true)
def apply(x: String): Constant = new Constant(x, StringTag, internal = true)
def apply(x: Char): Constant = new Constant(x, CharTag, internal = true)
def apply(x: Type): Constant = new Constant(x, ClazzTag, internal = true)

def unapply(c: Constant): Constant = c
}
Expand Down

0 comments on commit 01f46c9

Please sign in to comment.