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

Conversation

nicolasstucki
Copy link
Contributor

No description provided.

@nicolasstucki nicolasstucki self-assigned this Sep 17, 2020
@nicolasstucki nicolasstucki linked an issue Sep 17, 2020 that may be closed by this pull request
Copy link
Member

@bishabosha bishabosha left a comment

Choose a reason for hiding this comment

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

Looks good to me, thank you

@bishabosha
Copy link
Member

bishabosha commented Sep 17, 2020

test could be useful for the liftable fixes:

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 }

// new file
// @main def Test: Unit = println(get123)

@nicolasstucki nicolasstucki marked this pull request as ready for review September 17, 2020 11:31
@bishabosha bishabosha merged commit 89db0f8 into scala:master Sep 17, 2020
@bishabosha bishabosha deleted the fix-#9812 branch September 17, 2020 12:52
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

All quotes widen singleton types
3 participants