Skip to content

Commit 01f46c9

Browse files
authored
Merge pull request scalacenter#391 from sjrd/finalize-constant
Make Constants.Constant final.
2 parents 88257fd + 15dff42 commit 01f46c9

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

build.sbt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,24 @@ lazy val tastyQuery =
112112
mimaBinaryIssueFilters ++= {
113113
import com.typesafe.tools.mima.core.*
114114
Seq(
115+
// !!! Compatibility breach, but there was no way someone could have maningfully extended `Constant`
116+
ProblemFilters.exclude[FinalClassProblem]("tastyquery.Constants$Constant"),
115117
// Everything in tastyquery.reader is private[tastyquery] at most
116118
ProblemFilters.exclude[Problem]("tastyquery.reader.*"),
117119
)
118120
},
119121

120122
tastyMiMaPreviousArtifacts := mimaPreviousArtifacts.value,
121123
tastyMiMaConfig ~= { prev =>
124+
import tastymima.intf._
122125
prev
123126
.withMoreArtifactPrivatePackages(java.util.Arrays.asList(
124127
"tastyquery",
125128
))
129+
.withMoreProblemFilters(java.util.Arrays.asList(
130+
// !!! Compatibility breach, but there was no way someone could have maningfully extended `Constant`
131+
ProblemMatcher.make(ProblemKind.RestrictedOpenLevelChange, "tastyquery.Constants.Constant"),
132+
))
126133
},
127134
)
128135
.jvmSettings(

tasty-query/shared/src/main/scala/tastyquery/Constants.scala

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package tastyquery
22

3+
import scala.annotation.switch
4+
35
import scala.compiletime.asMatchable
46

57
import tastyquery.Contexts.*
@@ -20,7 +22,30 @@ object Constants {
2022
final val NullTag = 11
2123
final val ClazzTag = 12
2224

23-
class Constant(val value: Matchable, val tag: Int) {
25+
final class Constant private (val value: Matchable, val tag: Int, internal: Boolean) {
26+
@deprecated("this constructor is unsafe; use the `apply` methods in the companion instead", since = "1.1.0")
27+
def this(value: Matchable, tag: Int) =
28+
this(value, tag, internal = true)
29+
30+
// Check that the tag matches the value:
31+
val valid = (tag: @switch) match
32+
case UnitTag => value == ()
33+
case BooleanTag => value.isInstanceOf[Boolean]
34+
case CharTag => value.isInstanceOf[Char]
35+
case ByteTag => value.isInstanceOf[Byte]
36+
case ShortTag => value.isInstanceOf[Short]
37+
case IntTag => value.isInstanceOf[Int]
38+
case LongTag => value.isInstanceOf[Long]
39+
case FloatTag => value.isInstanceOf[Float]
40+
case DoubleTag => value.isInstanceOf[Double]
41+
case StringTag => value.isInstanceOf[String]
42+
case NullTag => value == null
43+
case ClazzTag => value.isInstanceOf[Type]
44+
case _ => false
45+
46+
require(valid, s"Illegal combination of value and tag for Constant($value, $tag)")
47+
end this
48+
2449
def wideType(using Context): Type = tag match
2550
case UnitTag => defn.UnitType
2651
case BooleanTag => defn.BooleanType
@@ -160,18 +185,18 @@ object Constants {
160185
}
161186

162187
object Constant {
163-
def apply(x: Null): Constant = new Constant(x, NullTag)
164-
def apply(x: Unit): Constant = new Constant(x, UnitTag)
165-
def apply(x: Boolean): Constant = new Constant(x, BooleanTag)
166-
def apply(x: Byte): Constant = new Constant(x, ByteTag)
167-
def apply(x: Short): Constant = new Constant(x, ShortTag)
168-
def apply(x: Int): Constant = new Constant(x, IntTag)
169-
def apply(x: Long): Constant = new Constant(x, LongTag)
170-
def apply(x: Float): Constant = new Constant(x, FloatTag)
171-
def apply(x: Double): Constant = new Constant(x, DoubleTag)
172-
def apply(x: String): Constant = new Constant(x, StringTag)
173-
def apply(x: Char): Constant = new Constant(x, CharTag)
174-
def apply(x: Type): Constant = new Constant(x, ClazzTag)
188+
def apply(x: Null): Constant = new Constant(x, NullTag, internal = true)
189+
def apply(x: Unit): Constant = new Constant(x, UnitTag, internal = true)
190+
def apply(x: Boolean): Constant = new Constant(x, BooleanTag, internal = true)
191+
def apply(x: Byte): Constant = new Constant(x, ByteTag, internal = true)
192+
def apply(x: Short): Constant = new Constant(x, ShortTag, internal = true)
193+
def apply(x: Int): Constant = new Constant(x, IntTag, internal = true)
194+
def apply(x: Long): Constant = new Constant(x, LongTag, internal = true)
195+
def apply(x: Float): Constant = new Constant(x, FloatTag, internal = true)
196+
def apply(x: Double): Constant = new Constant(x, DoubleTag, internal = true)
197+
def apply(x: String): Constant = new Constant(x, StringTag, internal = true)
198+
def apply(x: Char): Constant = new Constant(x, CharTag, internal = true)
199+
def apply(x: Type): Constant = new Constant(x, ClazzTag, internal = true)
175200

176201
def unapply(c: Constant): Constant = c
177202
}

0 commit comments

Comments
 (0)