Skip to content

Move quoted null and unit encoding to internal #9551

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 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/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,6 @@ class Definitions {

@tu lazy val QuotedExprClass: ClassSymbol = requiredClass("scala.quoted.Expr")
@tu lazy val QuotedExprModule: Symbol = QuotedExprClass.companionModule
@tu lazy val QuotedExprModule_nullExpr: Symbol = QuotedExprModule.requiredMethod(nme.nullExpr)
@tu lazy val QuotedExprModule_unitExpr: Symbol = QuotedExprModule.requiredMethod(nme.unitExpr)

@tu lazy val QuoteContextClass: ClassSymbol = requiredClass("scala.quoted.QuoteContext")

Expand Down Expand Up @@ -716,6 +714,8 @@ class Definitions {

@tu lazy val InternalQuotedExprModule: Symbol = requiredModule("scala.internal.quoted.Expr")
@tu lazy val InternalQuotedExpr_unapply: Symbol = InternalQuotedExprModule.requiredMethod(nme.unapply)
@tu lazy val InternalQuotedExpr_null: Symbol = InternalQuotedExprModule.requiredMethod(nme.null_)
@tu lazy val InternalQuotedExpr_unit: Symbol = InternalQuotedExprModule.requiredMethod(nme.Unit)

@tu lazy val InternalQuotedTypeModule: Symbol = requiredModule("scala.internal.quoted.Type")
@tu lazy val InternalQuotedType_unapply: Symbol = InternalQuotedTypeModule.requiredMethod(nme.unapply)
Expand Down
2 changes: 0 additions & 2 deletions compiler/src/dotty/tools/dotc/core/StdNames.scala
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,6 @@ object StdNames {
val notifyAll_ : N = "notifyAll"
val notify_ : N = "notify"
val null_ : N = "null"
val nullExpr: N = "nullExpr"
val ofDim: N = "ofDim"
val on: N = "on"
val opaque: N = "opaque"
Expand Down Expand Up @@ -608,7 +607,6 @@ object StdNames {
val unapply: N = "unapply"
val unapplySeq: N = "unapplySeq"
val unbox: N = "unbox"
val unitExpr: N = "unitExpr"
val universe: N = "universe"
val update: N = "update"
val updateDynamic: N = "updateDynamic"
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ class ReifyQuotes extends MacroTransform {
def liftedValue(lifter: Symbol) =
ref(lifter).appliedToType(originalTp).select(nme.toExpr).appliedTo(lit)
lit.const.tag match {
case Constants.NullTag => ref(defn.QuotedExprModule_nullExpr)
case Constants.UnitTag => ref(defn.QuotedExprModule_unitExpr)
case Constants.NullTag => ref(defn.InternalQuotedExpr_null)
case Constants.UnitTag => ref(defn.InternalQuotedExpr_unit)
case Constants.BooleanTag => liftedValue(defn.LiftableModule_BooleanIsLiftable)
case Constants.ByteTag => liftedValue(defn.LiftableModule_ByteIsLiftable)
case Constants.ShortTag => liftedValue(defn.LiftableModule_ShortIsLiftable)
Expand Down
12 changes: 0 additions & 12 deletions library/src-bootstrapped/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,6 @@ object Expr {
case Some(expr1) => expr1.seal.asInstanceOf[Expr[T]]
case _ => expr

/** Returns a null expresssion equivalent to `'{null}` */
def nullExpr: QuoteContext ?=> Expr[Null] = qctx ?=> {
import qctx.tasty._
Literal(Constant(null)).seal.asInstanceOf[Expr[Null]]
}

/** Returns a unit expresssion equivalent to `'{}` or `'{()}` */
def unitExpr: QuoteContext ?=> Expr[Unit] = qctx ?=> {
import qctx.tasty._
Literal(Constant(())).seal.asInstanceOf[Expr[Unit]]
}

/** Returns an expression containing a block with the given statements and ending with the expresion
* Given list of statements `s1 :: s2 :: ... :: Nil` and an expression `e` the resulting expression
* will be equivalent to `'{ $s1; $s2; ...; $e }`.
Expand Down
12 changes: 12 additions & 0 deletions library/src/scala/internal/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,16 @@ object Expr {
new Matcher.QuoteMatcher[qctx.type].termMatch(scrutineeExpr.unseal, patternExpr.unseal, hasTypeSplices).asInstanceOf[Option[Tup]]
}

/** Returns a null expresssion equivalent to `'{null}` */
def `null`: QuoteContext ?=> quoted.Expr[Null] = qctx ?=> {
import qctx.tasty._
Literal(Constant(null)).seal.asInstanceOf[quoted.Expr[Null]]
}

/** Returns a unit expresssion equivalent to `'{}` or `'{()}` */
def Unit: QuoteContext ?=> quoted.Expr[Unit] = qctx ?=> {
import qctx.tasty._
Literal(Constant(())).seal.asInstanceOf[quoted.Expr[Unit]]
}
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 both can be val instead of def?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They could but they would not get the optimization where the context parameter is passed directly as one of the method parameters and the closure is not created. We effectively call the following method at runtime (not closure creation needed):

def Unit(using q: QuoteContext): quoted.Expr[Unit] = {
  import qctx.tasty._
  Literal(Constant(())).seal.asInstanceOf[quoted.Expr[Unit]]
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point, maybe we can write it directly, instead of the original form?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is like this to align with the quote internal encoding to simplify the transformation.

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 will consider changing the encoding but this is a larger change that will take some time to accomplish.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the clarification, it makes sense.


}
2 changes: 1 addition & 1 deletion staging/src/scala/quoted/staging/staging.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ package object staging:
var result: T = noResult.asInstanceOf[T]
def dummyRun(using QuoteContext): Expr[Unit] = {
result = thunk
Expr.unitExpr
'{}
}
toolbox.run(dummyRun(using _))
assert(result != noResult) // toolbox.run should have thrown an exception
Expand Down