Skip to content

Encode unpickle call directly on context #10238

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
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
Prev Previous commit
Next Next commit
Add types to unpickle methods
  • Loading branch information
nicolasstucki committed Nov 9, 2020
commit 87411b56b23451b8a5f751c97adda6a6ed34bbd3
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2628,13 +2628,13 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern

end reflect

def unpickleExpr(pickledQuote: PickledQuote): scala.quoted.Expr[Any] =
def unpickleExpr[T](pickledQuote: PickledQuote): scala.quoted.Expr[T] =
val tree = PickledQuotes.unpickleTerm(pickledQuote)(using reflect.rootContext)
new scala.internal.quoted.Expr(tree, hash)
new scala.internal.quoted.Expr(tree, hash).asInstanceOf[scala.quoted.Expr[T]]
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems to me that the original type signature is better. By returning the type Expr[T], there are no guarantees.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, there are no guarantees. Calls to this method should only be inserted be the compiler.
The previous version added the cast after each call to this method, effectively having the same guaranties.

I will add some extra documentation to make it clear that no one should be calling this method directly. Currently, it is hidden from the user as it is defined in QuoteContextInteranal.

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 general, all methods in QuoteContextInteranal should only be called by code generated by the compiler or something in the library.


def unpickleType(pickledQuote: PickledQuote): scala.quoted.Type[?] =
def unpickleType[T <: AnyKind](pickledQuote: PickledQuote): scala.quoted.Type[T] =
val tree = PickledQuotes.unpickleTypeTree(pickledQuote)(using reflect.rootContext)
new scala.internal.quoted.Type(tree, hash)
new scala.internal.quoted.Type(tree, hash).asInstanceOf[scala.quoted.Type[T]]

def exprMatch(scrutinee: scala.quoted.Expr[Any], pattern: scala.quoted.Expr[Any]): Option[Tuple] =
treeMatch(scrutinee.unseal(using this), pattern.unseal(using this))
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ class ReifyQuotes extends MacroTransform {
*
* Generate the code
* ```scala
* qctx => qctx.asInstanceOf[QuoteContextInternal].<unpickleExpr|unpickleType>(
* qctx => qctx.asInstanceOf[QuoteContextInternal].<unpickleExpr|unpickleType>[<type>](
* <pickledQuote>
* ).asInstanceOf[scala.quoted.<Expr|Type>[<type>]]
* )
* ```
* this closure is always applied directly to the actual context and the BetaReduce phase removes it.
*/
Expand All @@ -211,7 +211,7 @@ class ReifyQuotes extends MacroTransform {
def callUnpickle(ts: List[Tree]) = {
val qctx = ts.head.asInstance(defn.QuoteContextInternalClass.typeRef)
val unpickleMethName = if isType then "unpickleType" else "unpickleExpr"
qctx.select(unpickleMethName.toTermName).appliedTo(pickledQuote).asInstance(quotedType)
qctx.select(unpickleMethName.toTermName).appliedToType(originalTp).appliedTo(pickledQuote)
}
Lambda(lambdaTpe, callUnpickle).withSpan(body.span)
}
Expand Down
4 changes: 2 additions & 2 deletions library/src/scala/internal/quoted/QuoteContextInternal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ trait QuoteContextInternal { self: scala.quoted.QuoteContext =>
/** Unpickle `repr` which represents a pickled `Expr` tree,
* replacing splice nodes with `holes`
*/
def unpickleExpr(pickledQuote: PickledQuote): scala.quoted.Expr[Any]
def unpickleExpr[T](pickledQuote: PickledQuote): scala.quoted.Expr[T]

/** Unpickle `repr` which represents a pickled `Type` tree,
* replacing splice nodes with `holes`
*/
def unpickleType(pickledQuote: PickledQuote): scala.quoted.Type[?]
def unpickleType[T <: AnyKind](pickledQuote: PickledQuote): scala.quoted.Type[T]

/** Pattern matches the scrutinee against the pattern and returns a tuple
* with the matched holes if successful.
Expand Down