Skip to content

Hide quote related exceptions from users #10202

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
Nov 7, 2020
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
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/quoted/PickledQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ object PickledQuotes {
/** Transform the expression into its fully spliced Tree */
def quotedExprToTree[T](expr: quoted.Expr[T])(using Context): Tree = {
val expr1 = expr.asInstanceOf[scala.internal.quoted.Expr[Tree]]
QuoteContextImpl.checkScopeId(expr1.scopeId)
expr1.checkScopeId(QuoteContextImpl.scopeId)
changeOwnerOfTree(expr1.tree, ctx.owner)
}

/** Transform the expression into its fully spliced TypeTree */
def quotedTypeToTree(tpe: quoted.Type[?])(using Context): Tree = {
val tpe1 = tpe.asInstanceOf[scala.internal.quoted.Type[Tree]]
QuoteContextImpl.checkScopeId(tpe1.scopeId)
tpe1.checkScopeId(QuoteContextImpl.scopeId)
changeOwnerOfTree(tpe1.typeTree, ctx.owner)
}

Expand Down
4 changes: 0 additions & 4 deletions compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ object QuoteContextImpl {
qctx.reflect.TreeMethodsImpl.extension_show(tree)
}

private[dotty] def checkScopeId(id: ScopeId)(using Context): Unit =
if (id != scopeId)
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")

// TODO Explore more fine grained scope ids.
// This id can only differentiate scope extrusion from one compiler instance to another.
private[dotty] def scopeId(using Context): ScopeId =
Expand Down
11 changes: 7 additions & 4 deletions library/src-bootstrapped/scala/internal/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@ import scala.internal.quoted.CompilerInterface.quoteContextWithCompilerInterface
*
* May contain references to code defined outside this Expr instance.
*/
final class Expr[Tree](val tree: Tree, val scopeId: Int) extends scala.quoted.Expr[Any] {
final class Expr[Tree](val tree: Tree, val scopeId: Int) extends scala.quoted.Expr[Any] {
override def equals(that: Any): Boolean = that match {
case that: Expr[_] =>
// Expr are wrappers around trees, therfore they are equals if their trees are equal.
// Expr are wrappers around trees, therefore they are equals if their trees are equal.
// All scopeId should be equal unless two different runs of the compiler created the trees.
tree == that.tree && scopeId == that.scopeId
case _ => false
}

def unseal(using qctx: QuoteContext): qctx.reflect.Term =
if (qctx.hashCode != scopeId)
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
checkScopeId(qctx.hashCode)
tree.asInstanceOf[qctx.reflect.Term]

def checkScopeId(expectedScopeId: Int): Unit =
if expectedScopeId != scopeId then
throw new scala.internal.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")

override def hashCode: Int = tree.hashCode
override def toString: String = "'{ ... }"
}
Expand Down
7 changes: 5 additions & 2 deletions library/src-bootstrapped/scala/internal/quoted/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quote

/** View this expression `quoted.Type[T]` as a `TypeTree` */
def unseal(using qctx: QuoteContext): qctx.reflect.TypeTree =
if (qctx.hashCode != scopeId)
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
checkScopeId(qctx.hashCode)
typeTree.asInstanceOf[qctx.reflect.TypeTree]

def checkScopeId(expectedScopeId: Int): Unit =
if expectedScopeId != scopeId then
throw new scala.internal.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")

override def hashCode: Int = typeTree.hashCode
override def toString: String = "'[ ... ]"
}
Expand Down
2 changes: 1 addition & 1 deletion library/src-bootstrapped/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class Expr[+T] private[scala] {
if isExprOf[X] then
this.asInstanceOf[scala.quoted.Expr[X]]
else
throw new tasty.reflect.ExprCastError(
throw new scala.internal.quoted.ExprCastError(
s"""Expr: ${this.show}
|of type: ${this.unseal.tpe.show}
|did not conform to type: ${tp.unseal.tpe.show}
Expand Down
7 changes: 4 additions & 3 deletions library/src-non-bootstrapped/scala/internal/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import scala.quoted._
}

def unseal(using qctx: QuoteContext): qctx.reflect.Term =
if (qctx.hashCode != scopeId)
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
tree.asInstanceOf[qctx.reflect.Term]
throw new Exception("Non bootstrapped lib")

def checkScopeId(scopeId: Int): Unit =
throw new Exception("Non bootstrapped lib")

override def hashCode: Int = tree.hashCode
override def toString: String = "'{ ... }"
Expand Down
7 changes: 4 additions & 3 deletions library/src-non-bootstrapped/scala/internal/quoted/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quote

/** View this expression `quoted.Type[T]` as a `TypeTree` */
def unseal(using qctx: QuoteContext): qctx.reflect.TypeTree =
if (qctx.hashCode != scopeId)
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
typeTree.asInstanceOf[qctx.reflect.TypeTree]
throw new Exception("Non bootstrapped lib")

def checkScopeId(scopeId: Int): Unit =
throw new Exception("Non bootstrapped lib")

override def hashCode: Int = typeTree.hashCode
override def toString: String = "'[ ... ]"
Expand Down
4 changes: 4 additions & 0 deletions library/src/scala/internal/quoted/ExprCastError.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package scala.internal.quoted

/** Exception thrown when an `Expr[?]` is casted to a `Expr[U]` and the expression is not of type `U` */
private[scala] class ExprCastError(msg: String) extends Throwable(msg)
4 changes: 4 additions & 0 deletions library/src/scala/internal/quoted/ScopeException.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package scala.internal.quoted
Copy link
Contributor

Choose a reason for hiding this comment

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

What about private? Or use scala.quoted.internal?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is one use in the compiler. I will try to remove it and make it private.

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 made it private


/** Exception thrown when an Expr or Type is used ouside of the scope where it is valid */
private[scala] class ScopeException(msg: String) extends Exception(msg)
4 changes: 0 additions & 4 deletions library/src/scala/quoted/ScopeException.scala

This file was deleted.

3 changes: 0 additions & 3 deletions library/src/scala/tasty/reflect/ExprCastError.scala

This file was deleted.

2 changes: 1 addition & 1 deletion staging/src/scala/quoted/staging/Toolbox.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object Toolbox:
def run[T](exprBuilder: QuoteContext => Expr[T]): T = synchronized {
try
if (running) // detected nested run
throw new ScopeException("Cannot call `scala.quoted.staging.run(...)` within a another `run(...)`")
throw new scala.internal.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a another `run(...)`")
running = true
driver.run(exprBuilder, settings)
finally
Expand Down
20 changes: 14 additions & 6 deletions tests/run-staging/i4730.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ object Test {
}
}
def main(args: Array[String]): Unit = {
try {
run(ret).apply(10)
throw new Exception
} catch {
case ex: scala.quoted.ScopeException =>
// ok
scala.mytest.myTest()
}
}

package scala {
package mytest {
def myTest()(using Toolbox) = {
try {
run(Test.ret).apply(10)
throw new Exception
} catch {
case ex: scala.internal.quoted.ScopeException =>
// ok
}
}
}
}
33 changes: 20 additions & 13 deletions tests/run-staging/i6754.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@ import scala.quoted._
import scala.quoted.staging._

object Test {
implicit val tbx: scala.quoted.staging.Toolbox = scala.quoted.staging.Toolbox.make(getClass.getClassLoader)
def main(args: Array[String]): Unit =
scala.MyTest.myTest()
}

def main(args: Array[String]): Unit = {
def y(using QuoteContext): Expr[Unit] = '{
def x(using QuoteContext): Expr[Unit] = '{println("bar")}
println("foo")
run(x)
}
try {
run(y)
throw new Exception
} catch {
case ex: java.lang.reflect.InvocationTargetException =>
assert(ex.getTargetException.isInstanceOf[scala.quoted.ScopeException])
package scala {
object MyTest {
implicit val tbx: scala.quoted.staging.Toolbox = scala.quoted.staging.Toolbox.make(getClass.getClassLoader)

def myTest() = {
def y(using QuoteContext): Expr[Unit] = '{
def x(using QuoteContext): Expr[Unit] = '{println("bar")}
println("foo")
run(x)
}
try {
run(y)
throw new Exception
} catch {
case ex: java.lang.reflect.InvocationTargetException =>
assert(ex.getTargetException.isInstanceOf[scala.internal.quoted.ScopeException])
}
}
}
}
28 changes: 19 additions & 9 deletions tests/run-staging/i6992/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@
import scala.quoted._
import scala.quoted.staging._

given Toolbox = Toolbox.make(getClass.getClassLoader)

object macros {
inline def mcr(x: => Any): Any = ${mcrImpl('x)}

class Foo { val x = 10 }

def mcrImpl(body: Expr[Any])(using ctx: QuoteContext): Expr[Any] = {
import ctx.reflect._
try {
body match {
case '{$x: Foo} => Expr(run(x).x)
def mcrImpl(body: Expr[Any])(using ctx: QuoteContext): Expr[Any] =
MyTest.mcrImpl(body)
}

package scala {
object MyTest {
import macros._

given Toolbox = Toolbox.make(getClass.getClassLoader)

def mcrImpl(body: Expr[Any])(using ctx: QuoteContext): Expr[Any] = {
import ctx.reflect._
try {
body match {
case '{$x: Foo} => Expr(run(x).x)
}
} catch {
case ex: scala.internal.quoted.ScopeException =>
'{"OK"}
}
} catch {
case ex: scala.quoted.ScopeException =>
'{"OK"}
}
}
}