Skip to content

Commit

Permalink
Teach typedConstructorPattern to ignore non-redefining unapplies
Browse files Browse the repository at this point in the history
In pos/t12250.scala Foo defines an unapply which doesn't redefine the
synthetic one.  Thus unapply is overloaded.  In the pattern being typed,
the synthetic one is indeed the one seeked, so despite being overloaded,
we should convertToCaseConstructor.
  • Loading branch information
dwijnand committed Jan 29, 2021
1 parent e9b1546 commit fc34bf2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/compiler/scala/tools/nsc/typechecker/PatternTypers.scala
Expand Up @@ -97,9 +97,9 @@ trait PatternTypers {
// Dueling test cases: pos/overloaded-unapply.scala, run/case-class-23.scala, pos/t5022.scala
// A case class with 23+ params has no unapply method.
// A case class constructor may be overloaded with unapply methods in the companion.
// A case class maybe have its own custom unapply (so non-synthetic) scala/bug#11252
// Unapply methods aren't `isCaseApplyOrUnapply` in Scala 3 tasty/run/src-2/tastytest/TestColour.scala
else if (canElide && caseClass.isCase && !member.isOverloaded && (member == NoSymbol || member.isSynthetic))
// A case class may have its own custom unapply (so non-synthetic) pos/t11252.scala
// A case class may have its own custom unapply unrelated to deconstructing pos/t12250.scala
else if (canElide && caseClass.isCase && (!member.isOverloaded && (member == NoSymbol || member.isSynthetic) || member.alternatives.exists(_.isSynthetic) && caseClass.tpe =:= pt))
logResult(s"convertToCaseConstructor($fun, $caseClass, pt=$pt)")(convertToCaseConstructor(fun, caseClass, pt))
else if (!reallyExists(member))
CaseClassConstructorError(fun, s"${fun.symbol} is not a case class, nor does it have a valid unapply/unapplySeq member")
Expand Down
11 changes: 11 additions & 0 deletions test/files/pos/t12250.scala
@@ -0,0 +1,11 @@
// scalac: -Werror
final case class Foo(value: String)

object Foo {
def unapply(str: String): Option[Foo] = Some(Foo(str))

def extract(id: Foo): String =
id match {
case Foo(a) => a
}
}

0 comments on commit fc34bf2

Please sign in to comment.