Skip to content

Fix #2156 #2158

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 6 commits into from
Mar 31, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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
17 changes: 6 additions & 11 deletions compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -848,26 +848,21 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {

val nextBinder = afterTest.asTerm

def needsOuterTest(patType: Type, selType: Type, currentOwner: Symbol): Boolean = {
def outerTestNeeded(implicit ctx: Context): Boolean = {
// See the test for SI-7214 for motivation for dealias. Later `treeCondStrategy#outerTest`
// generates an outer test based on `patType.prefix` with automatically dealises.
patType.dealias match {
case tref @ TypeRef(pre, name) =>
(pre ne NoPrefix) && tref.symbol.isClass &&
ExplicitOuter.needsOuterIfReferenced(tref.symbol.asClass)
expectedTp.dealias match {
case tref @ TypeRef(pre: SingletonType, name) =>
val s = tref
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the added definition of s. Just use tref!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variables defined in pattern matches are rarely debuggable when compiled by scalac.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, if you prefer.

s.symbol.isClass &&
ExplicitOuter.needsOuterIfReferenced(s.symbol.asClass)
case _ =>
false
}
}

override lazy val introducedRebindings = NoRebindings

def outerTestNeeded = {
val np = expectedTp.normalizedPrefix
val ts = np.termSymbol
(ts ne NoSymbol) && needsOuterTest(expectedTp, testedBinder.info, ctx.owner)
}

// the logic to generate the run-time test that follows from the fact that
// a `prevBinder` is expected to have type `expectedTp`
// the actual tree-generation logic is factored out, since the analyses generate Cond(ition)s rather than Trees
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/io/ClassPath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ abstract class ClassPath {
case Some((pkg, rest)) =>
val rep = packages find (_.name == pkg) flatMap (_ findClass rest)
rep map {
case x: ClassRep => x
case x => throw new FatalError("Unexpected ClassRep '%s' found searching for name '%s'".format(x, name))
case x: AnyClassRep => x
case x => throw new FatalError("Unexpected ClassRep '%s' found searching for name '%s'".format(x, name))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rep is an AnyClassRep so this case will never happen. I believe the original code intended to check the outer pointer, though I have no idea why, here's the commit that introduced the check: scala/scala@a1a6ab9

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code compiled by scalac doesn't contain the check:

option = rep.map((Function1)new Serializable(this, name){
                public static final long serialVersionUID = 0;
                private final String name$2;

                public final ClassRep apply(ClassRep x0$1) {
                    ClassRep classRep = x0$1;
                    if (classRep instanceof ClassRep) {
                        ClassRep classRep2;
                        ClassRep classRep3 = classRep2 = classRep;
                        return classRep3;
                    }
                    throw new dotty.tools.FatalError(new scala.collection.immutable.StringOps(Predef..MODULE$.augmentString("Unexpected ClassRep '%s' found searching for name '%s'")).format((Seq)Predef..MODULE$.genericWrapArray((Object)new Object[]{classRep, this.name$2})));
                }

                public final /* bridge */ /* synthetic */ Object apply(Object v1) {
                    return this.apply((ClassRep)v1);
                }
            });

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not saying that the check was actually performed, I'm saying that the code was written assuming that the check would be performed, which apparently it wasn't.

Copy link
Contributor

@odersky odersky Mar 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment on the ticket #2156. I think it's a bug in scalac. By guess is that it checks whether the test would be redundant (which is reasonable) but gets that logic messed up. For instance, here is a situation where the test should be omitted:

class Outer {
  trait Base
  class Inner extends Base

  val x: Base = ???
  x match {
    case x: Inner => ???
  }
}

}
case _ =>
classes find (_.name == name)
Expand All @@ -256,6 +256,7 @@ abstract class ClassPath {
}

def sortString = join(split(asClasspathString).sorted: _*)

override def equals(that: Any) = that match {
case x: ClassPath => this.sortString == x.sortString
case _ => false
Expand Down
37 changes: 37 additions & 0 deletions tests/run/i2156.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Outer {

case class Inner()

val inner: Inner = new Inner

def checkInstance(o: Outer) =
o.inner.isInstanceOf[this.Inner]

def checkPattern1(i: Any) =
i match {
case _: Inner => true
case _ => false
}

def checkPattern2(i: Any) =
i match {
case Inner() => true
case _ => false
}

def checkEquals(o: Outer) =
o.inner == inner
}

object Test {

def main(args: Array[String]) = {
val o1 = new Outer
val o2 = new Outer
assert(o1.checkInstance(o2)) // ok
assert(!o1.checkPattern1(o2.inner)) // ok under scalac, fails for dotc-compiled code
assert(!o1.checkPattern2(o2.inner)) // ok under scalac, fails for dotc-compiled code
assert(!o1.checkEquals(o2)) // ok under scalac, fails for dotc-compiled code
}
}