Skip to content

Commit 77b7a3c

Browse files
authored
Merge pull request #6175 from dotty-staging/add-typeChecks-2
Add typeChecks to the library
2 parents 2364270 + 69a6134 commit 77b7a3c

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package scala.testing
2+
3+
import scala.quoted._
4+
import scala.tasty.Reflection
5+
6+
inline def typeChecks(inline code: String): Boolean = ${ typeChecksImpl(code) }
7+
8+
private def typeChecksImpl(code: String)(implicit reflect: Reflection): Expr[Boolean] = {
9+
import reflect._
10+
typing.typeChecks(code).toExpr
11+
}
12+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import scala.testing._
2+
3+
object Test {
4+
5+
trait Eq[T]
6+
implicit val eq: Eq[Int] = new Eq[Int] {}
7+
8+
implicit class AnyOps[T](x: T) {
9+
def === (y: T)(implicit c: Eq[T]) = x == y
10+
}
11+
12+
def main(args: Array[String]): Unit = {
13+
assert(typeChecks("5 === 5"))
14+
assert(!typeChecks("5.6 === 7.7"))
15+
16+
val x: Int = 5
17+
assert(typeChecks("x + 3"))
18+
assert(!typeChecks("y + 3"))
19+
import scala.util.Left
20+
assert(typeChecks("Left(3)"))
21+
assert(!typeChecks("Rigth(3)"))
22+
23+
def f(x: Int): Int = x * x
24+
assert(typeChecks("f(3)"))
25+
assert(!typeChecks("g(3)"))
26+
27+
type T
28+
assert(typeChecks("def foo(x: T): T = x"))
29+
assert(!typeChecks("foo(???)"))
30+
assert(!typeChecks("def foo(x: S): S = x"))
31+
32+
assert(!typeChecks("def test(x: Int) ="))
33+
34+
assert(typeChecks(
35+
"""
36+
class EqString extends Eq[String]
37+
new EqString
38+
"""
39+
))
40+
}
41+
}

0 commit comments

Comments
 (0)