Skip to content

Commit 74b0258

Browse files
committed
Use TypeComparer.constValue in TypeEval
This uses `TypeComparer.constValue` to extract the constant in TypeEval. This is also how constValue is implemented. So I think also using it in TypeEval make behavior around this more consistent and predictable. This fixes: #24717
1 parent 577721f commit 74b0258

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

compiler/src/dotty/tools/dotc/core/TypeEval.scala

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty.tools
22
package dotc
33
package core
44

5+
import scala.reflect.Typeable
6+
57
import Types.*, Contexts.*, Symbols.*, Constants.*, Decorators.*
68
import config.Printers.typr
79
import reporting.trace
@@ -26,33 +28,22 @@ object TypeEval:
2628
if tp1.isStable then tp1.fixForEvaluation else tp
2729
case tp => tp
2830

29-
def constValue(tp: Type): Option[Any] = tp.fixForEvaluation match
30-
case ConstantType(Constant(n)) => Some(n)
31-
case _ => None
31+
extension (tp: Type) def constant[T: Typeable]: Option[T] =
32+
TypeComparer.constValue(tp.fixForEvaluation).collect { case Constant(c: T) => c }
33+
34+
def constValue(tp: Type): Option[Any] = tp.constant[Any]
3235

33-
def boolValue(tp: Type): Option[Boolean] = tp.fixForEvaluation match
34-
case ConstantType(Constant(n: Boolean)) => Some(n)
35-
case _ => None
36+
def boolValue(tp: Type): Option[Boolean] = tp.constant[Boolean]
3637

37-
def intValue(tp: Type): Option[Int] = tp.fixForEvaluation match
38-
case ConstantType(Constant(n: Int)) => Some(n)
39-
case _ => None
38+
def intValue(tp: Type): Option[Int] = tp.constant[Int]
4039

41-
def longValue(tp: Type): Option[Long] = tp.fixForEvaluation match
42-
case ConstantType(Constant(n: Long)) => Some(n)
43-
case _ => None
40+
def longValue(tp: Type): Option[Long] = tp.constant[Long]
4441

45-
def floatValue(tp: Type): Option[Float] = tp.fixForEvaluation match
46-
case ConstantType(Constant(n: Float)) => Some(n)
47-
case _ => None
42+
def floatValue(tp: Type): Option[Float] = tp.constant[Float]
4843

49-
def doubleValue(tp: Type): Option[Double] = tp.fixForEvaluation match
50-
case ConstantType(Constant(n: Double)) => Some(n)
51-
case _ => None
44+
def doubleValue(tp: Type): Option[Double] = tp.constant[Double]
5245

53-
def stringValue(tp: Type): Option[String] = tp.fixForEvaluation match
54-
case ConstantType(Constant(n: String)) => Some(n)
55-
case _ => None
46+
def stringValue(tp: Type): Option[String] = tp.constant[String]
5647

5748
// Returns Some(true) if the type is a constant.
5849
// Returns Some(false) if the type is not a constant.

tests/pos/i24717.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.compiletime.ops.int.+
2+
import scala.compiletime.ops.int.S
3+
4+
object test {
5+
object O {
6+
opaque type O = Int
7+
transparent inline def v: O = 123
8+
}
9+
10+
val a: 123 & O.O = O.v
11+
val b: S[a.type] = 124
12+
val c: a.type + 1 = 124
13+
}

0 commit comments

Comments
 (0)