Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Teach typedConstructorPattern to ignore non-redefining unapplies
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
Showing
3 changed files
with
50 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// scalac: -Werror | ||
|
||
sealed case class Sub1(str: String) | ||
final case class Sup1(str: String) extends Sup0 | ||
|
||
final class Sub2 extends Sub1("") | ||
sealed trait Sup0 { def str: String } | ||
|
||
// both of these unapplies are overloads of the synthetic unapply | ||
// i.e. it isn't suppressed | ||
object Sub1 { def unapply(x: Sub2): Some[String] = Some(x.str) } | ||
object Sup1 { def unapply(x: Sup0): Some[String] = Some(x.str) } | ||
|
||
object Test { | ||
// these seek the original unapplies and should be converted to use their constructors | ||
def testSub1(x: Sub1) = x match { case Sub1(str) => str } | ||
def testSup1(x: Sup1) = x match { case Sup1(str) => str } | ||
|
||
// these seek the user-defined alternative unapplies | ||
// thus they shouldn't accidentally be converted to use their constructors | ||
def testSub2(x: Sub2) = x match { case Sub1(str) => str } | ||
def testSup0(x: Sup0) = x match { case Sup1(str) => str } | ||
} |