Skip to content

Commit 553330b

Browse files
committed
Homogenize Reflect Type constructors
* Rename Reflect.typeOf to Reflect.Type.of * Rename Reflect.Type.apply to Reflect.Type.ofClass * Make Reflect.Type.of AnyKinded * Improve some other type uses in community build Before we did not do it due to the distinction between TypeOrBounds and Type. Now that they are all Type we can support it.
1 parent 101e620 commit 553330b

File tree

22 files changed

+80
-84
lines changed

22 files changed

+80
-84
lines changed

compiler/src/dotty/tools/dotc/quoted/reflect/ReflectionCompilerInterface.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte
11281128
case _ => None
11291129
}
11301130

1131-
def Type_apply(clazz: Class[?])(using Context): Type =
1131+
def Type_ofClass(clazz: Class[?])(using Context): Type =
11321132
if (clazz.isPrimitive)
11331133
if (clazz == classOf[Boolean]) defn.BooleanType
11341134
else if (clazz == classOf[Byte]) defn.ByteType
@@ -1140,10 +1140,10 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte
11401140
else if (clazz == classOf[Double]) defn.DoubleType
11411141
else defn.UnitType
11421142
else if (clazz.isArray)
1143-
defn.ArrayType.appliedTo(Type_apply(clazz.getComponentType))
1143+
defn.ArrayType.appliedTo(Type_ofClass(clazz.getComponentType))
11441144
else if (clazz.isMemberClass) {
11451145
val name = clazz.getSimpleName.toTypeName
1146-
val enclosing = Type_apply(clazz.getEnclosingClass)
1146+
val enclosing = Type_ofClass(clazz.getEnclosingClass)
11471147
if (enclosing.member(name).exists) enclosing.select(name)
11481148
else
11491149
enclosing.classSymbol.companionModule.termRef.select(name)

library/src-bootstrapped/dotty/internal/StringContextMacro.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,9 @@ object StringContextMacro {
586586
*/
587587
def checkTypeWithArgs(argument : (Type, Int), conversionChar : Char, partIndex : Int, flags : List[(Char, Int)]) = {
588588
val booleans = List(defn.BooleanType, defn.NullType)
589-
val dates = List(defn.LongType, typeOf[java.util.Calendar], typeOf[java.util.Date])
590-
val floatingPoints = List(defn.DoubleType, defn.FloatType, typeOf[java.math.BigDecimal])
591-
val integral = List(defn.IntType, defn.LongType, defn.ShortType, defn.ByteType, typeOf[java.math.BigInteger])
589+
val dates = List(defn.LongType, Type.ofClass[java.util.Calendar], Type.ofClass[java.util.Date])
590+
val floatingPoints = List(defn.DoubleType, defn.FloatType, Type.ofClass[java.math.BigDecimal])
591+
val integral = List(defn.IntType, defn.LongType, defn.ShortType, defn.ByteType, Type.ofClass[java.math.BigInteger])
592592
val character = List(defn.CharType, defn.ByteType, defn.ShortType, defn.IntType)
593593

594594
val (argType, argIndex) = argument
@@ -597,9 +597,9 @@ object StringContextMacro {
597597
case 'd' | 'o' | 'x' | 'X' => {
598598
checkSubtype(argType, "Int", argIndex, integral : _*)
599599
if (conversionChar != 'd') {
600-
val notAllowedFlagOnCondition = List(('+', !(argType <:< typeOf[java.math.BigInteger]), "only use '+' for BigInt conversions to o, x, X"),
601-
(' ', !(argType <:< typeOf[java.math.BigInteger]), "only use ' ' for BigInt conversions to o, x, X"),
602-
('(', !(argType <:< typeOf[java.math.BigInteger]), "only use '(' for BigInt conversions to o, x, X"),
600+
val notAllowedFlagOnCondition = List(('+', !(argType <:< Type.ofClass[java.math.BigInteger]), "only use '+' for BigInt conversions to o, x, X"),
601+
(' ', !(argType <:< Type.ofClass[java.math.BigInteger]), "only use ' ' for BigInt conversions to o, x, X"),
602+
('(', !(argType <:< Type.ofClass[java.math.BigInteger]), "only use '(' for BigInt conversions to o, x, X"),
603603
(',', true, "',' only allowed for d conversion of integral types"))
604604
checkFlags(partIndex, flags, notAllowedFlagOnCondition : _*)
605605
}
@@ -608,7 +608,7 @@ object StringContextMacro {
608608
case 't' | 'T' => checkSubtype(argType, "Date", argIndex, dates : _*)
609609
case 'b' | 'B' => checkSubtype(argType, "Boolean", argIndex, booleans : _*)
610610
case 'h' | 'H' | 'S' | 's' =>
611-
if (!(argType <:< typeOf[java.util.Formattable]))
611+
if (!(argType <:< Type.ofClass[java.util.Formattable]))
612612
for {flag <- flags ; if (flag._1 == '#')}
613613
reporter.argError("type mismatch;\n found : " + argType.widen.show.stripPrefix("scala.Predef.").stripPrefix("java.lang.").stripPrefix("scala.") + "\n required: java.util.Formattable", argIndex)
614614
case 'n' | '%' =>

library/src-bootstrapped/scala/quoted/Liftable.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ object Liftable {
6060
given ClassLiftable[T] as Liftable[Class[T]] = new Liftable[Class[T]] {
6161
def toExpr(x: Class[T]) = qctx ?=> {
6262
import qctx.tasty._
63-
Ref(defn.Predef_classOf).appliedToType(Type(x)).seal.asInstanceOf[Expr[Class[T]]]
63+
Ref(defn.Predef_classOf).appliedToType(Type.ofClass(using ClassTag(x))).seal.asInstanceOf[Expr[Class[T]]]
6464
}
6565
}
6666

library/src/scala/internal/tasty/CompilerInterface.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ trait CompilerInterface extends scala.tasty.reflect.Types {
512512

513513
def Type_TypeTest(using ctx: Context): TypeTest[Type, Type]
514514

515-
def Type_apply(clazz: Class[_])(using ctx: Context): Type
515+
def Type_ofClass(clazz: Class[_])(using ctx: Context): Type
516516

517517
/** Is `self` type the same as `that` type?
518518
* This is the case iff `Type_isSubType(self, that)` and `Type_isSubType(that, self)`.

library/src/scala/tasty/Reflection.scala

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,20 +1336,20 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface =>
13361336
// TYPES //
13371337
///////////////
13381338

1339-
/** Returns the type (Type) of T */
1340-
def typeOf[T](using qtype: scala.quoted.Type[T], ctx: Context): Type =
1341-
qtype.asInstanceOf[scala.internal.quoted.Type[T]].typeTree.asInstanceOf[TypeTree].tpe
1342-
1343-
13441339
// ----- Types ----------------------------------------------------
13451340

13461341
given (using ctx: Context) as TypeTest[Type, Type] = reflectSelf.Type_TypeTest
13471342
given TypeOps as Type.type = Type
13481343

13491344
object Type:
13501345

1351-
def apply(clazz: Class[_])(using ctx: Context): Type =
1352-
reflectSelf.Type_apply(clazz)
1346+
/** Returns the type or kind (Type) of T */
1347+
def of[T <: AnyKind](using qtype: scala.quoted.Type[T], ctx: Context): Type =
1348+
qtype.asInstanceOf[scala.internal.quoted.Type[TypeTree]].typeTree.tpe
1349+
1350+
/** Returns the type or kind of the class of the type T */
1351+
def ofClass[T](using ct: scala.reflect.ClassTag[T]): Type =
1352+
reflectSelf.Type_ofClass(ct.runtimeClass)
13531353

13541354
extension (self: Type):
13551355

@@ -1626,13 +1626,9 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface =>
16261626
end MatchType
16271627

16281628

1629-
/**
1630-
* An accessor for `scala.internal.MatchCase[_,_]`, the representation of a `MatchType` case.
1631-
*/
1632-
def MatchCaseType(using ctx: Context): Type = {
1633-
import scala.internal.MatchCase
1634-
Type(classOf[MatchCase[_,_]])
1635-
}
1629+
/** An accessor for `scala.internal.MatchCase[_,_]`, the representation of a `MatchType` case. */
1630+
def MatchCaseType(using ctx: Context): Type =
1631+
Type.ofClass[scala.internal.MatchCase[_,_]]
16361632

16371633
given (using ctx: Context) as TypeTest[Type, ByNameType] = reflectSelf.ByNameType_TypeTest
16381634
given ByNameTypeOps as ByNameType.type = ByNameType

library/src/scala/tasty/reflect/Types.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ trait Types {
292292
/** Pattern representing `X | Y | ...` alternatives. */
293293
type Alternatives <: Tree
294294

295-
/** A type */
295+
/** A type, kind, type bounds or NoPrefix */
296296
type Type
297297

298298
/** A singleton type representing a known constant value */

tests/neg-macros/i7919.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object Test {
55
import qctx.tasty._
66
given typeT as quoted.Type[T] // error
77
val tTypeTree = typeT.unseal
8-
val tt = typeOf[T]
8+
val tt = Type.of[T]
99
'{ "in staged" }
1010
}
1111

tests/neg-macros/i8871.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import scala.quoted._
22
object Macro {
33
def impl[A : Type](using qctx: QuoteContext): Unit = {
44
import qctx.tasty._
5-
val tpe = typeOf[A].seal.asInstanceOf[quoted.Type[_ <: AnyRef]]
5+
val tpe = Type.of[A].seal.asInstanceOf[quoted.Type[_ <: AnyRef]]
66
'{ (a: ${tpe}) => ???} // error
77
}
88
}

tests/neg-macros/i8871b.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import scala.quoted._
22
object Macro {
33
def impl[A : Type](using qctx: QuoteContext): Unit = {
44
import qctx.tasty._
5-
val tpe/*: quoted.Type[? <: AnyKind]*/ = typeOf[A].seal
5+
val tpe/*: quoted.Type[? <: AnyKind]*/ = Type.of[A].seal
66
'{ f[$tpe] } // error
77
}
88
def f[T <: AnyKind]: Unit = ()

tests/pos-macros/i8879/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Test {
99
import qctx.tasty._
1010
import util._
1111

12-
val foo = typeOf[Foo[String]]
12+
val foo = Type.of[Foo[String]]
1313
val symbol = foo.typeSymbol.field("a")
1414
val a = foo.select(symbol)
1515
assert(a <:< defn.StringType)

tests/pos-macros/i9240/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ inline def diveInto[T]: String = ${ diveIntoImpl[T]() }
55

66
def diveIntoImpl[T]()(implicit qctx: QuoteContext, ttype: scala.quoted.Type[T]): Expr[String] =
77
import qctx.tasty._
8-
Expr( unwindType(qctx.tasty)(typeOf[T]) )
8+
Expr( unwindType(qctx.tasty)(Type.of[T]) )
99

1010
def unwindType(reflect: Reflection)(aType: reflect.Type): String =
1111
import reflect._

tests/pos-macros/i9518/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def shiftTerm(using QuoteContext): Expr[Unit] = {
1212
val tp2 = '[([X] =>> CB[X])[Int]].unseal.tpe
1313
val ta = '[[X] =>> CB[X]]
1414
val tp3 = '[ta.T[Int]].unseal.tpe
15-
val tp4 = '[CB].unseal.tpe.appliedTo(typeOf[Int])
15+
val tp4 = '[CB].unseal.tpe.appliedTo(Type.of[Int])
1616
assert(nTree.tpe <:< tp1)
1717
assert(nTree.tpe <:< tp2)
1818
assert(nTree.tpe <:< tp3)

tests/run-macros/i5941/macro_1.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ object Iso {
8888
import qctx.tasty._
8989
import util._
9090

91-
val tpS = typeOf[S]
92-
val tpA = typeOf[A]
91+
val tpS = Type.of[S]
92+
val tpA = Type.of[A]
9393

9494
// 1. S must be a case class
9595
// 2. A must be a tuple
@@ -127,7 +127,7 @@ object Iso {
127127
import qctx.tasty._
128128
import util._
129129

130-
val tpS = typeOf[S]
130+
val tpS = Type.of[S]
131131

132132
if (tpS.isSingleton) {
133133
val ident = Ident(tpS.asInstanceOf[TermRef]).seal.cast[S]

tests/run-macros/i6518/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ object Macros {
66

77
private def testImpl(using qctx: QuoteContext) : Expr[String] = {
88
import qctx.tasty._
9-
val classSym = typeOf[Function1[_, _]].classSymbol.get
9+
val classSym = Type.ofClass[Function1[_, _]].classSymbol.get
1010
classSym.classMethod("apply")
1111
classSym.classMethods
1212
classSym.method("apply")

tests/run-macros/tasty-construct-types/Macro_1.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@ object Macros {
1616

1717
val x1T = ConstantType(Constant(1))
1818
val x2T = OrType(ConstantType(Constant(1)), ConstantType(Constant(2)))
19-
val x3T = AndType(ConstantType(Constant(3)), typeOf[Any])
19+
val x3T = AndType(ConstantType(Constant(3)), Type.of[Any])
2020
val x4T =
2121
TypeLambda(
2222
List("A","B"),
23-
_ => List(TypeBounds(typeOf[Nothing], typeOf[Any]), TypeBounds(typeOf[Nothing], typeOf[Any])),
23+
_ => List(TypeBounds(Type.of[Nothing], Type.of[Any]), TypeBounds(Type.of[Nothing], Type.of[Any])),
2424
(tl : TypeLambda) => tl.param(1))
2525
val x5T =
2626
Refinement(
27-
typeOf[RefineMe],
27+
Type.of[RefineMe],
2828
"T",
29-
TypeBounds(typeOf[Int], typeOf[Int]))
30-
val x6T = Type(classOf[List[_]]).appliedTo(List(typeOf[Int]))
29+
TypeBounds(Type.of[Int], Type.of[Int]))
30+
val x6T = Type.ofClass[List[_]].appliedTo(List(Type.of[Int]))
3131
val x7T = AnnotatedType(ConstantType(Constant(7)), '{ new TestAnnotation }.unseal)
3232
val x8T =
3333
MatchType(
34-
typeOf[Int],
35-
typeOf[List[8]],
34+
Type.of[Int],
35+
Type.of[List[8]],
3636
List(
3737
TypeLambda(
3838
List("t"),
39-
_ => List(TypeBounds(typeOf[Nothing], typeOf[Any])),
40-
tl => MatchCaseType.appliedTo(List(Type(classOf[List[_]]).appliedTo(tl.param(0)), tl.param(0)))))
39+
_ => List(TypeBounds(Type.of[Nothing], Type.of[Any])),
40+
tl => MatchCaseType.appliedTo(List(Type.ofClass[List[_]].appliedTo(tl.param(0)), tl.param(0)))))
4141
)
4242

4343
assert(x1T =:= '[1].unseal.tpe)

tests/run-macros/tasty-create-method-symbol/Macro_1.scala

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ object Macros {
1212
Symbol.currentOwner,
1313
"sym1",
1414
MethodType(List("a","b"))(
15-
_ => List(typeOf[Int], typeOf[Int]),
16-
_ => typeOf[Int]))
15+
_ => List(Type.of[Int], Type.of[Int]),
16+
_ => Type.of[Int]))
1717
assert(sym1.isDefDef)
1818
assert(sym1.name == "sym1")
1919
val sym1Statements : List[Statement] = List(
@@ -29,7 +29,7 @@ object Macros {
2929
val sym2 : Symbol = Symbol.newMethod(
3030
Symbol.currentOwner,
3131
"sym2",
32-
ByNameType(typeOf[Int]))
32+
ByNameType(Type.of[Int]))
3333
assert(sym2.isDefDef)
3434
assert(sym2.name == "sym2")
3535
val sym2Statements : List[Statement] = List(
@@ -46,7 +46,7 @@ object Macros {
4646
Symbol.currentOwner,
4747
"sym3",
4848
MethodType(List("a"))(
49-
_ => List(typeOf[Int]),
49+
_ => List(Type.of[Int]),
5050
mt => MethodType(List("b"))(
5151
_ => List(mt.param(0)),
5252
_ => mt.param(0))))
@@ -66,8 +66,8 @@ object Macros {
6666
Symbol.currentOwner,
6767
"sym4",
6868
MethodType(List("x"))(
69-
_ => List(typeOf[Int]),
70-
_ => typeOf[Int]))
69+
_ => List(Type.of[Int]),
70+
_ => Type.of[Int]))
7171
assert(sym4.isDefDef)
7272
assert(sym4.name == "sym4")
7373
val sym4Statements : List[Statement] = List(
@@ -88,8 +88,8 @@ object Macros {
8888
Symbol.currentOwner,
8989
"sym5",
9090
MethodType(List("x"))(
91-
_ => List(typeOf[Int]),
92-
_ => typeOf[Int=>Int]))
91+
_ => List(Type.of[Int]),
92+
_ => Type.of[Int=>Int]))
9393
assert(sym5.isDefDef)
9494
assert(sym5.name == "sym5")
9595
val sym5Statements : List[Statement] = List(
@@ -101,8 +101,8 @@ object Macros {
101101
sym5,
102102
"sym51",
103103
MethodType(List("x"))(
104-
_ => List(typeOf[Int]),
105-
_ => typeOf[Int]))
104+
_ => List(Type.of[Int]),
105+
_ => Type.of[Int]))
106106
Block(
107107
List(
108108
DefDef(sym51, {
@@ -122,14 +122,14 @@ object Macros {
122122
Symbol.currentOwner,
123123
"sym6_1",
124124
MethodType(List("x"))(
125-
_ => List(typeOf[Int]),
126-
_ => typeOf[Int]))
125+
_ => List(Type.of[Int]),
126+
_ => Type.of[Int]))
127127
val sym6_2 : Symbol = Symbol.newMethod(
128128
Symbol.currentOwner,
129129
"sym6_2",
130130
MethodType(List("x"))(
131-
_ => List(typeOf[Int]),
132-
_ => typeOf[Int]))
131+
_ => List(Type.of[Int]),
132+
_ => Type.of[Int]))
133133
assert(sym6_1.isDefDef)
134134
assert(sym6_2.isDefDef)
135135
assert(sym6_1.name == "sym6_1")
@@ -169,7 +169,7 @@ object Macros {
169169
Symbol.currentOwner,
170170
"sym7",
171171
PolyType(List("T"))(
172-
tp => List(TypeBounds(typeOf[Nothing], typeOf[Any])),
172+
tp => List(TypeBounds(Type.of[Nothing], Type.of[Any])),
173173
tp => MethodType(List("t"))(
174174
_ => List(tp.param(0)),
175175
_ => tp.param(0))))
@@ -182,7 +182,7 @@ object Macros {
182182
Some(Typed(x, Inferred(t)))
183183
}
184184
}),
185-
'{ assert(${ Apply(TypeApply(Ref(sym7), List(Inferred(typeOf[Int]))), List(Literal(Constant(7)))).seal.asInstanceOf[Expr[Int]] } == 7) }.unseal)
185+
'{ assert(${ Apply(TypeApply(Ref(sym7), List(Inferred(Type.of[Int]))), List(Literal(Constant(7)))).seal.asInstanceOf[Expr[Int]] } == 7) }.unseal)
186186

187187
Block(
188188
sym1Statements ++

tests/run-macros/tasty-simplified/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ object Macros {
1818
loop(tp, Nil).reverse
1919
}
2020

21-
val tps = unpackTuple(typeOf[T])
21+
val tps = unpackTuple(Type.of[T])
2222
Varargs(tps.map(x => Expr(x.show)))
2323
}
2424
}

0 commit comments

Comments
 (0)