Skip to content

Commit efb74b7

Browse files
authored
Merge pull request scala#5301 from retronym/ticket/SD-167
SD-167 Fine tuning constructor pattern translation Fixes scala/scala-dev#167
2 parents 4f749ce + a2cba53 commit efb74b7

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

src/compiler/scala/tools/nsc/typechecker/Namers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,7 @@ trait Namers extends MethodSynthesis {
17641764
* bugs waiting to be reported? If not, why not? When exactly do we need to
17651765
* call this method?
17661766
*/
1767-
def companionSymbolOf(original: Symbol, ctx: Context): Symbol = {
1767+
def companionSymbolOf(original: Symbol, ctx: Context): Symbol = if (original == NoSymbol) NoSymbol else {
17681768
val owner = original.owner
17691769
// SI-7264 Force the info of owners from previous compilation runs.
17701770
// Doing this generally would trigger cycles; that's what we also

src/compiler/scala/tools/nsc/typechecker/PatternTypers.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ trait PatternTypers {
7979
// do not update the symbol if the tree's symbol's type does not define an unapply member
8080
// (e.g. since it's some method that returns an object with an unapply member)
8181
val fun = inPlaceAdHocOverloadingResolution(fun0)(hasUnapplyMember)
82+
val canElide = treeInfo.isQualifierSafeToElide(fun)
8283
val caseClass = companionSymbolOf(fun.tpe.typeSymbol.sourceModule, context)
8384
val member = unapplyMember(fun.tpe)
8485
def resultType = (fun.tpe memberType member).finalResultType
@@ -94,7 +95,7 @@ trait PatternTypers {
9495
// Dueling test cases: pos/overloaded-unapply.scala, run/case-class-23.scala, pos/t5022.scala
9596
// A case class with 23+ params has no unapply method.
9697
// A case class constructor may be overloaded with unapply methods in the companion.
97-
if (caseClass.isCase && !member.isOverloaded)
98+
if (canElide && caseClass.isCase && !member.isOverloaded)
9899
logResult(s"convertToCaseConstructor($fun, $caseClass, pt=$pt)")(convertToCaseConstructor(fun, caseClass, pt))
99100
else if (!reallyExists(member))
100101
CaseClassConstructorError(fun, s"${fun.symbol} is not a case class, nor does it have an unapply/unapplySeq member")

test/files/run/sd167.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x

test/files/run/sd167.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object Test {
2+
implicit class ToExtractor(val s: StringContext) {
3+
def x = {println("x"); Some }
4+
}
5+
def main(args: Array[String]) {
6+
Some(1) match { case x"${a}" => } // used to convert to `case Some(a) =>` and omit side effects
7+
}
8+
}

0 commit comments

Comments
 (0)