From 7cfe8784e2b325c192a2ca5316e868a3e4f04f8c Mon Sep 17 00:00:00 2001 From: NTPape <10488949+NTPape@users.noreply.github.com> Date: Mon, 21 Feb 2022 00:21:36 +0100 Subject: [PATCH] Fix GenLens issue with evidence shadowing Will (mostly) solve #1259 for Scala 2.13. --- .../monocle/macros/internal/Macro.scala | 5 ++-- .../ContextBoundCompilationIssueSpec.scala | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 macro/src/test/scala-2.x/monocle/macros/internal/ContextBoundCompilationIssueSpec.scala diff --git a/macro/src/main/scala-2.x/monocle/macros/internal/Macro.scala b/macro/src/main/scala-2.x/monocle/macros/internal/Macro.scala index d16c1ef43..6908cb9c3 100644 --- a/macro/src/main/scala-2.x/monocle/macros/internal/Macro.scala +++ b/macro/src/main/scala-2.x/monocle/macros/internal/Macro.scala @@ -85,7 +85,8 @@ private[macros] class MacroImpl(val c: blackbox.Context) { .find(_.name.decodedName.toString == strFieldName) .getOrElse(c.abort(c.enclosingPosition, s"Cannot find constructor field named $fieldName in $sTpe")) - val F = TypeName(c.freshName("F")) + val F = TypeName(c.freshName("F")) + val ev = TermName(c.freshName("evFunctor")) c.Expr[PLens[S, T, A, B]](q""" import _root_.scala.language.higherKinds // prevent warning at call site @@ -97,7 +98,7 @@ private[macros] class MacroImpl(val c: blackbox.Context) { override def replace(a: $bTpe): $sTpe => $tTpe = _.copy($field = a) - override def modifyF[$F[_]: _root_.cats.Functor](f: $aTpe => $F[$bTpe])(s: $sTpe): $F[$tTpe] = + override def modifyF[$F[_]](f: $aTpe => $F[$bTpe])(s: $sTpe)(implicit $ev: _root_.cats.Functor[$F]): $F[$tTpe] = _root_.cats.Functor[$F].map(f(s.$fieldMethod))(a => s.copy($field = a)) override def modify(f: $aTpe => $bTpe): $sTpe => $tTpe = diff --git a/macro/src/test/scala-2.x/monocle/macros/internal/ContextBoundCompilationIssueSpec.scala b/macro/src/test/scala-2.x/monocle/macros/internal/ContextBoundCompilationIssueSpec.scala new file mode 100644 index 000000000..d0a1bf3f3 --- /dev/null +++ b/macro/src/test/scala-2.x/monocle/macros/internal/ContextBoundCompilationIssueSpec.scala @@ -0,0 +1,29 @@ +package monocle.macros.internal + +import monocle.Lens +import monocle.macros.GenLens +import munit.DisciplineSuite + +class ContextBoundCompilationIssueSpec extends DisciplineSuite { + + private trait Foo[T] + private trait Bar[T] + + private case class A[T: Foo](s: A.S[T]) { + val lens: Lens[A.S[T], Bar[T]] = GenLens[A.S[T]](_.bar) + } + + private object A { + case class S[T: Foo](bar: Bar[T]) + } + + private case object FooImpl extends Foo[Unit] + private case object BarImpl extends Bar[Unit] + + private val a: A[Unit] = A(A.S(BarImpl)(FooImpl))(FooImpl) + + test("context.bound.compilation") { + assertEquals(a.lens.get(a.s), BarImpl) + } + +}