Skip to content

Add typeChecks to the library #6175

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 4 commits into from
Mar 27, 2019
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
15 changes: 15 additions & 0 deletions library/src-bootstrapped/scala/tests/TypeChecking.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package scala.tests

import scala.quoted._
import scala.tasty.Reflection

object TypeChecking {

inline def typeChecks(inline code: String): Boolean = ${ typeChecksImpl(code) }

private def typeChecksImpl(code: String)(implicit reflect: Reflection): Expr[Boolean] = {
import reflect._
typing.typeChecks(code).toExpr
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe remove private, as inlining will create an accessor if it's private.

Can we use top-level definitions to get rid of object TypeChecking?

Regarding scala.tests, what about scala.test?

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 try the top level definition and change to scala.test. typeChecksImpl must be private or it would be visible under import scala.test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the future, we will also support arbitrary code in top-level splices. Which will allow us to implement it like

inline def typeChecks(inline code: String): Boolean = ${
  val reflect = implicitly[Reflection]
  import reflect._
  typing.typeChecks(code).toExpr
}


}
41 changes: 41 additions & 0 deletions tests/run-with-compiler/scala-tests-typeChecks.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import scala.tests.TypeChecking._

object Test {

trait Eq[T]
implicit val eq: Eq[Int] = new Eq[Int] {}

implicit class AnyOps[T](x: T) {
def === (y: T)(implicit c: Eq[T]) = x == y
}

def main(args: Array[String]): Unit = {
assert(typeChecks("5 === 5"))
assert(!typeChecks("5.6 === 7.7"))

val x: Int = 5
assert(typeChecks("x + 3"))
assert(!typeChecks("y + 3"))
import scala.util.Left
assert(typeChecks("Left(3)"))
assert(!typeChecks("Rigth(3)"))

def f(x: Int): Int = x * x
assert(typeChecks("f(3)"))
assert(!typeChecks("g(3)"))

type T
assert(typeChecks("def foo(x: T): T = x"))
assert(!typeChecks("foo(???)"))
assert(!typeChecks("def foo(x: S): S = x"))

assert(!typeChecks("def test(x: Int) ="))

assert(typeChecks(
"""
class EqString extends Eq[String]
new EqString
"""
))
}
}