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

Add test for #258, improve error message #261

Closed
Closed
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 @@ -58,6 +58,9 @@ object QuicklensMacros {
def noSuchMember(tpeStr: String, name: String) =
s"$tpeStr has no member named $name"

def noSuitableMember(tpeStr: String, name: String, argNames: Iterable[String]) =
s"$tpeStr has no member $name with parameters ${argNames.mkString("(", ", ", ")")}"

def multipleMatchingMethods(tpeStr: String, name: String, syms: Seq[Symbol]) =
val symsStr = syms.map(s => s" - $s: ${s.termRef.dealias.widen.show}").mkString("\n", "\n", "")
s"Multiple methods named $name found in $tpeStr: $symsStr"
Expand Down Expand Up @@ -181,13 +184,16 @@ object QuicklensMacros {

def methodSymbolByNameAndArgsOrError(sym: Symbol, name: String, argsMap: Map[String, Term]): Symbol = {
val argNames = argsMap.keys
sym.methodMember(name).filter{ msym =>
val allCopyMembers = sym.methodMember(name)
allCopyMembers.filter{ msym =>
// for copy, we filter out the methods that don't have the desired parameter names
val paramNames = msym.paramSymss.flatten.filter(_.isTerm).map(_.name)
argNames.forall(paramNames.contains)
} match
case List(m) => m
case Nil => report.errorAndAbort(noSuchMember(sym.name, name))
case Nil =>
if allCopyMembers.isEmpty then report.errorAndAbort(noSuchMember(sym.name, name))
else report.errorAndAbort(noSuitableMember(sym.name, name, argNames))
case lst @ (m :: _) =>
// if we have multiple matching copy methods, pick the synthetic one, if it exists, otherwise, pick any method
val syntheticCopies = lst.filter(_.flags.is(Flags.Synthetic))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ class ExplicitCopyTest extends AnyFlatSpec with Matchers {
accessed shouldEqual 0
}

it should "not compile when modifying a field which is not present as a copy parameter" in {
"""
case class Content(x: String)

class A(val c: Content) {
def copy(x: String = c.x): A = new A(Content(x))
}

val a = new A(Content("A"))
val am = a.modify(_.c).setTo(Content("B"))
""" shouldNot compile
}

// TODO: Would be nice to be able to handle this case. Based on the types, it
// is obvious, that the explicit copy should be picked, but I'm not sure if we
// can get that information
Expand Down