Skip to content
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

Add <:< and =:= to testy reflect Type #4887

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
6 changes: 6 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,12 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
// ----- Types ----------------------------------------------------

type Type = Types.Type

def TypeDeco(tpe: Type): TypeAPI = new TypeAPI {
def =:=(other: Type)(implicit ctx: Context): Boolean = tpe =:= other
def <:<(other: Type)(implicit ctx: Context): Boolean = tpe <:< other
}

type RecursiveType = Types.RecType
type LambdaType[ParamInfo <: TypeOrBounds] = Types.LambdaType { type PInfo = ParamInfo }
type MethodType = Types.MethodType
Expand Down
6 changes: 6 additions & 0 deletions library/src/scala/tasty/Tasty.scala
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,12 @@ abstract class Tasty { tasty =>

type Type <: TypeOrBounds

trait TypeAPI {
def =:=(other: Type)(implicit ctx: Context): Boolean
def <:<(other: Type)(implicit ctx: Context): Boolean
}
implicit def TypeDeco(tpe: Type): TypeAPI
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR, but we were thinking about making the implicit defs inline — did we forget?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do a larger refactor later. Now we don't have a standard way to inline both on bootstrap and non bootstraped


type RecursiveType <: Type

type LambdaType[ParamInfo <: TypeOrBounds] <: Type
Expand Down
24 changes: 24 additions & 0 deletions tests/run/tasty-subtyping.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
true
true
true
true
true

false
false
false
false
false

true
true
true
true
true
true

false
false
false
false
false
24 changes: 24 additions & 0 deletions tests/run/tasty-subtyping/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import scala.quoted._

import scala.tasty._

object Macros {

transparent def isTypeEqual[T, U]: Boolean =
~isTypeEqualImpl('[T], '[U])(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~

transparent def isSubTypeOf[T, U]: Boolean =
~isSubTypeOfImpl('[T], '[U])(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~

def isTypeEqualImpl[T, U](t: Type[T], u: Type[U])(implicit tasty: Tasty): Expr[Boolean] = {
import tasty._
val isTypeEqual = t.toTasty.tpe =:= u.toTasty.tpe
isTypeEqual.toExpr
}

def isSubTypeOfImpl[T, U](t: Type[T], u: Type[U])(implicit tasty: Tasty): Expr[Boolean] = {
import tasty._
val isTypeEqual = t.toTasty.tpe <:< u.toTasty.tpe
isTypeEqual.toExpr
}
}
41 changes: 41 additions & 0 deletions tests/run/tasty-subtyping/quoted_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

import Macros._

object Test {
type A
type B <: A

def main(args: Array[String]): Unit = {
// true
println(isTypeEqual[Int, Int])
println(isTypeEqual[String, String])
println(isTypeEqual[Test.type, Test.type])
println(isTypeEqual[2, 2])
println(isTypeEqual[A, A])
println()

// false
println(isTypeEqual[Int, Double])
println(isTypeEqual[String, Int])
println(isTypeEqual[Test.type, Macros.type])
println(isTypeEqual[2, 1])
println(isTypeEqual[A, B])
println()

// true
println(isSubTypeOf[Int, Int])
println(isSubTypeOf[String, Object])
println(isSubTypeOf[Int, AnyVal])
println(isSubTypeOf[AnyRef, Any])
println(isSubTypeOf[AnyVal, Any])
println(isSubTypeOf[B, A])
println()

// false
println(isSubTypeOf[Object, Int])
println(isSubTypeOf[AnyVal, Int])
println(isSubTypeOf[Any, AnyRef])
println(isSubTypeOf[Any, AnyVal])
println(isSubTypeOf[A, B])
}
}