Skip to content

Commit 8913374

Browse files
Merge pull request #9796 from dotty-staging/homogenize-reflect-type-api
Homogenize Reflect Type constructors
2 parents 89af58f + 3560062 commit 8913374

File tree

21 files changed

+72
-73
lines changed

21 files changed

+72
-73
lines changed

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.of[java.util.Calendar], Type.of[java.util.Date])
590+
val floatingPoints = List(defn.DoubleType, defn.FloatType, Type.of[java.math.BigDecimal])
591+
val integral = List(defn.IntType, defn.LongType, defn.ShortType, defn.ByteType, Type.of[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.of[java.math.BigInteger]), "only use '+' for BigInt conversions to o, x, X"),
601+
(' ', !(argType <:< Type.of[java.math.BigInteger]), "only use ' ' for BigInt conversions to o, x, X"),
602+
('(', !(argType <:< Type.of[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.of[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/util/ExprMap.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ trait ExprMap {
6363
case Typed(expr, tpt) =>
6464
val tp = tpt.tpe match
6565
case AppliedType(TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "<repeated>"), List(tp0: Type)) =>
66-
Type(classOf[Seq[_]]).appliedTo(tp0)
66+
Type.of[Seq].appliedTo(tp0)
6767
case tp => tp
6868
Typed.copy(tree)(transformTerm(expr, tp), transformTypeTree(tpt))
6969
case tree: NamedArg =>

library/src/scala/tasty/Reflection.scala

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,17 +1343,16 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface =>
13431343
// TYPES //
13441344
///////////////
13451345

1346-
/** Returns the type (Type) of T */
1347-
def typeOf[T](using qtype: scala.quoted.Type[T], ctx: Context): Type =
1348-
qtype.asInstanceOf[scala.internal.quoted.Type[T]].typeTree.asInstanceOf[TypeTree].tpe
1349-
1350-
13511346
// ----- Types ----------------------------------------------------
13521347

13531348
given (using ctx: Context) as TypeTest[Type, Type] = reflectSelf.Type_TypeTest
13541349

13551350
object Type:
13561351

1352+
/** Returns the type or kind (Type) of T */
1353+
def of[T <: AnyKind](using qtype: scala.quoted.Type[T], ctx: Context): Type =
1354+
qtype.asInstanceOf[scala.internal.quoted.Type[TypeTree]].typeTree.tpe
1355+
13571356
def apply(clazz: Class[_])(using ctx: Context): Type =
13581357
reflectSelf.Type_apply(clazz)
13591358
end Type
@@ -1643,7 +1642,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface =>
16431642
end extension
16441643
end MatchTypeOps
16451644

1646-
1645+
// TODO remove this definition from here
16471646
/**
16481647
* An accessor for `scala.internal.MatchCase[_,_]`, the representation of a `MatchType` case.
16491648
*/

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, type constructors, 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.of[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.of[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.of[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
}

tests/run-macros/tasty-typeof/Macro_1.scala

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@ object Macros {
77
private def testTypeOfImpl(using qctx: QuoteContext) : Expr[Unit] = {
88
import qctx.tasty._
99
'{
10-
assert(${Expr(typeOf[Unit] =:= defn.UnitType)}, "Unit")
11-
assert(${Expr(typeOf[Byte] =:= defn.ByteType)}, "Byte")
12-
assert(${Expr(typeOf[Short] =:= defn.ShortType)}, "Short")
13-
assert(${Expr(typeOf[Int] =:= defn.IntType)}, "Int")
14-
assert(${Expr(typeOf[Long] =:= defn.LongType)}, "Long")
15-
assert(${Expr(typeOf[Float] =:= defn.FloatType)}, "Float")
16-
assert(${Expr(typeOf[Double] =:= defn.DoubleType)}, "Double")
17-
assert(${Expr(typeOf[Char] =:= defn.CharType)}, "Char")
18-
assert(${Expr(typeOf[String] =:= defn.StringType)}, "String")
10+
assert(${Expr(Type.of[Unit] =:= defn.UnitType)}, "Unit")
11+
assert(${Expr(Type.of[Byte] =:= defn.ByteType)}, "Byte")
12+
assert(${Expr(Type.of[Short] =:= defn.ShortType)}, "Short")
13+
assert(${Expr(Type.of[Int] =:= defn.IntType)}, "Int")
14+
assert(${Expr(Type.of[Long] =:= defn.LongType)}, "Long")
15+
assert(${Expr(Type.of[Float] =:= defn.FloatType)}, "Float")
16+
assert(${Expr(Type.of[Double] =:= defn.DoubleType)}, "Double")
17+
assert(${Expr(Type.of[Char] =:= defn.CharType)}, "Char")
18+
assert(${Expr(Type.of[String] =:= defn.StringType)}, "String")
1919

20-
assert(${Expr(typeOf[Any] =:= defn.AnyType)}, "Any")
21-
assert(${Expr(typeOf[AnyRef] =:= defn.AnyRefType)}, "AnyRef")
22-
assert(${Expr(typeOf[AnyVal] =:= defn.AnyValType)}, "AnyVal")
23-
assert(${Expr(typeOf[Object] =:= defn.ObjectType)}, "Object")
24-
assert(${Expr(typeOf[Nothing] =:= defn.NothingType)}, "Nothing")
20+
assert(${Expr(Type.of[Any] =:= defn.AnyType)}, "Any")
21+
assert(${Expr(Type.of[AnyRef] =:= defn.AnyRefType)}, "AnyRef")
22+
assert(${Expr(Type.of[AnyVal] =:= defn.AnyValType)}, "AnyVal")
23+
assert(${Expr(Type.of[Object] =:= defn.ObjectType)}, "Object")
24+
assert(${Expr(Type.of[Nothing] =:= defn.NothingType)}, "Nothing")
2525

26-
println(${Expr(typeOf[List[Int]].show)})
27-
println(${Expr(typeOf[Macros].show)})
28-
println(${Expr(typeOf[Macros.type].show)})
26+
println(${Expr(Type.of[List[Int]].show)})
27+
println(${Expr(Type.of[Macros].show)})
28+
println(${Expr(Type.of[Macros.type].show)})
2929
}
3030
}
3131

0 commit comments

Comments
 (0)