Skip to content

Commit 4652b7b

Browse files
committed
Address review
1 parent 90371d2 commit 4652b7b

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
6060
* The code should be a sequence of expressions or statements that may appear in a block.
6161
*/
6262
def typeChecks(code: String)(implicit ctx: Context): Boolean = {
63-
val tree = new Parser(SourceFile.virtual("tasty-reflect", code)).block()
64-
6563
val ctx2 = ctx.fresh.setNewTyperState().setTyper(new Typer)
66-
ctx2.typer.typed(tree)(ctx2)
67-
!ctx2.reporter.hasErrors
64+
val tree = new Parser(SourceFile.virtual("tasty-reflect", code))(ctx2).block()
65+
66+
!ctx2.reporter.hasErrors && {
67+
ctx2.typer.typed(tree)(ctx2)
68+
!ctx2.reporter.hasErrors
69+
}
6870
}
6971

7072
//

library/src/scala/tasty/Reflection.scala

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ abstract class Reflection
2727
def typeOf[T: scala.quoted.Type]: Type =
2828
implicitly[scala.quoted.Type[T]].unseal.tpe
2929

30-
31-
/** Whether the code type checks in the given context?
32-
*
33-
* @param code The code to be type checked
34-
*
35-
* The code should be a sequence of expressions or statements that may appear in a block.
36-
*/
37-
def typeChecks(code: String)(implicit ctx: Context): Boolean = kernel.typeChecks(code)(ctx)
30+
object typing {
31+
/** Whether the code type checks in the given context?
32+
*
33+
* @param code The code to be type checked
34+
*
35+
* @return false if the code has syntax error or type error in the given context, otherwise returns true.
36+
*
37+
* The code should be a sequence of expressions or statements that may appear in a block.
38+
*/
39+
def typeChecks(code: String)(implicit ctx: Context): Boolean = kernel.typeChecks(code)(ctx)
40+
}
3841

3942
val util: reflect.utils.TreeUtils { val reflect: self.type } = new reflect.utils.TreeUtils {
4043
val reflect: self.type = self

tests/run-with-compiler/reflect-typeChecks/assert_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import scala.tasty._
33

44
object scalatest {
55

6-
inline def assertCompiles(inline code: String): Unit = ${ assertImpl(code, true) }
6+
inline def assertCompile(inline code: String): Unit = ${ assertImpl(code, true) }
77
inline def assertNotCompile(inline code: String): Unit = ${ assertImpl(code, false) }
88

99
def assertImpl(code: String, expect: Boolean)(implicit refl: Reflection): Expr[Unit] = {
1010
import refl._
1111

12-
val actual = typeChecks(code)
12+
val actual = typing.typeChecks(code)
1313

1414
'{ assert(${expect.toExpr} == ${actual.toExpr}) }
1515
}

tests/run-with-compiler/reflect-typeChecks/test_2.scala

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,32 @@ object Test {
99
}
1010

1111
def main(args: Array[String]): Unit = {
12-
assertCompiles("5 === 5")
12+
assertCompile("5 === 5")
1313
assertNotCompile("5.6 === 7.7")
14+
15+
val x: Int = 5
16+
assertCompile("x + 3")
17+
assertNotCompile("y + 3")
18+
import scala.util.Left
19+
assertCompile("Left(3)")
20+
assertNotCompile("Rigth(3)")
21+
22+
def f(x: Int): Int = x * x
23+
assertCompile("f(3)")
24+
assertNotCompile("g(3)")
25+
26+
type T
27+
assertCompile("def foo(x: T): T = x")
28+
assertNotCompile("foo(???)")
29+
assertNotCompile("def foo(x: S): S = x")
30+
31+
assertNotCompile("def test(x: Int) =")
32+
33+
assertCompile(
34+
"""
35+
class EqString extends Eq[String]
36+
new EqString
37+
"""
38+
)
1439
}
1540
}

0 commit comments

Comments
 (0)