Skip to content

Fix #1540: overloaded get and isDefined in option-less patmat #1597

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

Merged
merged 2 commits into from
Oct 14, 2016
Merged
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
22 changes: 13 additions & 9 deletions src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,21 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {
val isDefined = extractorMemberType(prev.tpe, nme.isDefined)

if ((isDefined isRef defn.BooleanClass) && getTp.exists) {
val tmpSym = freshSym(prev.pos, prev.tpe, "o")
val prevValue = ref(tmpSym).select("get".toTermName).ensureApplied
// isDefined and get may be overloaded
val getDenot = prev.tpe.member(nme.get).suchThat(_.info.isParameterless)
val isDefinedDenot = prev.tpe.member(nme.isDefined).suchThat(_.info.isParameterless)

Block(
List(ValDef(tmpSym, prev)),
// must be isEmpty and get as we don't control the target of the call (prev is an extractor call)
ifThenElseZero(
ref(tmpSym).select(nme.isDefined),
Block(List(ValDef(b.asTerm, prevValue)), next)
)
val tmpSym = freshSym(prev.pos, prev.tpe, "o")
val prevValue = ref(tmpSym).select(getDenot.symbol).ensureApplied

Block(
List(ValDef(tmpSym, prev)),
// must be isEmpty and get as we don't control the target of the call (prev is an extractor call)
ifThenElseZero(
ref(tmpSym).select(isDefinedDenot.symbol),
Block(List(ValDef(b.asTerm, prevValue)), next)
)
)
} else {
assert(defn.isProductSubType(prev.tpe))
val nullCheck: Tree = prev.select(defn.Object_ne).appliedTo(Literal(Constant(null)))
Expand Down
14 changes: 14 additions & 0 deletions tests/pos/i1540.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Casey1(val a: Int) {
def isDefined: Boolean = true
def isDefined(x: Int): Boolean = ???
def get: Int = a
def get(x: Int): String = ???
}
object Casey1 { def unapply(a: Casey1) = a }

object Test {
def main(args: Array[String]): Unit = {
val c @ Casey1(x) = new Casey1(0)
assert(x == c.get)
}
}
14 changes: 14 additions & 0 deletions tests/pos/i1540b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Casey1[T](val a: T) {
def isDefined: Boolean = true
def isDefined(x: T): Boolean = ???
def get: T = a
def get(x: T): String = ???
}
object Casey1 { def unapply[T](a: Casey1[T]) = a }

object Test {
def main(args: Array[String]): Unit = {
val c @ Casey1(x) = new Casey1(0)
assert(x == c.get)
}
}