Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions compiler/src/dotty/tools/dotc/core/TypeEval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package dotty.tools
package dotc
package core

import scala.reflect.Typeable

import Types.*, Contexts.*, Symbols.*, Constants.*, Decorators.*
import config.Printers.typr
import reporting.trace
Expand All @@ -26,33 +28,22 @@ object TypeEval:
if tp1.isStable then tp1.fixForEvaluation else tp
case tp => tp

def constValue(tp: Type): Option[Any] = tp.fixForEvaluation match
case ConstantType(Constant(n)) => Some(n)
case _ => None
extension (tp: Type) def constant[T: Typeable]: Option[T] =
TypeComparer.constValue(tp.fixForEvaluation).collect { case Constant(c: T) => c }

def constValue(tp: Type): Option[Any] = tp.constant[Any]

def boolValue(tp: Type): Option[Boolean] = tp.fixForEvaluation match
case ConstantType(Constant(n: Boolean)) => Some(n)
case _ => None
def boolValue(tp: Type): Option[Boolean] = tp.constant[Boolean]

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

def longValue(tp: Type): Option[Long] = tp.fixForEvaluation match
case ConstantType(Constant(n: Long)) => Some(n)
case _ => None
def longValue(tp: Type): Option[Long] = tp.constant[Long]

def floatValue(tp: Type): Option[Float] = tp.fixForEvaluation match
case ConstantType(Constant(n: Float)) => Some(n)
case _ => None
def floatValue(tp: Type): Option[Float] = tp.constant[Float]

def doubleValue(tp: Type): Option[Double] = tp.fixForEvaluation match
case ConstantType(Constant(n: Double)) => Some(n)
case _ => None
def doubleValue(tp: Type): Option[Double] = tp.constant[Double]

def stringValue(tp: Type): Option[String] = tp.fixForEvaluation match
case ConstantType(Constant(n: String)) => Some(n)
case _ => None
def stringValue(tp: Type): Option[String] = tp.constant[String]

// Returns Some(true) if the type is a constant.
// Returns Some(false) if the type is not a constant.
Expand Down
13 changes: 13 additions & 0 deletions tests/pos/i24717.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import scala.compiletime.ops.int.+
import scala.compiletime.ops.int.S

object test {
object O {
opaque type O = Int
transparent inline def v: O = 123
}

val a: 123 & O.O = O.v
val b: S[a.type] = 124
val c: a.type + 1 = 124
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces some inconsistency as

val d: a.type + 1 = a + 1

will still not compiles as a + 1 does not get constant folded.

Should ConstFold also use TypeComparer for extracting the constant type?

}
Loading