Skip to content

Homogenize Reflect Type constructors #9796

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 2 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 7 additions & 7 deletions library/src-bootstrapped/dotty/internal/StringContextMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,9 @@ object StringContextMacro {
*/
def checkTypeWithArgs(argument : (Type, Int), conversionChar : Char, partIndex : Int, flags : List[(Char, Int)]) = {
val booleans = List(defn.BooleanType, defn.NullType)
val dates = List(defn.LongType, typeOf[java.util.Calendar], typeOf[java.util.Date])
val floatingPoints = List(defn.DoubleType, defn.FloatType, typeOf[java.math.BigDecimal])
val integral = List(defn.IntType, defn.LongType, defn.ShortType, defn.ByteType, typeOf[java.math.BigInteger])
val dates = List(defn.LongType, Type.of[java.util.Calendar], Type.of[java.util.Date])
val floatingPoints = List(defn.DoubleType, defn.FloatType, Type.of[java.math.BigDecimal])
val integral = List(defn.IntType, defn.LongType, defn.ShortType, defn.ByteType, Type.of[java.math.BigInteger])
val character = List(defn.CharType, defn.ByteType, defn.ShortType, defn.IntType)

val (argType, argIndex) = argument
Expand All @@ -597,9 +597,9 @@ object StringContextMacro {
case 'd' | 'o' | 'x' | 'X' => {
checkSubtype(argType, "Int", argIndex, integral : _*)
if (conversionChar != 'd') {
val notAllowedFlagOnCondition = List(('+', !(argType <:< typeOf[java.math.BigInteger]), "only use '+' for BigInt conversions to o, x, X"),
(' ', !(argType <:< typeOf[java.math.BigInteger]), "only use ' ' for BigInt conversions to o, x, X"),
('(', !(argType <:< typeOf[java.math.BigInteger]), "only use '(' for BigInt conversions to o, x, X"),
val notAllowedFlagOnCondition = List(('+', !(argType <:< Type.of[java.math.BigInteger]), "only use '+' for BigInt conversions to o, x, X"),
(' ', !(argType <:< Type.of[java.math.BigInteger]), "only use ' ' for BigInt conversions to o, x, X"),
('(', !(argType <:< Type.of[java.math.BigInteger]), "only use '(' for BigInt conversions to o, x, X"),
(',', true, "',' only allowed for d conversion of integral types"))
checkFlags(partIndex, flags, notAllowedFlagOnCondition : _*)
}
Expand All @@ -608,7 +608,7 @@ object StringContextMacro {
case 't' | 'T' => checkSubtype(argType, "Date", argIndex, dates : _*)
case 'b' | 'B' => checkSubtype(argType, "Boolean", argIndex, booleans : _*)
case 'h' | 'H' | 'S' | 's' =>
if (!(argType <:< typeOf[java.util.Formattable]))
if (!(argType <:< Type.of[java.util.Formattable]))
for {flag <- flags ; if (flag._1 == '#')}
reporter.argError("type mismatch;\n found : " + argType.widen.show.stripPrefix("scala.Predef.").stripPrefix("java.lang.").stripPrefix("scala.") + "\n required: java.util.Formattable", argIndex)
case 'n' | '%' =>
Expand Down
2 changes: 1 addition & 1 deletion library/src-bootstrapped/scala/quoted/util/ExprMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ trait ExprMap {
case Typed(expr, tpt) =>
val tp = tpt.tpe match
case AppliedType(TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "<repeated>"), List(tp0: Type)) =>
Type(classOf[Seq[_]]).appliedTo(tp0)
Type.of[Seq].appliedTo(tp0)
case tp => tp
Typed.copy(tree)(transformTerm(expr, tp), transformTypeTree(tpt))
case tree: NamedArg =>
Expand Down
11 changes: 5 additions & 6 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1343,17 +1343,16 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface =>
// TYPES //
///////////////

/** Returns the type (Type) of T */
def typeOf[T](using qtype: scala.quoted.Type[T], ctx: Context): Type =
qtype.asInstanceOf[scala.internal.quoted.Type[T]].typeTree.asInstanceOf[TypeTree].tpe


// ----- Types ----------------------------------------------------

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

object Type:

/** Returns the type or kind (Type) of T */
def of[T <: AnyKind](using qtype: scala.quoted.Type[T], ctx: Context): Type =
qtype.asInstanceOf[scala.internal.quoted.Type[TypeTree]].typeTree.tpe

def apply(clazz: Class[_])(using ctx: Context): Type =
reflectSelf.Type_apply(clazz)
end Type
Expand Down Expand Up @@ -1643,7 +1642,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface =>
end extension
end MatchTypeOps


// TODO remove this definition from here
/**
* An accessor for `scala.internal.MatchCase[_,_]`, the representation of a `MatchType` case.
*/
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/tasty/reflect/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ trait Types {
/** Pattern representing `X | Y | ...` alternatives. */
type Alternatives <: Tree

/** A type */
/** A type, kind, type bounds or NoPrefix */
type Type

/** A singleton type representing a known constant value */
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/i7919.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object Test {
import qctx.tasty._
given typeT as quoted.Type[T] // error
val tTypeTree = typeT.unseal
val tt = typeOf[T]
val tt = Type.of[T]
'{ "in staged" }
}

Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/i8871.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import scala.quoted._
object Macro {
def impl[A : Type](using qctx: QuoteContext): Unit = {
import qctx.tasty._
val tpe = typeOf[A].seal.asInstanceOf[quoted.Type[_ <: AnyRef]]
val tpe = Type.of[A].seal.asInstanceOf[quoted.Type[_ <: AnyRef]]
'{ (a: ${tpe}) => ???} // error
}
}
2 changes: 1 addition & 1 deletion tests/neg-macros/i8871b.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import scala.quoted._
object Macro {
def impl[A : Type](using qctx: QuoteContext): Unit = {
import qctx.tasty._
val tpe/*: quoted.Type[? <: AnyKind]*/ = typeOf[A].seal
val tpe/*: quoted.Type[? <: AnyKind]*/ = Type.of[A].seal
'{ f[$tpe] } // error
}
def f[T <: AnyKind]: Unit = ()
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i8879/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object Test {
import qctx.tasty._
import util._

val foo = typeOf[Foo[String]]
val foo = Type.of[Foo[String]]
val symbol = foo.typeSymbol.field("a")
val a = foo.select(symbol)
assert(a <:< defn.StringType)
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i9240/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ inline def diveInto[T]: String = ${ diveIntoImpl[T]() }

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

def unwindType(reflect: Reflection)(aType: reflect.Type): String =
import reflect._
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i9518/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def shiftTerm(using QuoteContext): Expr[Unit] = {
val tp2 = '[([X] =>> CB[X])[Int]].unseal.tpe
val ta = '[[X] =>> CB[X]]
val tp3 = '[ta.T[Int]].unseal.tpe
val tp4 = '[CB].unseal.tpe.appliedTo(typeOf[Int])
val tp4 = '[CB].unseal.tpe.appliedTo(Type.of[Int])
assert(nTree.tpe <:< tp1)
assert(nTree.tpe <:< tp2)
assert(nTree.tpe <:< tp3)
Expand Down
6 changes: 3 additions & 3 deletions tests/run-macros/i5941/macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ object Iso {
import qctx.tasty._
import util._

val tpS = typeOf[S]
val tpA = typeOf[A]
val tpS = Type.of[S]
val tpA = Type.of[A]

// 1. S must be a case class
// 2. A must be a tuple
Expand Down Expand Up @@ -127,7 +127,7 @@ object Iso {
import qctx.tasty._
import util._

val tpS = typeOf[S]
val tpS = Type.of[S]

if (tpS.isSingleton) {
val ident = Ident(tpS.asInstanceOf[TermRef]).seal.cast[S]
Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/i6518/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Macros {

private def testImpl(using qctx: QuoteContext) : Expr[String] = {
import qctx.tasty._
val classSym = typeOf[Function1[_, _]].classSymbol.get
val classSym = Type.of[Function1].classSymbol.get
classSym.classMethod("apply")
classSym.classMethods
classSym.method("apply")
Expand Down
18 changes: 9 additions & 9 deletions tests/run-macros/tasty-construct-types/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@ object Macros {

val x1T = ConstantType(Constant(1))
val x2T = OrType(ConstantType(Constant(1)), ConstantType(Constant(2)))
val x3T = AndType(ConstantType(Constant(3)), typeOf[Any])
val x3T = AndType(ConstantType(Constant(3)), Type.of[Any])
val x4T =
TypeLambda(
List("A","B"),
_ => List(TypeBounds(typeOf[Nothing], typeOf[Any]), TypeBounds(typeOf[Nothing], typeOf[Any])),
_ => List(TypeBounds(Type.of[Nothing], Type.of[Any]), TypeBounds(Type.of[Nothing], Type.of[Any])),
(tl : TypeLambda) => tl.param(1))
val x5T =
Refinement(
typeOf[RefineMe],
Type.of[RefineMe],
"T",
TypeBounds(typeOf[Int], typeOf[Int]))
val x6T = Type(classOf[List[_]]).appliedTo(List(typeOf[Int]))
TypeBounds(Type.of[Int], Type.of[Int]))
val x6T = Type.of[List].appliedTo(List(Type.of[Int]))
val x7T = AnnotatedType(ConstantType(Constant(7)), '{ new TestAnnotation }.unseal)
val x8T =
MatchType(
typeOf[Int],
typeOf[List[8]],
Type.of[Int],
Type.of[List[8]],
List(
TypeLambda(
List("t"),
_ => List(TypeBounds(typeOf[Nothing], typeOf[Any])),
tl => MatchCaseType.appliedTo(List(Type(classOf[List[_]]).appliedTo(tl.param(0)), tl.param(0)))))
_ => List(TypeBounds(Type.of[Nothing], Type.of[Any])),
tl => MatchCaseType.appliedTo(List(Type.of[List].appliedTo(tl.param(0)), tl.param(0)))))
)

assert(x1T =:= '[1].unseal.tpe)
Expand Down
32 changes: 16 additions & 16 deletions tests/run-macros/tasty-create-method-symbol/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ object Macros {
Symbol.currentOwner,
"sym1",
MethodType(List("a","b"))(
_ => List(typeOf[Int], typeOf[Int]),
_ => typeOf[Int]))
_ => List(Type.of[Int], Type.of[Int]),
_ => Type.of[Int]))
assert(sym1.isDefDef)
assert(sym1.name == "sym1")
val sym1Statements : List[Statement] = List(
Expand All @@ -29,7 +29,7 @@ object Macros {
val sym2 : Symbol = Symbol.newMethod(
Symbol.currentOwner,
"sym2",
ByNameType(typeOf[Int]))
ByNameType(Type.of[Int]))
assert(sym2.isDefDef)
assert(sym2.name == "sym2")
val sym2Statements : List[Statement] = List(
Expand All @@ -46,7 +46,7 @@ object Macros {
Symbol.currentOwner,
"sym3",
MethodType(List("a"))(
_ => List(typeOf[Int]),
_ => List(Type.of[Int]),
mt => MethodType(List("b"))(
_ => List(mt.param(0)),
_ => mt.param(0))))
Expand All @@ -66,8 +66,8 @@ object Macros {
Symbol.currentOwner,
"sym4",
MethodType(List("x"))(
_ => List(typeOf[Int]),
_ => typeOf[Int]))
_ => List(Type.of[Int]),
_ => Type.of[Int]))
assert(sym4.isDefDef)
assert(sym4.name == "sym4")
val sym4Statements : List[Statement] = List(
Expand All @@ -88,8 +88,8 @@ object Macros {
Symbol.currentOwner,
"sym5",
MethodType(List("x"))(
_ => List(typeOf[Int]),
_ => typeOf[Int=>Int]))
_ => List(Type.of[Int]),
_ => Type.of[Int=>Int]))
assert(sym5.isDefDef)
assert(sym5.name == "sym5")
val sym5Statements : List[Statement] = List(
Expand All @@ -101,8 +101,8 @@ object Macros {
sym5,
"sym51",
MethodType(List("x"))(
_ => List(typeOf[Int]),
_ => typeOf[Int]))
_ => List(Type.of[Int]),
_ => Type.of[Int]))
Block(
List(
DefDef(sym51, {
Expand All @@ -122,14 +122,14 @@ object Macros {
Symbol.currentOwner,
"sym6_1",
MethodType(List("x"))(
_ => List(typeOf[Int]),
_ => typeOf[Int]))
_ => List(Type.of[Int]),
_ => Type.of[Int]))
val sym6_2 : Symbol = Symbol.newMethod(
Symbol.currentOwner,
"sym6_2",
MethodType(List("x"))(
_ => List(typeOf[Int]),
_ => typeOf[Int]))
_ => List(Type.of[Int]),
_ => Type.of[Int]))
assert(sym6_1.isDefDef)
assert(sym6_2.isDefDef)
assert(sym6_1.name == "sym6_1")
Expand Down Expand Up @@ -169,7 +169,7 @@ object Macros {
Symbol.currentOwner,
"sym7",
PolyType(List("T"))(
tp => List(TypeBounds(typeOf[Nothing], typeOf[Any])),
tp => List(TypeBounds(Type.of[Nothing], Type.of[Any])),
tp => MethodType(List("t"))(
_ => List(tp.param(0)),
_ => tp.param(0))))
Expand All @@ -182,7 +182,7 @@ object Macros {
Some(Typed(x, Inferred(t)))
}
}),
'{ assert(${ Apply(TypeApply(Ref(sym7), List(Inferred(typeOf[Int]))), List(Literal(Constant(7)))).seal.asInstanceOf[Expr[Int]] } == 7) }.unseal)
'{ assert(${ Apply(TypeApply(Ref(sym7), List(Inferred(Type.of[Int]))), List(Literal(Constant(7)))).seal.asInstanceOf[Expr[Int]] } == 7) }.unseal)

Block(
sym1Statements ++
Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/tasty-simplified/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object Macros {
loop(tp, Nil).reverse
}

val tps = unpackTuple(typeOf[T])
val tps = unpackTuple(Type.of[T])
Varargs(tps.map(x => Expr(x.show)))
}
}
34 changes: 17 additions & 17 deletions tests/run-macros/tasty-typeof/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ object Macros {
private def testTypeOfImpl(using qctx: QuoteContext) : Expr[Unit] = {
import qctx.tasty._
'{
assert(${Expr(typeOf[Unit] =:= defn.UnitType)}, "Unit")
assert(${Expr(typeOf[Byte] =:= defn.ByteType)}, "Byte")
assert(${Expr(typeOf[Short] =:= defn.ShortType)}, "Short")
assert(${Expr(typeOf[Int] =:= defn.IntType)}, "Int")
assert(${Expr(typeOf[Long] =:= defn.LongType)}, "Long")
assert(${Expr(typeOf[Float] =:= defn.FloatType)}, "Float")
assert(${Expr(typeOf[Double] =:= defn.DoubleType)}, "Double")
assert(${Expr(typeOf[Char] =:= defn.CharType)}, "Char")
assert(${Expr(typeOf[String] =:= defn.StringType)}, "String")
assert(${Expr(Type.of[Unit] =:= defn.UnitType)}, "Unit")
assert(${Expr(Type.of[Byte] =:= defn.ByteType)}, "Byte")
assert(${Expr(Type.of[Short] =:= defn.ShortType)}, "Short")
assert(${Expr(Type.of[Int] =:= defn.IntType)}, "Int")
assert(${Expr(Type.of[Long] =:= defn.LongType)}, "Long")
assert(${Expr(Type.of[Float] =:= defn.FloatType)}, "Float")
assert(${Expr(Type.of[Double] =:= defn.DoubleType)}, "Double")
assert(${Expr(Type.of[Char] =:= defn.CharType)}, "Char")
assert(${Expr(Type.of[String] =:= defn.StringType)}, "String")

assert(${Expr(typeOf[Any] =:= defn.AnyType)}, "Any")
assert(${Expr(typeOf[AnyRef] =:= defn.AnyRefType)}, "AnyRef")
assert(${Expr(typeOf[AnyVal] =:= defn.AnyValType)}, "AnyVal")
assert(${Expr(typeOf[Object] =:= defn.ObjectType)}, "Object")
assert(${Expr(typeOf[Nothing] =:= defn.NothingType)}, "Nothing")
assert(${Expr(Type.of[Any] =:= defn.AnyType)}, "Any")
assert(${Expr(Type.of[AnyRef] =:= defn.AnyRefType)}, "AnyRef")
assert(${Expr(Type.of[AnyVal] =:= defn.AnyValType)}, "AnyVal")
assert(${Expr(Type.of[Object] =:= defn.ObjectType)}, "Object")
assert(${Expr(Type.of[Nothing] =:= defn.NothingType)}, "Nothing")

println(${Expr(typeOf[List[Int]].show)})
println(${Expr(typeOf[Macros].show)})
println(${Expr(typeOf[Macros.type].show)})
println(${Expr(Type.of[List[Int]].show)})
println(${Expr(Type.of[Macros].show)})
println(${Expr(Type.of[Macros.type].show)})
}
}

Expand Down