Skip to content

Commit 125b0e1

Browse files
committed
Remove useless constant extractors
1 parent 65901c5 commit 125b0e1

File tree

22 files changed

+151
-325
lines changed

22 files changed

+151
-325
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,46 +1455,15 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
14551455
def Constant_value(const: Constant): Any = const.value
14561456

14571457
def matchConstant(constant: Constant): Option[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type] =
1458-
Some(constant.asInstanceOf[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type])
1459-
1460-
def matchConstant_Unit(x: Constant): Boolean = x.tag == Constants.UnitTag
1461-
def matchConstant_Null(x: Constant): Boolean = x.tag == Constants.NullTag
1462-
def matchConstant_Boolean(x: Constant): Option[Boolean] =
1463-
if (x.tag == Constants.BooleanTag) Some(x.booleanValue) else None
1464-
def matchConstant_Byte(x: Constant): Option[Byte] =
1465-
if (x.tag == Constants.ByteTag) Some(x.byteValue) else None
1466-
def matchConstant_Short(x: Constant): Option[Short] =
1467-
if (x.tag == Constants.ShortTag) Some(x.shortValue) else None
1468-
def matchConstant_Char(x: Constant): Option[Char] =
1469-
if (x.tag == Constants.CharTag) Some(x.charValue) else None
1470-
def matchConstant_Int(x: Constant): Option[Int] =
1471-
if (x.tag == Constants.IntTag) Some(x.intValue) else None
1472-
def matchConstant_Long(x: Constant): Option[Long] =
1473-
if (x.tag == Constants.LongTag) Some(x.longValue) else None
1474-
def matchConstant_Float(x: Constant): Option[Float] =
1475-
if (x.tag == Constants.FloatTag) Some(x.floatValue) else None
1476-
def matchConstant_Double(x: Constant): Option[Double] =
1477-
if (x.tag == Constants.DoubleTag) Some(x.doubleValue) else None
1478-
def matchConstant_String(x: Constant): Option[String] =
1479-
if (x.tag == Constants.StringTag) Some(x.stringValue) else None
1458+
Some(constant.value.asInstanceOf[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type])
1459+
14801460
def matchConstant_ClassTag(x: Constant): Option[Type] =
14811461
if (x.tag == Constants.ClazzTag) Some(x.typeValue) else None
14821462

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

1486-
def Constant_Unit_apply(): Constant = Constants.Constant(())
1487-
def Constant_Null_apply(): Constant = Constants.Constant(null)
1488-
def Constant_Boolean_apply(x: Boolean): Constant = Constants.Constant(x)
1489-
def Constant_Byte_apply(x: Byte): Constant = Constants.Constant(x)
1490-
def Constant_Short_apply(x: Short): Constant = Constants.Constant(x)
1491-
def Constant_Char_apply(x: Char): Constant = Constants.Constant(x)
1492-
def Constant_Int_apply(x: Int): Constant = Constants.Constant(x)
1493-
def Constant_Long_apply(x: Long): Constant = Constants.Constant(x)
1494-
def Constant_Float_apply(x: Float): Constant = Constants.Constant(x)
1495-
def Constant_Double_apply(x: Double): Constant = Constants.Constant(x)
1496-
def Constant_String_apply(x: String): Constant = Constants.Constant(x)
1497-
def Constant_ClassTag_apply(x: scala.reflect.ClassTag[_]): Constant = Constants.Constant(x)
1466+
def Constant_ClassTag_apply(x: Type): Constant = Constants.Constant(x)
14981467

14991468
//
15001469
// SYMBOLS

library/src/scala/tasty/reflect/ConstantOps.scala

Lines changed: 1 addition & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -16,133 +16,10 @@ trait ConstantOps extends Core {
1616
def unapply(constant: Constant): Option[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type] =
1717
kernel.matchConstant(constant)
1818

19-
// TODO remove all extractors bellow and use only use the two above
20-
21-
/** Module of Null literals */
22-
object Unit {
23-
/** Unit `()` literal */
24-
def apply(): Constant =
25-
kernel.Constant_Unit_apply()
26-
27-
/** Extractor for Unit literals */
28-
def unapply(constant: Constant): Boolean =
29-
kernel.matchConstant_Unit(constant)
30-
}
31-
32-
/** Module of Null literals */
33-
object Null {
34-
/** `null` literal */
35-
def apply(): Constant =
36-
kernel.Constant_Null_apply()
37-
38-
/** Extractor for Null literals */
39-
def unapply(constant: Constant): Boolean =
40-
kernel.matchConstant_Null(constant)
41-
}
42-
43-
/** Module of Boolean literals */
44-
object Boolean {
45-
/** Boolean literal */
46-
def apply(x: Boolean): Constant =
47-
kernel.Constant_Boolean_apply(x)
48-
49-
/** Extractor for Boolean literals */
50-
def unapply(constant: Constant): Option[Boolean] =
51-
kernel.matchConstant_Boolean(constant)
52-
}
53-
54-
/** Module of Byte literals */
55-
object Byte {
56-
/** Byte literal */
57-
def apply(x: Byte): Constant =
58-
kernel.Constant_Byte_apply(x)
59-
60-
/** Extractor for Byte literals */
61-
def unapply(constant: Constant): Option[Byte] =
62-
kernel.matchConstant_Byte(constant)
63-
}
64-
65-
/** Module of Short literals */
66-
object Short {
67-
/** Short literal */
68-
def apply(x: Short): Constant =
69-
kernel.Constant_Short_apply(x)
70-
71-
/** Extractor for Short literals */
72-
def unapply(constant: Constant): Option[Short] =
73-
kernel.matchConstant_Short(constant)
74-
}
75-
76-
/** Module of Char literals */
77-
object Char {
78-
/** Char literal */
79-
def apply(x: Char): Constant =
80-
kernel.Constant_Char_apply(x)
81-
82-
/** Extractor for Char literals */
83-
def unapply(constant: Constant): Option[Char] =
84-
kernel.matchConstant_Char(constant)
85-
}
86-
87-
/** Module of Int literals */
88-
object Int {
89-
/** Int literal */
90-
def apply(x: Int): Constant =
91-
kernel.Constant_Int_apply(x)
92-
93-
/** Extractor for Int literals */
94-
def unapply(constant: Constant): Option[Int] =
95-
kernel.matchConstant_Int(constant)
96-
}
97-
98-
/** Module of Long literals */
99-
object Long {
100-
/** Long literal */
101-
def apply(x: Long): Constant =
102-
kernel.Constant_Long_apply(x)
103-
104-
/** Extractor for Long literals */
105-
def unapply(constant: Constant): Option[Long] =
106-
kernel.matchConstant_Long(constant)
107-
}
108-
109-
/** Module of Float literals */
110-
object Float {
111-
/** Float literal */
112-
def apply(x: Float): Constant =
113-
kernel.Constant_Float_apply(x)
114-
115-
/** Extractor for Float literals */
116-
def unapply(constant: Constant): Option[Float] =
117-
kernel.matchConstant_Float(constant)
118-
}
119-
120-
/** Module of Double literals */
121-
object Double {
122-
/** Double literal */
123-
def apply(x: Double): Constant =
124-
kernel.Constant_Double_apply(x)
125-
126-
/** Extractor for Double literals */
127-
def unapply(constant: Constant): Option[Double] =
128-
kernel.matchConstant_Double(constant)
129-
}
130-
131-
/** Module of String literals */
132-
object String {
133-
/** String literal */
134-
def apply(x: String): Constant =
135-
kernel.Constant_String_apply(x)
136-
137-
/** Extractor for String literals */
138-
def unapply(constant: Constant): Option[String] =
139-
kernel.matchConstant_String(constant)
140-
}
141-
14219
/** Module of ClassTag literals */
14320
object ClassTag {
14421
/** scala.reflect.ClassTag literal */
145-
def apply[T](implicit x: scala.reflect.ClassTag[T]): Constant =
22+
def apply[T](implicit x: Type): Constant =
14623
kernel.Constant_ClassTag_apply(x)
14724

14825
/** Extractor for ClassTag literals */

library/src/scala/tasty/reflect/Kernel.scala

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,32 +1179,10 @@ trait Kernel {
11791179
def Constant_value(const: Constant): Any
11801180

11811181
def matchConstant(constant: Constant): Option[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type]
1182-
def matchConstant_Unit(constant: Constant): Boolean
1183-
def matchConstant_Null(constant: Constant): Boolean
1184-
def matchConstant_Boolean(constant: Constant): Option[Boolean]
1185-
def matchConstant_Byte(constant: Constant): Option[Byte]
1186-
def matchConstant_Short(constant: Constant): Option[Short]
1187-
def matchConstant_Char(constant: Constant): Option[Char]
1188-
def matchConstant_Int(constant: Constant): Option[Int]
1189-
def matchConstant_Long(constant: Constant): Option[Long]
1190-
def matchConstant_Float(constant: Constant): Option[Float]
1191-
def matchConstant_Double(constant: Constant): Option[Double]
1192-
def matchConstant_String(constant: Constant): Option[String]
11931182
def matchConstant_ClassTag(constant: Constant): Option[Type]
11941183

11951184
def Constant_apply(x: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type): Constant
1196-
def Constant_Unit_apply(): Constant
1197-
def Constant_Null_apply(): Constant
1198-
def Constant_Boolean_apply(x: Boolean): Constant
1199-
def Constant_Byte_apply(x: Byte): Constant
1200-
def Constant_Short_apply(x: Short): Constant
1201-
def Constant_Char_apply(x: Char): Constant
1202-
def Constant_Int_apply(x: Int): Constant
1203-
def Constant_Long_apply(x: Long): Constant
1204-
def Constant_Float_apply(x: Float): Constant
1205-
def Constant_Double_apply(x: Double): Constant
1206-
def Constant_String_apply(x: String): Constant
1207-
def Constant_ClassTag_apply(x: scala.reflect.ClassTag[_]): Constant
1185+
def Constant_ClassTag_apply(x: Type): Constant
12081186

12091187
//
12101188
// SYMBOLS

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -289,18 +289,20 @@ trait Printers
289289
}
290290

291291
def visitConstant(x: Constant): Buffer = x match {
292-
case Constant.Unit() => this += "Constant.Unit()"
293-
case Constant.Null() => this += "Constant.Null()"
294-
case Constant.Boolean(value) => this += "Constant.Boolean(" += value += ")"
295-
case Constant.Byte(value) => this += "Constant.Byte(" += value += ")"
296-
case Constant.Short(value) => this += "Constant.Short(" += value += ")"
297-
case Constant.Char(value) => this += "Constant.Char(" += value += ")"
298-
case Constant.Int(value) => this += "Constant.Int(" += value.toString += ")"
299-
case Constant.Long(value) => this += "Constant.Long(" += value += ")"
300-
case Constant.Float(value) => this += "Constant.Float(" += value += ")"
301-
case Constant.Double(value) => this += "Constant.Double(" += value += ")"
302-
case Constant.String(value) => this += "Constant.String(\"" += value += "\")"
303-
case Constant.ClassTag(value) => this += "Constant.ClassTag(" += value += ")"
292+
case Constant(()) => this += "Constant(())"
293+
case Constant(null) => this += "Constant(null)"
294+
case Constant(value: Boolean) => this += "Constant(" += value += ")"
295+
case Constant(value: Byte) => this += "Constant(" += value += ": Byte)"
296+
case Constant(value: Short) => this += "Constant(" += value += ": Short)"
297+
case Constant(value: Char) => this += "Constant('" += value += "')"
298+
case Constant(value: Int) => this += "Constant(" += value.toString += ")"
299+
case Constant(value: Long) => this += "Constant(" += value += "L)"
300+
case Constant(value: Float) => this += "Constant(" += value += "f)"
301+
case Constant(value: Double) => this += "Constant(" += value += "d)"
302+
case Constant(value: String) => this += "Constant(\"" += value += "\")"
303+
case Constant.ClassTag(value) =>
304+
this += "Constant.ClassTag("
305+
visitType(value) += ")"
304306
}
305307

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

729731
case While(cond, body) =>
730732
(cond, body) match {
731-
case (Block(Block(Nil, body1) :: Nil, Block(Nil, cond1)), Literal(Constant.Unit())) =>
733+
case (Block(Block(Nil, body1) :: Nil, Block(Nil, cond1)), Literal(Constant(()))) =>
732734
this += highlightKeyword("do ")
733735
printTree(body1) += highlightKeyword(" while ")
734736
inParens(printTree(cond1))
@@ -990,7 +992,7 @@ trait Printers
990992
while (it.hasNext)
991993
extractFlatStats(it.next())
992994
extractFlatStats(expansion)
993-
case Literal(Constant.Unit()) => // ignore
995+
case Literal(Constant(())) => // ignore
994996
case stat => flatStats += stat
995997
}
996998
def extractFlatExpr(term: Term): Term = term match {
@@ -1374,17 +1376,17 @@ trait Printers
13741376
}
13751377

13761378
def printConstant(const: Constant): Buffer = const match {
1377-
case Constant.Unit() => this += highlightLiteral("()")
1378-
case Constant.Null() => this += highlightLiteral("null")
1379-
case Constant.Boolean(v) => this += highlightLiteral(v.toString)
1380-
case Constant.Byte(v) => this += highlightLiteral(v.toString)
1381-
case Constant.Short(v) => this += highlightLiteral(v.toString)
1382-
case Constant.Int(v) => this += highlightLiteral(v.toString)
1383-
case Constant.Long(v) => this += highlightLiteral(v.toString + "L")
1384-
case Constant.Float(v) => this += highlightLiteral(v.toString + "f")
1385-
case Constant.Double(v) => this += highlightLiteral(v.toString)
1386-
case Constant.Char(v) => this += highlightString('\'' + escapedChar(v) + '\'')
1387-
case Constant.String(v) => this += highlightString('"' + escapedString(v) + '"')
1379+
case Constant(()) => this += highlightLiteral("()")
1380+
case Constant(null) => this += highlightLiteral("null")
1381+
case Constant(v: Boolean) => this += highlightLiteral(v.toString)
1382+
case Constant(v: Byte) => this += highlightLiteral(v.toString)
1383+
case Constant(v: Short) => this += highlightLiteral(v.toString)
1384+
case Constant(v: Int) => this += highlightLiteral(v.toString)
1385+
case Constant(v: Long) => this += highlightLiteral(v.toString + "L")
1386+
case Constant(v: Float) => this += highlightLiteral(v.toString + "f")
1387+
case Constant(v: Double) => this += highlightLiteral(v.toString)
1388+
case Constant(v: Char) => this += highlightString('\'' + escapedChar(v) + '\'')
1389+
case Constant(v: String) => this += highlightString('"' + escapedString(v) + '"')
13881390
case Constant.ClassTag(v) =>
13891391
this += "classOf"
13901392
inSquare(printType(v))

semanticdb/src/dotty/semanticdb/TastyScalaFileInferrer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TastyScalaFileInferrer extends TastyConsumer {
2222
cdef.symbol.annots.foreach { annot =>
2323
annot match {
2424
case Apply(Select(New(t), _),
25-
List(Literal(Constant.String(path))))
25+
List(Literal(Constant(path: String))))
2626
if t.symbol.name == "SourceFile" =>
2727
// we found the path to a file. In this case, we do not need to
2828
// continue traversing the tree

tests/pos-macros/tasty-constant-type/Macro_1.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ object Macro {
99
def impl[A <: Int : Type, B <: Int : Type](a: Type[A], b: Type[B]) given (qctx: QuoteContext): Expr[AddInt[A, B]] = {
1010
import qctx.tasty._
1111

12-
val Type.ConstantType(Constant.Int(v1)) = a.unseal.tpe
13-
val Type.ConstantType(Constant.Int(v2)) = b.unseal.tpe
12+
val Type.ConstantType(Constant(v1: Int)) = a.unseal.tpe
13+
val Type.ConstantType(Constant(v2: Int)) = b.unseal.tpe
1414

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

1717
'{ null: AddInt[$a, $b] { type Out = $t } }
1818
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
DefDef("foo", Nil, Nil, TypeIdent("Int"), Some(Apply(Select(Literal(Constant.Int(1)), "+"), List(Literal(Constant.Int(2))))))
2-
ValDef("bar", TypeIdent("Int"), Some(Apply(Select(Literal(Constant.Int(2)), "+"), List(Literal(Constant.Int(3))))))
1+
DefDef("foo", Nil, Nil, TypeIdent("Int"), Some(Apply(Select(Literal(Constant(1)), "+"), List(Literal(Constant(2))))))
2+
ValDef("bar", TypeIdent("Int"), Some(Apply(Select(Literal(Constant(2)), "+"), List(Literal(Constant(3))))))
33
Pattern.Bind("x", Pattern.WildcardPattern())
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
DefDef("foo", Nil, Nil, TypeIdent("Int"), Some(Apply(Select(Literal(Constant.Int(1)), "+"), List(Literal(Constant.Int(2))))))
2-
ValDef("bar", TypeIdent("Int"), Some(Apply(Select(Literal(Constant.Int(2)), "+"), List(Literal(Constant.Int(3))))))
1+
DefDef("foo", Nil, Nil, TypeIdent("Int"), Some(Apply(Select(Literal(Constant(1)), "+"), List(Literal(Constant(2))))))
2+
ValDef("bar", TypeIdent("Int"), Some(Apply(Select(Literal(Constant(2)), "+"), List(Literal(Constant(3))))))

tests/run-macros/f-interpolation-1/FQuote_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ object FQuote {
3737
if isSCOpsConversion(conv) &&
3838
isStringContextApply(fun) &&
3939
values.forall(isStringConstant) =>
40-
values.collect { case Literal(Constant.String(value)) => value }
40+
values.collect { case Literal(Constant(value: String)) => value }
4141
case tree =>
4242
QuoteError(s"String literal expected, but ${tree.showExtractors} found")
4343
}

tests/run-macros/i5119.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
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")
2-
Typed(Repeated(List(Literal(Constant.Int(1))), Inferred()), Inferred())
1+
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")
2+
Typed(Repeated(List(Literal(Constant(1))), Inferred()), Inferred())

tests/run-macros/i5119b.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Apply(Ident("foo"), List(Literal(Constant.Int(1))))
2-
Apply(Ident("foo"), List(Literal(Constant.Int(2))))
3-
Apply(Ident("foo"), List(Literal(Constant.Int(4))))
4-
Apply(Ident("foo"), List(Literal(Constant.Int(3))))
1+
Apply(Ident("foo"), List(Literal(Constant(1))))
2+
Apply(Ident("foo"), List(Literal(Constant(2))))
3+
Apply(Ident("foo"), List(Literal(Constant(4))))
4+
Apply(Ident("foo"), List(Literal(Constant(3))))

0 commit comments

Comments
 (0)