From d49b2937921610f996941b89ef63d9b730a85e63 Mon Sep 17 00:00:00 2001 From: Anatolii Date: Fri, 16 Aug 2019 14:41:24 +0200 Subject: [PATCH 1/3] Fix #7046: Literal types don't work with quotes --- compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala | 7 ++----- tests/pos/i7046.scala | 7 +++++++ 2 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 tests/pos/i7046.scala diff --git a/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala b/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala index d042966bdc30..8d97b0d68430 100644 --- a/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala +++ b/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala @@ -230,7 +230,7 @@ class ReifyQuotes extends MacroTransform { def pickleAsTasty() = { val meth = if (isType) ref(defn.Unpickler_unpickleType).appliedToType(originalTp) - else ref(defn.Unpickler_unpickleExpr).appliedToType(originalTp.widen) + else ref(defn.Unpickler_unpickleExpr).appliedToType(originalTp) val spliceResType = if (isType) defn.QuotedTypeClass.typeRef.appliedTo(WildcardType) else defn.FunctionType(1, isContextual = true).appliedTo(defn.QuoteContextClass.typeRef, defn.QuotedExprClass.typeRef.appliedTo(defn.AnyType)) | defn.QuotedTypeClass.typeRef.appliedTo(WildcardType) @@ -244,10 +244,7 @@ class ReifyQuotes extends MacroTransform { if (splices.isEmpty && body.symbol.isPrimitiveValueClass) tag(s"${body.symbol.name}Tag") else pickleAsTasty().select(nme.apply).appliedTo(qctx) } - else toValue(body) match { - case Some(value) => pickleAsValue(value) - case _ => pickleAsTasty() - } + else pickleAsTasty() } /** If inside a quote, split the body of the splice into a core and a list of embedded quotes diff --git a/tests/pos/i7046.scala b/tests/pos/i7046.scala new file mode 100644 index 000000000000..7ebb76479ac5 --- /dev/null +++ b/tests/pos/i7046.scala @@ -0,0 +1,7 @@ +import scala.quoted._ + +inline def mcr: Any = ${mcrImpl} +def mcrImpl given (ctx: QuoteContext): Expr[Any] = { + val tpl: Expr[1] = '{1} + '{()} +} From 0c7dac7c9acc666e58cb2e377e917c6c07891d8d Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 19 Aug 2019 10:32:07 +0200 Subject: [PATCH 2/3] Revert changes in ReifyQuotes --- compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala b/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala index 8d97b0d68430..d042966bdc30 100644 --- a/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala +++ b/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala @@ -230,7 +230,7 @@ class ReifyQuotes extends MacroTransform { def pickleAsTasty() = { val meth = if (isType) ref(defn.Unpickler_unpickleType).appliedToType(originalTp) - else ref(defn.Unpickler_unpickleExpr).appliedToType(originalTp) + else ref(defn.Unpickler_unpickleExpr).appliedToType(originalTp.widen) val spliceResType = if (isType) defn.QuotedTypeClass.typeRef.appliedTo(WildcardType) else defn.FunctionType(1, isContextual = true).appliedTo(defn.QuoteContextClass.typeRef, defn.QuotedExprClass.typeRef.appliedTo(defn.AnyType)) | defn.QuotedTypeClass.typeRef.appliedTo(WildcardType) @@ -244,7 +244,10 @@ class ReifyQuotes extends MacroTransform { if (splices.isEmpty && body.symbol.isPrimitiveValueClass) tag(s"${body.symbol.name}Tag") else pickleAsTasty().select(nme.apply).appliedTo(qctx) } - else pickleAsTasty() + else toValue(body) match { + case Some(value) => pickleAsValue(value) + case _ => pickleAsTasty() + } } /** If inside a quote, split the body of the splice into a core and a list of embedded quotes From 1c3029d903961dcbf62a48b83064fe7177bcd794 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 19 Aug 2019 10:43:39 +0200 Subject: [PATCH 3/3] Fix #7046: Support lifting for literal constant types --- .../tools/dotc/transform/ReifyQuotes.scala | 4 +++- .../scala/quoted/Liftable.scala | 18 +++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala b/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala index d042966bdc30..89a75e6dcac7 100644 --- a/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala +++ b/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala @@ -209,7 +209,9 @@ class ReifyQuotes extends MacroTransform { } def liftedValue[T](value: T, name: TermName) = - ref(defn.LiftableModule).select(name).select("toExpr".toTermName).appliedTo(Literal(Constant(value))) + ref(defn.LiftableModule) + .select(name).appliedToType(originalTp) + .select("toExpr".toTermName).appliedTo(Literal(Constant(value))) def pickleAsValue[T](value: T) = { value match { diff --git a/library/src-bootstrapped/scala/quoted/Liftable.scala b/library/src-bootstrapped/scala/quoted/Liftable.scala index 0c9595c1521c..e48576738c08 100644 --- a/library/src-bootstrapped/scala/quoted/Liftable.scala +++ b/library/src-bootstrapped/scala/quoted/Liftable.scala @@ -19,15 +19,15 @@ trait Liftable[T] { */ object Liftable { - given Liftable_Boolean_delegate as Liftable[Boolean] = new PrimitiveLiftable - given Liftable_Byte_delegate as Liftable[Byte] = new PrimitiveLiftable - given Liftable_Short_delegate as Liftable[Short] = new PrimitiveLiftable - given Liftable_Int_delegate as Liftable[Int] = new PrimitiveLiftable - given Liftable_Long_delegate as Liftable[Long] = new PrimitiveLiftable - given Liftable_Float_delegate as Liftable[Float] = new PrimitiveLiftable - given Liftable_Double_delegate as Liftable[Double] = new PrimitiveLiftable - given Liftable_Char_delegate as Liftable[Char] = new PrimitiveLiftable - given Liftable_String_delegate as Liftable[String] = new PrimitiveLiftable + given Liftable_Boolean_delegate[T <: Boolean] as Liftable[T] = new PrimitiveLiftable + given Liftable_Byte_delegate[T <: Byte] as Liftable[T] = new PrimitiveLiftable + given Liftable_Short_delegate[T <: Short] as Liftable[T] = new PrimitiveLiftable + given Liftable_Int_delegate[T <: Int] as Liftable[T] = new PrimitiveLiftable + given Liftable_Long_delegate[T <: Long] as Liftable[T] = new PrimitiveLiftable + given Liftable_Float_delegate[T <: Float] as Liftable[T] = new PrimitiveLiftable + given Liftable_Double_delegate[T <: Double] as Liftable[T] = new PrimitiveLiftable + given Liftable_Char_delegate[T <: Char] as Liftable[T] = new PrimitiveLiftable + given Liftable_String_delegate[T <: String] as Liftable[T] = new PrimitiveLiftable private class PrimitiveLiftable[T <: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String] extends Liftable[T] { /** Lift a primitive value `n` into `'{ n }` */