Skip to content
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

Issue with this in presence of self alias + & types #99

Merged
merged 7 commits into from
Sep 2, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ object QuicklensMacros {
def unsupportedShapeInfo(tree: Tree) =
s"Unsupported path element. Path must have shape: _.field1.field2.each.field3.(...), got: ${tree.show}"

def noSuchMember(term: Term, name: String) =
s"${term.tpe} has no member named $name"
def noSuchMember(tpeStr: String, name: String) =
s"$tpeStr has no member named $name"

def methodSupported(method: String) =
Seq("at", "each", "eachWhere", "eachRight", "eachLeft", "atOrElse", "index", "when").contains(method)
Expand Down Expand Up @@ -136,33 +136,65 @@ object QuicklensMacros {
case i: Ident if i.name.startsWith("_") =>
Seq.empty
case _ =>
report.throwError(unsupportedShapeInfo(focus.asTerm))
report.errorAndAbort(unsupportedShapeInfo(focus.asTerm))
}
}

def termMethodByNameUnsafe(term: Term, name: String): Symbol = {
term.tpe.widen.dealias.typeSymbol
.memberMethod(name)
extension (tpe: TypeRepr)
def poorMansLUB: TypeRepr = tpe match {
case AndType(l, r) if l <:< r => l
case AndType(l, r) if r <:< l => r
case _ => tpe
}
def widenAll: TypeRepr =
tpe.widen.dealias.poorMansLUB
def matchingTypeSymbol: Symbol = tpe.widenAll match {
case AndType(l, r) =>
val lSym = l.matchingTypeSymbol
if l.matchingTypeSymbol != Symbol.noSymbol then lSym else r.matchingTypeSymbol
case tpe if isProduct(tpe.typeSymbol) || isSum(tpe.typeSymbol) =>
tpe.typeSymbol
case tpe =>
Symbol.noSymbol
}

def symbolMethodByNameUnsafe(sym: Symbol, name: String): Symbol = {
sym
.methodMember(name)
.headOption
.getOrElse(report.errorAndAbort(noSuchMember(term, name)))
.getOrElse(report.errorAndAbort(noSuchMember(sym.name, name)))
}

def termMethodByNameUnsafe(term: Term, name: String): Symbol = {
symbolMethodByNameUnsafe(term.tpe.widenAll.typeSymbol, name)
}

def termAccessorMethodByNameUnsafe(term: Term, name: String): (Symbol, Int) = {
val caseParamNames = term.tpe.widen.dealias.typeSymbol.primaryConstructor.paramSymss.flatten.filter(_.isTerm).map(_.name)
val typeSymbol = term.tpe.widenAll.matchingTypeSymbol
val caseParamNames = typeSymbol.primaryConstructor.paramSymss.flatten.filter(_.isTerm).map(_.name)
val idx = caseParamNames.indexOf(name)
term.tpe.widen.dealias.typeSymbol.caseFields.find(_.name == name).getOrElse(report.errorAndAbort(noSuchMember(term, name)))
typeSymbol.caseFields.find(_.name == name).getOrElse(report.errorAndAbort(noSuchMember(term.tpe.show, name)))
-> (idx + 1)
}

def isProduct(sym: Symbol): Boolean = {
sym.flags.is(Flags.Case)
}

def isSum(sym: Symbol): Boolean = {
sym.flags.is(Flags.Enum) ||
(sym.flags.is(Flags.Sealed) && (sym.flags.is(Flags.Trait) || sym.flags.is(Flags.Abstract)))
}

def caseClassCopy(
owner: Symbol,
mod: Expr[A => A],
obj: Term,
fields: Seq[(PathSymbol.Field, Seq[PathTree])]
): Term = {
val objSymbol = obj.tpe.widen.dealias.typeSymbol
if objSymbol.flags.is(Flags.Case) then {
val copy = termMethodByNameUnsafe(obj, "copy")
val objSymbol = obj.tpe.widenAll.matchingTypeSymbol
if isProduct(objSymbol) then {
val copy = symbolMethodByNameUnsafe(objSymbol, "copy")
val argsMap: Map[Int, Term] = fields.map { (field, trees) =>
val (fieldMethod, idx) = termAccessorMethodByNameUnsafe(obj, field.name)
val resTerm: Term = trees.foldLeft[Term](Select(obj, fieldMethod)) { (term, tree) =>
Expand All @@ -172,36 +204,38 @@ object QuicklensMacros {
idx -> namedArg
}.toMap

val typeParams = obj.tpe.widenAll match {
case AppliedType(_, typeParams) => Some(typeParams)
case _ => None
}

val fieldsIdxs = 1.to(objSymbol.primaryConstructor.paramSymss.flatten.filter(_.isTerm).length)
val args = fieldsIdxs.map { i =>
val defaultMethod = obj.select(symbolMethodByNameUnsafe(objSymbol, "copy$default$" + i.toString))
argsMap.getOrElse(
i,
Select(obj, termMethodByNameUnsafe(obj, "copy$default$" + i.toString))
typeParams.fold(defaultMethod)(defaultMethod.appliedToTypes)
)
}.toList

obj.tpe.widen match {
typeParams match {
// if the object's type is parametrised, we need to call .copy with the same type parameters
case AppliedType(_, typeParams) => Apply(TypeApply(Select(obj, copy), typeParams.map(Inferred(_))), args)
case Some(typeParams) => Apply(TypeApply(Select(obj, copy), typeParams.map(Inferred(_))), args)
case _ => Apply(Select(obj, copy), args)
}
} else if objSymbol.flags.is(Flags.Enum) ||
(objSymbol.flags.is(Flags.Sealed) && (objSymbol.flags.is(Flags.Trait) || objSymbol.flags.is(Flags.Abstract)))
then {
// if the source is a sealed trait / sealed abstract class / enum, generating a if-then-else with a .copy for each child (implementing case class)
val cases = obj.tpe.widen.dealias.typeSymbol.children.map { child =>
val subtype = TypeIdent(child)
val bind = Symbol.newBind(owner, "c", Flags.EmptyFlags, subtype.tpe)
CaseDef(Bind(bind, Typed(Ref(bind), subtype)), None, caseClassCopy(owner, mod, Ref(bind), fields))
} else if isSum(objSymbol) then {
obj.tpe.widenAll match {
case AndType(_, _) =>
report.errorAndAbort(s"Implementation limitation: Cannot modify sealed hierarchies mixed with & types. Try providing a more specific type.")
case _ =>
}

/*
if (obj.isInstanceOf[Child1]) caseClassCopy(obj.asInstanceOf[Child1]) else
if (obj.isInstanceOf[Child2]) caseClassCopy(obj.asInstanceOf[Child2]) else
...
else throw new IllegalStateException()
*/
val ifThens = obj.tpe.widen.dealias.typeSymbol.children.map { child =>
val ifThens = objSymbol.children.map { child =>
val ifCond = TypeApply(Select.unique(obj, "isInstanceOf"), List(TypeIdent(child)))

val ifThen = ValDef.let(owner, TypeApply(Select.unique(obj, "asInstanceOf"), List(TypeIdent(child)))) {
Expand All @@ -217,7 +251,7 @@ object QuicklensMacros {
If(ifCond, ifThen, ifElse)
}
} else
report.throwError(s"Unsupported source object: must be a case class or sealed trait, but got: $objSymbol")
report.errorAndAbort(s"Unsupported source object: must be a case class or sealed trait, but got: $objSymbol")
}

def applyFunctionDelegate(
Expand Down Expand Up @@ -289,14 +323,14 @@ object QuicklensMacros {
case Block(List(DefDef(_, _, _, Some(p))), _) =>
toPath(p, focus)
case _ =>
report.throwError(unsupportedShapeInfo(tree))
report.errorAndAbort(unsupportedShapeInfo(tree))
}

val pathTree: PathTree =
paths.foldLeft(PathTree.empty) { (tree, path) => tree <> path }

val res: (Expr[A => A] => Expr[S]) = (mod: Expr[A => A]) =>
mapToCopy(Symbol.spliceOwner, mod, obj.asTerm, pathTree).asExpr.asInstanceOf[Expr[S]]
Typed(mapToCopy(Symbol.spliceOwner, mod, obj.asTerm, pathTree), TypeTree.of[S]).asExpr.asInstanceOf[Expr[S]]
to(res)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.softwaremill.quicklens

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import ModifyAndTypeTest._

object ModifyAndTypeTest {
case class A(a: Int) extends B
trait B {
def a: Int
}

case class A1(a: Int)

sealed trait T
case class C(a: Int) extends T with B

sealed trait T1
case class C1(a: Int) extends T1
}

class ModifyAndTypeTest extends AnyFlatSpec with Matchers {
it should "modify an & type object" in {
val ab: A & B = A(0)

val modified = ab.modify(_.a).setTo(1)

modified.a shouldBe 1
}

it should "modify an & type object 1" in {
val ab: B & A = A(0)

val modified = ab.modify(_.a).setTo(1)

modified.a shouldBe 1
}

it should "modify an & type object 2" in {
val ab: B & A1 = new A1(0) with B

val modified = ab.modify(_.a).setTo(1)

modified.a shouldBe 1
}

it should "modify an & type object 3" in {
val ab: A1 & B = new A1(0) with B

val modified = ab.modify(_.a).setTo(1)

modified.a shouldBe 1
}

// TODO this is an implemenation limitation for now, since anonymous classes crash on runtime
Copy link
Member

Choose a reason for hiding this comment

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

limitation of quicklens, Scala 3? If the latter, is there a dotty ticket maybe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

limitation of quicklens, Scala 3?

quicklens. I think.

I'm not sure if there should be a dotty ticket for it since we're using asInstanceOf.
Also, the problem occurs only for the last two tests - the ones with anonymous classes.
It's not hard to minimize, because we can just type the generated code by hand:

sealed trait T
case class A(a: Int) extends T
trait B {
  def a: Int
}

val ab: T & B = new A(0) with B

val res: T & B = // if we change the ascription to `A & B` it works
  if(ab.isInstanceOf[A]) then {
    ab.asInstanceOf[A].copy().asInstanceOf[A & B]
  } else {
    ???
  }

Scastie: https://scastie.scala-lang.org/KacperFKorban/s7A3euoTST2AoPYcTug8iA/5

Maybe there is some other way to write it so that it won't crash on runtime 🤔

The only weird thing is that it doesn't crash if we change the type ascription to the more specific one.

Copy link
Contributor

Choose a reason for hiding this comment

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

Note: when I rewrite this using with instead of ¨&` and try it with Scala 2.13, it works: https://scastie.scala-lang.org/0vAFV0B8SDmZrjvbRj3jsA

The same code with with still does not work in Scala 3.

To me this looks like a Scala 3 bug.

Copy link
Contributor

Choose a reason for hiding this comment

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

Reported in scala/scala3#15952

Copy link
Member

Choose a reason for hiding this comment

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

Ok so now that we know it's a quicklens not a dotty issue, can we do anything about it? :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't see an immediate solution. The problem is in the generated bytecode, so no matter what we do ab.asInstanceOf[A].copy() will have to be cast to T & B. Which is impossible.

Also if we want to have a discussion about it, maybe a new issue will be a better place for it 😃

Copy link
Member

Choose a reason for hiding this comment

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

Sure :) #100

// it should "modify an & type object with a sealed trait" in {
// val tb: T & B = C(0)

// val modified = tb.modify(_.a).setTo(1)

// modified.a shouldBe 1
// }

// it should "modify an & type object with a sealed trait 1" in {
// val tb: B & T = C(0)

// val modified = tb.modify(_.a).setTo(1)

// modified.a shouldBe 1
// }

// it should "modify an & type object with a sealed trait 2" in {
// val tb: B & T1 = new C1(0) with B

// val modified = tb.modify(_.a).setTo(1)

// modified.a shouldBe 1
// }

// it should "modify an & type object with a sealed trait 3" in {
// val tb: T1 & B = new C1(0) with B

// val modified = tb.modify(_.a).setTo(1)

// modified.a shouldBe 1
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.softwaremill.quicklens

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import ModifySelfThisTest._

object ModifySelfThisTest {

case class State(x: Int) { self =>

def mod: State = this.modify(_.x).setTo(1)
}

trait A {
def a: Unit
}

case class State1(x: Int) extends A { self: A =>

def mod: State1 = this.modify(_.x).setTo(1)

def a: Unit = ()
}
}

class ModifySelfThisTest extends AnyFlatSpec with Matchers {
it should "modify an object even in presence of self alias" in {
val s = State(0)
val modified = s.mod

modified.x shouldBe 1
}

it should "modify an object even in presence of self type" in {
val s = State(0)
val modified = s.mod

modified.x shouldBe 1
}
}