Skip to content

Fix #9812: Do not widen types during quote reification #9814

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 3 commits into from
Sep 17, 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
10 changes: 2 additions & 8 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,10 @@ class ReifyQuotes extends MacroTransform {
}

def pickleAsTasty() = {
val meth =
if (isType) ref(defn.Unpickler_unpickleType).appliedToType(originalTp)
else
val tpe =
if originalTp =:= defn.NilModule.termRef then originalTp // Workaround #4987
else originalTp.widen.dealias
ref(defn.Unpickler_unpickleExpr).appliedToType(tpe)
val meth = if isType then defn.Unpickler_unpickleType else defn.Unpickler_unpickleExpr
val pickledQuoteStrings = liftList(PickledQuotes.pickleQuote(body).map(x => Literal(Constant(x))), defn.StringType)
val splicesList = liftList(splices, defn.FunctionType(1).appliedTo(defn.SeqType.appliedTo(defn.AnyType), defn.AnyType))
meth.appliedTo(pickledQuoteStrings, splicesList)
ref(meth).appliedToType(originalTp).appliedTo(pickledQuoteStrings, splicesList)
}

def taggedType(sym: Symbol) = ref(defn.InternalQuotedTypeModule).select(sym.name.toTermName)
Expand Down
8 changes: 8 additions & 0 deletions tests/pos-macros/i9812.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// compile with -Ycheck:reifyQuotes -Ystop-after:reifyQuotes
import quoted._

sealed abstract class SomeEnum
object SomeEnum:
final val Foo = new SomeEnum {}

def quoteFoo: QuoteContext ?=> Expr[SomeEnum.Foo.type] = '{SomeEnum.Foo}
51 changes: 51 additions & 0 deletions tests/run-macros/i9812b/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import quoted._
import SomeEnum._

trait Liftable[T] {
/** Lift a value into an expression containing the construction of that value */
def toExpr(x: T): QuoteContext ?=> Expr[T]
}

object Lift:
def apply[T: Liftable](t: T)(using qctx: QuoteContext, ev: Liftable[T]): Expr[T] = ev.toExpr(t)

sealed abstract class SomeEnum
object SomeEnum:
final val Foo = new SomeEnum {}
final case class Bar[S <: SomeEnum](s: S) extends SomeEnum
object Bar:
def apply[S <: SomeEnum](s: S): SomeEnum = new Bar(s)

given LiftFoo as Liftable[Foo.type]:
def toExpr(x: Foo.type): QuoteContext ?=> Expr[Foo.type] = '{Foo}

given LiftBar[S <: SomeEnum: Type: Liftable] as Liftable[Bar[S]]:
def toExpr(x: Bar[S]): QuoteContext ?=> Expr[Bar[S]] = '{new Bar(${Lift(x.s)})}

sealed abstract class Lst[+T]
final case class CONS[+T](head: T, tail: Lst[T]) extends Lst[T]
case object NIL extends Lst[Nothing]

given IntLiftable[T <: Int] as Liftable[T]:
def toExpr(x: T): QuoteContext ?=> Expr[T] = qctx ?=> {
import qctx.tasty._
Literal(Constant(x)).seal.asInstanceOf[Expr[T]]
}

given LiftLst[T: Type: Liftable](using ev1: => Liftable[CONS[T]], ev2: => Liftable[NIL.type]) as Liftable[Lst[T]]:
def toExpr(xs: Lst[T]): QuoteContext ?=> Expr[Lst[T]] = xs match
case NIL => ev2.toExpr(NIL)
case cons @ CONS(_, _) => ev1.toExpr(cons)

given LiftCONS[T: Type: Liftable](using Liftable[Lst[T]]) as Liftable[CONS[T]]:
def toExpr(x: CONS[T]): QuoteContext ?=> Expr[CONS[T]] = '{CONS(${Lift(x.head)}, ${Lift(x.tail)})}

given LiftNIL as Liftable[NIL.type]:
def toExpr(x: NIL.type): QuoteContext ?=> Expr[NIL.type] = '{NIL}

def mkLst[T](ts: T*) = ts.foldRight(NIL: Lst[T])(CONS(_,_))

def quote123: QuoteContext ?=> Expr[Lst[Int]] = Lift(mkLst(1,2,3))

inline def get123: Lst[Int] = ${ quote123 }

2 changes: 2 additions & 0 deletions tests/run-macros/i9812b/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@main def Test: Unit = println(get123)