Skip to content

Move QuoteContext implementation to scala.quoted.internal.impl #10293

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import scala.io.Codec
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Phases.Phase
import dotty.tools.dotc.core.tasty.TastyPrinter
import dotty.tools.dotc.quoted.QuoteContextImpl
import dotty.tools.io.File

import scala.quoted.internal.impl.QuoteContextImpl

/** Phase that prints the trees in all loaded compilation units.
*
* @author Nicolas Stucki
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core._
import dotty.tools.dotc.core.tasty.TastyHTMLPrinter
import dotty.tools.dotc.reporting._
import dotty.tools.dotc.quoted.QuoteContextImpl

import scala.quoted.internal.impl.QuoteContextImpl

/**
* Decompiler to be used with IDEs
Expand Down
12 changes: 7 additions & 5 deletions compiler/src/dotty/tools/dotc/quoted/PickledQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import dotty.tools.dotc.report
import scala.reflect.ClassTag

import scala.quoted.QuoteContext
import scala.quoted.internal.impl._

import scala.collection.mutable

import QuoteUtils._
Expand All @@ -38,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[dotty.tools.dotc.quoted.ExprImpl]
val expr1 = expr.asInstanceOf[ExprImpl]
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[dotty.tools.dotc.quoted.TypeImpl]
val tpe1 = tpe.asInstanceOf[TypeImpl]
tpe1.checkScopeId(QuoteContextImpl.scopeId)
changeOwnerOfTree(tpe1.typeTree, ctx.owner)
}
Expand All @@ -72,11 +74,11 @@ object PickledQuotes {
override def transform(tree: tpd.Tree)(using Context): tpd.Tree = tree match {
case Hole(isTerm, idx, args) =>
val reifiedArgs = args.map { arg =>
if (arg.isTerm) (using qctx: QuoteContext) => new dotty.tools.dotc.quoted.ExprImpl(arg, QuoteContextImpl.scopeId)
else new dotty.tools.dotc.quoted.TypeImpl(arg, QuoteContextImpl.scopeId)
if (arg.isTerm) (using qctx: QuoteContext) => new ExprImpl(arg, QuoteContextImpl.scopeId)
else new TypeImpl(arg, QuoteContextImpl.scopeId)
}
if isTerm then
val quotedExpr = termHole(idx, reifiedArgs, dotty.tools.dotc.quoted.QuoteContextImpl())
val quotedExpr = termHole(idx, reifiedArgs, QuoteContextImpl())
val filled = PickledQuotes.quotedExprToTree(quotedExpr)

// We need to make sure a hole is created with the source file of the surrounding context, even if
Expand Down
8 changes: 5 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import dotty.tools.repl.AbstractFileClassLoader

import scala.reflect.ClassTag

import dotty.tools.dotc.quoted._
import dotty.tools.dotc.quoted.{PickledQuotes, QuoteUtils}

import scala.quoted.QuoteContext
import scala.quoted.internal.impl._

/** Utility class to splice quoted expressions */
object Splicer {
Expand Down Expand Up @@ -323,10 +325,10 @@ object Splicer {
}

private def interpretQuote(tree: Tree)(implicit env: Env): Object =
new dotty.tools.dotc.quoted.ExprImpl(Inlined(EmptyTree, Nil, QuoteUtils.changeOwnerOfTree(tree, ctx.owner)).withSpan(tree.span), QuoteContextImpl.scopeId)
new ExprImpl(Inlined(EmptyTree, Nil, QuoteUtils.changeOwnerOfTree(tree, ctx.owner)).withSpan(tree.span), QuoteContextImpl.scopeId)

private def interpretTypeQuote(tree: Tree)(implicit env: Env): Object =
new dotty.tools.dotc.quoted.TypeImpl(QuoteUtils.changeOwnerOfTree(tree, ctx.owner), QuoteContextImpl.scopeId)
new TypeImpl(QuoteUtils.changeOwnerOfTree(tree, ctx.owner), QuoteContextImpl.scopeId)

private def interpretLiteral(value: Any)(implicit env: Env): Object =
value.asInstanceOf[Object]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dotty.tools.dotc.quoted

import scala.quoted._
package scala.quoted
package internal.impl
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the consideration here for prefering scala.quoted. internal.impl over dotty.tools.dotc.quoted?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to add ExprImpl and TypeImpl in the scala package as Expr/Type have private[scala] constructors. If we have that it also makes sense to put the other related classes in there for consistency.


import dotty.tools.dotc.ast.tpd

Expand All @@ -11,7 +10,7 @@ import dotty.tools.dotc.ast.tpd
*
* May contain references to code defined outside this Expr instance.
*/
final class ExprImpl(val tree: tpd.Tree, val scopeId: Int) extends scala.quoted.internal.Expr[Any] {
final class ExprImpl(val tree: tpd.Tree, val scopeId: Int) extends Expr[Any] {
override def equals(that: Any): Boolean = that match {
case that: ExprImpl =>
// Expr are wrappers around trees, therefore they are equals if their trees are equal.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package dotty.tools.dotc.quoted
package scala.quoted
package internal.impl

import scala.annotation.internal.sharable
import scala.annotation.{Annotation, compileTimeOnly}

import scala.quoted._

/** Matches a quoted tree against a quoted pattern tree.
* A quoted pattern tree may have type and term holes in addition to normal terms.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package dotty.tools.dotc.quoted
package scala.quoted
package internal.impl

import dotty.tools.dotc
import dotty.tools.dotc.ast.tpd
Expand All @@ -13,9 +14,11 @@ import dotty.tools.dotc.quoted.reflect._
import dotty.tools.dotc.quoted.QuoteUtils._
import dotty.tools.dotc.core.Decorators._

import scala.quoted.QuoteContext
import dotty.tools.dotc.quoted.{MacroExpansion, PickledQuotes, QuoteUtils}

import scala.quoted.internal.{QuoteUnpickler, QuoteMatching}
import dotty.tools.dotc.quoted.printers.{Extractors, SourceCode, SyntaxHighlight}
import scala.quoted.internal.impl.printers._


import scala.tasty.reflect._

Expand All @@ -36,7 +39,7 @@ object QuoteContextImpl {

// 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 =
def scopeId(using Context): ScopeId =
ctx.outersIterator.toList.last.hashCode()

}
Expand Down Expand Up @@ -2708,7 +2711,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
ctx1.gadt.addToConstraint(typeHoles)
ctx1

val qctx1 = dotty.tools.dotc.quoted.QuoteContextImpl()(using ctx1)
val qctx1 = QuoteContextImpl()(using ctx1)

val matcher = new Matcher.QuoteMatcher[qctx1.type](qctx1) {
def patternHoleSymbol: qctx1.reflect.Symbol = dotc.core.Symbols.defn.InternalQuotedPatterns_patternHole.asInstanceOf
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package dotty.tools.dotc.quoted
package scala.quoted.internal.impl

class ScopeException(msg: String) extends Exception(msg)
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package dotty.tools.dotc.quoted

import scala.quoted._
package scala.quoted
package internal.impl

import dotty.tools.dotc.ast.tpd

/** Quoted type (or kind) `T` backed by a tree */
final class TypeImpl(val typeTree: tpd.Tree, val scopeId: Int) extends scala.quoted.internal.Type[?] {
final class TypeImpl(val typeTree: tpd.Tree, val scopeId: Int) extends Type[?] {
override def equals(that: Any): Boolean = that match {
case that: TypeImpl => typeTree ==
// TastyTreeExpr are wrappers around trees, therfore they are equals if their trees are equal.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package dotty.tools.dotc.quoted.printers
package scala.quoted
package internal.impl.printers

import scala.quoted._

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package dotty.tools.dotc
package quoted.printers
package scala.quoted
package internal.impl.printers

import scala.annotation.switch
import scala.quoted._

/** Printer for fully elaborated representation of the source code */
object SourceCode {
Expand Down Expand Up @@ -437,7 +436,7 @@ object SourceCode {
case _ =>
inParens {
printTree(term)
this += (if (util.Chars.isOperatorPart(sb.last)) " : " else ": ")
this += (if (dotty.tools.dotc.util.Chars.isOperatorPart(sb.last)) " : " else ": ")
def printTypeOrAnnots(tpe: TypeRepr): Unit = tpe match {
case AnnotatedType(tp, annot) if tp == term.tpe =>
printAnnotation(annot)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package dotty.tools.dotc.quoted.printers
package scala.quoted
package internal.impl.printers

trait SyntaxHighlight {
def highlightKeyword(str: String): String
Expand Down
3 changes: 0 additions & 3 deletions library/src/scala/quoted/internal/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package internal

import scala.annotation.{Annotation, compileTimeOnly}

/** Implementation of scala.quoted.Expr that sould only be extended by the implementation of `QuoteContext` */
abstract class Expr[+T] extends scala.quoted.Expr[T]

@compileTimeOnly("Illegal reference to `scala.quoted.internal.Expr`")
object Expr:

Expand Down
4 changes: 0 additions & 4 deletions library/src/scala/quoted/internal/Type.scala

This file was deleted.

2 changes: 1 addition & 1 deletion scala3doc/src/dotty/dokka/tasty/TastyParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ case class SbtDokkaTastyInspector(
import dotty.tools.dotc.core.Mode
import dotty.tools.dotc.core.Phases.Phase
import dotty.tools.dotc.fromtasty._
import dotty.tools.dotc.quoted.QuoteContextImpl
import scala.quoted.internal.impl.QuoteContextImpl


val parser: Parser = null
Expand Down
4 changes: 3 additions & 1 deletion staging/src/scala/quoted/staging/QuoteCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import dotty.tools.dotc.util.Spans.Span
import dotty.tools.dotc.util.SourceFile
import dotty.tools.io.{Path, VirtualFile}

import scala.quoted.internal.impl.QuoteContextImpl

import scala.annotation.tailrec
import scala.concurrent.Promise
import scala.quoted.{Expr, QuoteContext, Type}
Expand Down Expand Up @@ -68,7 +70,7 @@ private class QuoteCompiler extends Compiler:

val quoted =
given Context = unitCtx.withOwner(meth)
val qctx = dotty.tools.dotc.quoted.QuoteContextImpl()
val qctx = QuoteContextImpl()
val quoted = PickledQuotes.quotedExprToTree(exprUnit.exprBuilder.apply(qctx))
checkEscapedVariables(quoted, meth)
end quoted
Expand Down
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 @@ -3,7 +3,7 @@ package staging

import scala.annotation.implicitNotFound

import dotty.tools.dotc.quoted.ScopeException
import scala.quoted.internal.impl.ScopeException

@implicitNotFound("Could not find implicit scala.quoted.staging.Toolbox.\n\nDefault toolbox can be instantiated with:\n `given scala.quoted.staging.Toolbox = scala.quoted.staging.Toolbox.make(getClass.getClassLoader)`\n\n")
trait Toolbox:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scala.tasty.inspector

import scala.quoted._
import scala.quoted.internal.impl.QuoteContextImpl

import dotty.tools.dotc.Compiler
import dotty.tools.dotc.Driver
Expand All @@ -9,7 +10,6 @@ import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.core.Mode
import dotty.tools.dotc.core.Phases.Phase
import dotty.tools.dotc.fromtasty._
import dotty.tools.dotc.quoted.QuoteContextImpl
import dotty.tools.dotc.util.ClasspathFromClassloader

import java.io.File.pathSeparator
Expand Down
4 changes: 2 additions & 2 deletions tests/run-staging/i4730.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object Test {
given Toolbox = Toolbox.make(getClass.getClassLoader)
def ret(using QuoteContext): Expr[Int => Int] = '{ (x: Int) =>
${
val z = run('{x + 1}) // throws dotty.tools.dotc.quoted.ScopeException =>
val z = run('{x + 1}) // throws scala.quoted.internal.impl.ScopeException =>
Expr(z)
}
}
Expand All @@ -21,7 +21,7 @@ package scala {
run(Test.ret).apply(10)
throw new Exception
} catch {
case ex: Exception if ex.getClass.getName == "dotty.tools.dotc.quoted.ScopeException" =>
case ex: Exception if ex.getClass.getName == "scala.quoted.internal.impl.ScopeException" =>
// ok
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/run-staging/i6754.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package scala {
throw new Exception
} catch {
case ex: java.lang.reflect.InvocationTargetException =>
assert(ex.getTargetException.getClass.getName == "dotty.tools.dotc.quoted.ScopeException")
assert(ex.getTargetException.getClass.getName == "scala.quoted.internal.impl.ScopeException")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/run-staging/i6992/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ package scala {
case '{$x: Foo} => Expr(run(x).x)
}
} catch {
case ex: Exception if ex.getClass.getName == "dotty.tools.dotc.quoted.ScopeException" =>
case ex: Exception if ex.getClass.getName == "scala.quoted.internal.impl.ScopeException" =>
'{"OK"}
}
}
Expand Down