Skip to content

Remove useless constant extractors #6799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
37 changes: 3 additions & 34 deletions compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1455,46 +1455,15 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
def Constant_value(const: Constant): Any = const.value

def matchConstant(constant: Constant): Option[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type] =
Some(constant.asInstanceOf[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type])

def matchConstant_Unit(x: Constant): Boolean = x.tag == Constants.UnitTag
def matchConstant_Null(x: Constant): Boolean = x.tag == Constants.NullTag
def matchConstant_Boolean(x: Constant): Option[Boolean] =
if (x.tag == Constants.BooleanTag) Some(x.booleanValue) else None
def matchConstant_Byte(x: Constant): Option[Byte] =
if (x.tag == Constants.ByteTag) Some(x.byteValue) else None
def matchConstant_Short(x: Constant): Option[Short] =
if (x.tag == Constants.ShortTag) Some(x.shortValue) else None
def matchConstant_Char(x: Constant): Option[Char] =
if (x.tag == Constants.CharTag) Some(x.charValue) else None
def matchConstant_Int(x: Constant): Option[Int] =
if (x.tag == Constants.IntTag) Some(x.intValue) else None
def matchConstant_Long(x: Constant): Option[Long] =
if (x.tag == Constants.LongTag) Some(x.longValue) else None
def matchConstant_Float(x: Constant): Option[Float] =
if (x.tag == Constants.FloatTag) Some(x.floatValue) else None
def matchConstant_Double(x: Constant): Option[Double] =
if (x.tag == Constants.DoubleTag) Some(x.doubleValue) else None
def matchConstant_String(x: Constant): Option[String] =
if (x.tag == Constants.StringTag) Some(x.stringValue) else None
Some(constant.value.asInstanceOf[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type])

def matchConstant_ClassTag(x: Constant): Option[Type] =
if (x.tag == Constants.ClazzTag) Some(x.typeValue) else None

def Constant_apply(x: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type): Constant =
Constants.Constant(x)

def Constant_Unit_apply(): Constant = Constants.Constant(())
def Constant_Null_apply(): Constant = Constants.Constant(null)
def Constant_Boolean_apply(x: Boolean): Constant = Constants.Constant(x)
def Constant_Byte_apply(x: Byte): Constant = Constants.Constant(x)
def Constant_Short_apply(x: Short): Constant = Constants.Constant(x)
def Constant_Char_apply(x: Char): Constant = Constants.Constant(x)
def Constant_Int_apply(x: Int): Constant = Constants.Constant(x)
def Constant_Long_apply(x: Long): Constant = Constants.Constant(x)
def Constant_Float_apply(x: Float): Constant = Constants.Constant(x)
def Constant_Double_apply(x: Double): Constant = Constants.Constant(x)
def Constant_String_apply(x: String): Constant = Constants.Constant(x)
def Constant_ClassTag_apply(x: scala.reflect.ClassTag[_]): Constant = Constants.Constant(x)
def Constant_ClassTag_apply(x: Type): Constant = Constants.Constant(x)

//
// SYMBOLS
Expand Down
125 changes: 1 addition & 124 deletions library/src/scala/tasty/reflect/ConstantOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,133 +16,10 @@ trait ConstantOps extends Core {
def unapply(constant: Constant): Option[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type] =
kernel.matchConstant(constant)

// TODO remove all extractors bellow and use only use the two above

/** Module of Null literals */
object Unit {
/** Unit `()` literal */
def apply(): Constant =
kernel.Constant_Unit_apply()

/** Extractor for Unit literals */
def unapply(constant: Constant): Boolean =
kernel.matchConstant_Unit(constant)
}

/** Module of Null literals */
object Null {
/** `null` literal */
def apply(): Constant =
kernel.Constant_Null_apply()

/** Extractor for Null literals */
def unapply(constant: Constant): Boolean =
kernel.matchConstant_Null(constant)
}

/** Module of Boolean literals */
object Boolean {
/** Boolean literal */
def apply(x: Boolean): Constant =
kernel.Constant_Boolean_apply(x)

/** Extractor for Boolean literals */
def unapply(constant: Constant): Option[Boolean] =
kernel.matchConstant_Boolean(constant)
}

/** Module of Byte literals */
object Byte {
/** Byte literal */
def apply(x: Byte): Constant =
kernel.Constant_Byte_apply(x)

/** Extractor for Byte literals */
def unapply(constant: Constant): Option[Byte] =
kernel.matchConstant_Byte(constant)
}

/** Module of Short literals */
object Short {
/** Short literal */
def apply(x: Short): Constant =
kernel.Constant_Short_apply(x)

/** Extractor for Short literals */
def unapply(constant: Constant): Option[Short] =
kernel.matchConstant_Short(constant)
}

/** Module of Char literals */
object Char {
/** Char literal */
def apply(x: Char): Constant =
kernel.Constant_Char_apply(x)

/** Extractor for Char literals */
def unapply(constant: Constant): Option[Char] =
kernel.matchConstant_Char(constant)
}

/** Module of Int literals */
object Int {
/** Int literal */
def apply(x: Int): Constant =
kernel.Constant_Int_apply(x)

/** Extractor for Int literals */
def unapply(constant: Constant): Option[Int] =
kernel.matchConstant_Int(constant)
}

/** Module of Long literals */
object Long {
/** Long literal */
def apply(x: Long): Constant =
kernel.Constant_Long_apply(x)

/** Extractor for Long literals */
def unapply(constant: Constant): Option[Long] =
kernel.matchConstant_Long(constant)
}

/** Module of Float literals */
object Float {
/** Float literal */
def apply(x: Float): Constant =
kernel.Constant_Float_apply(x)

/** Extractor for Float literals */
def unapply(constant: Constant): Option[Float] =
kernel.matchConstant_Float(constant)
}

/** Module of Double literals */
object Double {
/** Double literal */
def apply(x: Double): Constant =
kernel.Constant_Double_apply(x)

/** Extractor for Double literals */
def unapply(constant: Constant): Option[Double] =
kernel.matchConstant_Double(constant)
}

/** Module of String literals */
object String {
/** String literal */
def apply(x: String): Constant =
kernel.Constant_String_apply(x)

/** Extractor for String literals */
def unapply(constant: Constant): Option[String] =
kernel.matchConstant_String(constant)
}

/** Module of ClassTag literals */
object ClassTag {
/** scala.reflect.ClassTag literal */
def apply[T](implicit x: scala.reflect.ClassTag[T]): Constant =
def apply[T](implicit x: Type): Constant =
kernel.Constant_ClassTag_apply(x)

/** Extractor for ClassTag literals */
Expand Down
24 changes: 1 addition & 23 deletions library/src/scala/tasty/reflect/Kernel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1179,32 +1179,10 @@ trait Kernel {
def Constant_value(const: Constant): Any

def matchConstant(constant: Constant): Option[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type]
def matchConstant_Unit(constant: Constant): Boolean
def matchConstant_Null(constant: Constant): Boolean
def matchConstant_Boolean(constant: Constant): Option[Boolean]
def matchConstant_Byte(constant: Constant): Option[Byte]
def matchConstant_Short(constant: Constant): Option[Short]
def matchConstant_Char(constant: Constant): Option[Char]
def matchConstant_Int(constant: Constant): Option[Int]
def matchConstant_Long(constant: Constant): Option[Long]
def matchConstant_Float(constant: Constant): Option[Float]
def matchConstant_Double(constant: Constant): Option[Double]
def matchConstant_String(constant: Constant): Option[String]
def matchConstant_ClassTag(constant: Constant): Option[Type]

def Constant_apply(x: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type): Constant
def Constant_Unit_apply(): Constant
def Constant_Null_apply(): Constant
def Constant_Boolean_apply(x: Boolean): Constant
def Constant_Byte_apply(x: Byte): Constant
def Constant_Short_apply(x: Short): Constant
def Constant_Char_apply(x: Char): Constant
def Constant_Int_apply(x: Int): Constant
def Constant_Long_apply(x: Long): Constant
def Constant_Float_apply(x: Float): Constant
def Constant_Double_apply(x: Double): Constant
def Constant_String_apply(x: String): Constant
def Constant_ClassTag_apply(x: scala.reflect.ClassTag[_]): Constant
def Constant_ClassTag_apply(x: Type): Constant

//
// SYMBOLS
Expand Down
52 changes: 27 additions & 25 deletions library/src/scala/tasty/reflect/Printers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,20 @@ trait Printers
}

def visitConstant(x: Constant): Buffer = x match {
case Constant.Unit() => this += "Constant.Unit()"
case Constant.Null() => this += "Constant.Null()"
case Constant.Boolean(value) => this += "Constant.Boolean(" += value += ")"
case Constant.Byte(value) => this += "Constant.Byte(" += value += ")"
case Constant.Short(value) => this += "Constant.Short(" += value += ")"
case Constant.Char(value) => this += "Constant.Char(" += value += ")"
case Constant.Int(value) => this += "Constant.Int(" += value.toString += ")"
case Constant.Long(value) => this += "Constant.Long(" += value += ")"
case Constant.Float(value) => this += "Constant.Float(" += value += ")"
case Constant.Double(value) => this += "Constant.Double(" += value += ")"
case Constant.String(value) => this += "Constant.String(\"" += value += "\")"
case Constant.ClassTag(value) => this += "Constant.ClassTag(" += value += ")"
case Constant(()) => this += "Constant(())"
case Constant(null) => this += "Constant(null)"
case Constant(value: Boolean) => this += "Constant(" += value += ")"
case Constant(value: Byte) => this += "Constant(" += value += ": Byte)"
case Constant(value: Short) => this += "Constant(" += value += ": Short)"
case Constant(value: Char) => this += "Constant('" += value += "')"
case Constant(value: Int) => this += "Constant(" += value.toString += ")"
case Constant(value: Long) => this += "Constant(" += value += "L)"
case Constant(value: Float) => this += "Constant(" += value += "f)"
case Constant(value: Double) => this += "Constant(" += value += "d)"
case Constant(value: String) => this += "Constant(\"" += value += "\")"
case Constant.ClassTag(value) =>
this += "Constant.ClassTag("
visitType(value) += ")"
}

def visitType(x: TypeOrBounds): Buffer = x match {
Expand Down Expand Up @@ -728,7 +730,7 @@ trait Printers

case While(cond, body) =>
(cond, body) match {
case (Block(Block(Nil, body1) :: Nil, Block(Nil, cond1)), Literal(Constant.Unit())) =>
case (Block(Block(Nil, body1) :: Nil, Block(Nil, cond1)), Literal(Constant(()))) =>
this += highlightKeyword("do ")
printTree(body1) += highlightKeyword(" while ")
inParens(printTree(cond1))
Expand Down Expand Up @@ -990,7 +992,7 @@ trait Printers
while (it.hasNext)
extractFlatStats(it.next())
extractFlatStats(expansion)
case Literal(Constant.Unit()) => // ignore
case Literal(Constant(())) => // ignore
case stat => flatStats += stat
}
def extractFlatExpr(term: Term): Term = term match {
Expand Down Expand Up @@ -1374,17 +1376,17 @@ trait Printers
}

def printConstant(const: Constant): Buffer = const match {
case Constant.Unit() => this += highlightLiteral("()")
case Constant.Null() => this += highlightLiteral("null")
case Constant.Boolean(v) => this += highlightLiteral(v.toString)
case Constant.Byte(v) => this += highlightLiteral(v.toString)
case Constant.Short(v) => this += highlightLiteral(v.toString)
case Constant.Int(v) => this += highlightLiteral(v.toString)
case Constant.Long(v) => this += highlightLiteral(v.toString + "L")
case Constant.Float(v) => this += highlightLiteral(v.toString + "f")
case Constant.Double(v) => this += highlightLiteral(v.toString)
case Constant.Char(v) => this += highlightString('\'' + escapedChar(v) + '\'')
case Constant.String(v) => this += highlightString('"' + escapedString(v) + '"')
case Constant(()) => this += highlightLiteral("()")
case Constant(null) => this += highlightLiteral("null")
case Constant(v: Boolean) => this += highlightLiteral(v.toString)
case Constant(v: Byte) => this += highlightLiteral(v.toString)
case Constant(v: Short) => this += highlightLiteral(v.toString)
case Constant(v: Int) => this += highlightLiteral(v.toString)
case Constant(v: Long) => this += highlightLiteral(v.toString + "L")
case Constant(v: Float) => this += highlightLiteral(v.toString + "f")
case Constant(v: Double) => this += highlightLiteral(v.toString)
case Constant(v: Char) => this += highlightString('\'' + escapedChar(v) + '\'')
case Constant(v: String) => this += highlightString('"' + escapedString(v) + '"')
case Constant.ClassTag(v) =>
this += "classOf"
inSquare(printType(v))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TastyScalaFileInferrer extends TastyConsumer {
cdef.symbol.annots.foreach { annot =>
annot match {
case Apply(Select(New(t), _),
List(Literal(Constant.String(path))))
List(Literal(Constant(path: String))))
if t.symbol.name == "SourceFile" =>
// we found the path to a file. In this case, we do not need to
// continue traversing the tree
Expand Down
6 changes: 3 additions & 3 deletions tests/pos-macros/tasty-constant-type/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ object Macro {
def impl[A <: Int : Type, B <: Int : Type](a: Type[A], b: Type[B]) given (qctx: QuoteContext): Expr[AddInt[A, B]] = {
import qctx.tasty._

val Type.ConstantType(Constant.Int(v1)) = a.unseal.tpe
val Type.ConstantType(Constant.Int(v2)) = b.unseal.tpe
val Type.ConstantType(Constant(v1: Int)) = a.unseal.tpe
val Type.ConstantType(Constant(v2: Int)) = b.unseal.tpe

val t = Literal(Constant.Int(v1 + v2)).tpe.seal
val t = Literal(Constant((v1 + v2): Int)).tpe.seal

'{ null: AddInt[$a, $b] { type Out = $t } }
}
Expand Down
4 changes: 2 additions & 2 deletions tests/run-custom-args/Yretain-trees/tasty-definitions-2.check
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
DefDef("foo", Nil, Nil, TypeIdent("Int"), Some(Apply(Select(Literal(Constant.Int(1)), "+"), List(Literal(Constant.Int(2))))))
ValDef("bar", TypeIdent("Int"), Some(Apply(Select(Literal(Constant.Int(2)), "+"), List(Literal(Constant.Int(3))))))
DefDef("foo", Nil, Nil, TypeIdent("Int"), Some(Apply(Select(Literal(Constant(1)), "+"), List(Literal(Constant(2))))))
ValDef("bar", TypeIdent("Int"), Some(Apply(Select(Literal(Constant(2)), "+"), List(Literal(Constant(3))))))
Pattern.Bind("x", Pattern.WildcardPattern())
4 changes: 2 additions & 2 deletions tests/run-custom-args/Yretain-trees/tasty-load-tree-1.check
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DefDef("foo", Nil, Nil, TypeIdent("Int"), Some(Apply(Select(Literal(Constant.Int(1)), "+"), List(Literal(Constant.Int(2))))))
ValDef("bar", TypeIdent("Int"), Some(Apply(Select(Literal(Constant.Int(2)), "+"), List(Literal(Constant.Int(3))))))
DefDef("foo", Nil, Nil, TypeIdent("Int"), Some(Apply(Select(Literal(Constant(1)), "+"), List(Literal(Constant(2))))))
ValDef("bar", TypeIdent("Int"), Some(Apply(Select(Literal(Constant(2)), "+"), List(Literal(Constant(3))))))
2 changes: 1 addition & 1 deletion tests/run-macros/f-interpolation-1/FQuote_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object FQuote {
if isSCOpsConversion(conv) &&
isStringContextApply(fun) &&
values.forall(isStringConstant) =>
values.collect { case Literal(Constant.String(value)) => value }
values.collect { case Literal(Constant(value: String)) => value }
case tree =>
QuoteError(s"String literal expected, but ${tree.showExtractors} found")
}
Expand Down
4 changes: 2 additions & 2 deletions tests/run-macros/i5119.check
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Select(Typed(Apply(Select(New(TypeIdent("StringContextOps")), "<init>"), List(Apply(Select(Select(Select(Ident("_root_"), "scala"), "StringContext"), "apply"), List(Typed(Repeated(List(Literal(Constant.String("Hello World ")), Literal(Constant.String("!"))), Inferred()), Inferred()))))), TypeIdent("StringContextOps")), "inline$sc")
Typed(Repeated(List(Literal(Constant.Int(1))), Inferred()), Inferred())
Select(Typed(Apply(Select(New(TypeIdent("StringContextOps")), "<init>"), List(Apply(Select(Select(Select(Ident("_root_"), "scala"), "StringContext"), "apply"), List(Typed(Repeated(List(Literal(Constant("Hello World ")), Literal(Constant("!"))), Inferred()), Inferred()))))), TypeIdent("StringContextOps")), "inline$sc")
Typed(Repeated(List(Literal(Constant(1))), Inferred()), Inferred())
8 changes: 4 additions & 4 deletions tests/run-macros/i5119b.check
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Apply(Ident("foo"), List(Literal(Constant.Int(1))))
Apply(Ident("foo"), List(Literal(Constant.Int(2))))
Apply(Ident("foo"), List(Literal(Constant.Int(4))))
Apply(Ident("foo"), List(Literal(Constant.Int(3))))
Apply(Ident("foo"), List(Literal(Constant(1))))
Apply(Ident("foo"), List(Literal(Constant(2))))
Apply(Ident("foo"), List(Literal(Constant(4))))
Apply(Ident("foo"), List(Literal(Constant(3))))
Loading