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
More accurate outer checks in patterns #9504
Merged
dwijnand
merged 1 commit into
scala:2.13.x
from
dwijnand:2.13/more-accurate-outer-checks-in-patterns
Apr 20, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -347,9 +347,6 @@ trait MatchTreeMaking extends MatchCodeGen with Debugging { | |
def eqTest(pat: Tree, testedBinder: Symbol) = REF(testedBinder) OBJ_EQ pat | ||
|
||
override def withOuterTest(orig: Tree)(testedBinder: Symbol, expectedTp: Type): Tree = { | ||
val expectedPrefix = expectedTp.prefix | ||
val testedPrefix = testedBinder.info.prefix | ||
|
||
// Check if a type is defined in a static location. Unlike `tp.isStatic` before `flatten`, | ||
// this also includes methods and (possibly nested) objects inside of methods. | ||
def definedInStaticLocation(tp: Type): Boolean = { | ||
|
@@ -361,20 +358,81 @@ trait MatchTreeMaking extends MatchCodeGen with Debugging { | |
tp.typeSymbol.owner == tp.prefix.typeSymbol && isStatic(tp.prefix) | ||
} | ||
|
||
if ((expectedPrefix eq NoPrefix) | ||
|| expectedTp.typeSymbol.isJava | ||
|| definedInStaticLocation(expectedTp) | ||
|| testedPrefix =:= expectedPrefix) orig | ||
else gen.mkAttributedQualifierIfPossible(expectedPrefix) match { | ||
case None => orig | ||
case Some(expectedOuterRef) => | ||
// ExplicitOuter replaces `Select(q, outerSym) OBJ_EQ expectedPrefix` | ||
// by `Select(q, outerAccessor(outerSym.owner)) OBJ_EQ expectedPrefix` | ||
// if there's an outer accessor, otherwise the condition becomes `true` | ||
// TODO: centralize logic whether there's an outer accessor and use here? | ||
val synthOuterGetter = expectedTp.typeSymbol.newMethod(nme.OUTER_SYNTH, newFlags = SYNTHETIC | ARTIFACT) setInfo expectedPrefix | ||
val outerTest = (Select(codegen._asInstanceOf(testedBinder, expectedTp), synthOuterGetter)) OBJ_EQ expectedOuterRef | ||
and(orig, outerTest) | ||
// In `def foo(a: b.B) = a match { case _: p.P }` | ||
// testedBinder.symbol.info = b.B | ||
// expectedTp = p.P | ||
|
||
expectedTp.dealias match { | ||
case RefinedType(Nil, _) => orig | ||
case rt@RefinedType(parent :: rest, scope) => | ||
// If the pattern type is refined type, emit outer tests for each component. | ||
withOuterTest(withOuterTest(orig)(testedBinder, parent))(testedBinder, copyRefinedType(rt, rest, scope)) | ||
case expectedTp => | ||
val expectedClass = expectedTp.typeSymbol | ||
assert(!expectedClass.isRefinementClass, orig) | ||
// .typeSymbol dealiases, so look at the prefix of the base type at the dealiased symbol, | ||
// not of expectedTp itself. | ||
val expectedPrefix = expectedTp.baseType(expectedClass).prefix | ||
|
||
|
||
// Given `(a: x.B) match { case _: x.P }` where P is subclass of B, is it possible | ||
// that a value conforms to both x.B and x1.P where `x ne x1`? | ||
// | ||
// To answer this, we create a new prefix based on a fresh symbol and check the | ||
// base type of TypeRef(freshPrefix, typePatternSymbol (P), args) at the binder | ||
// symbol (B). If that is prefixed by the fresh symbol, they are statically the | ||
// same. | ||
// | ||
// It is not sufficient to show that x.P is a subtype of x.B, as this | ||
// would incorrectly elide the outer test in: | ||
// | ||
// class P extends p1.B | ||
// def test(b: p1.B) = b match { case _: p1.P } | ||
// test(new p2.P) | ||
def prefixAligns: Boolean = { | ||
expectedTp match { | ||
case TypeRef(pre, _, _) if !pre.isStable => // e.g. _: Outer#Inner | ||
false | ||
case TypeRef(pre, sym, args) => | ||
val testedBinderClass = testedBinder.info.upperBound.typeSymbol | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also works. Maybe slightly clearer than diff --git a/src/compiler/scala/tools/nsc/transform/patmat/MatchTreeMaking.scala b/src/compiler/scala/tools/nsc/transform/patmat/MatchTreeMaking.scala
index c62510e5b2..894fcd834a 100644
--- a/src/compiler/scala/tools/nsc/transform/patmat/MatchTreeMaking.scala
+++ b/src/compiler/scala/tools/nsc/transform/patmat/MatchTreeMaking.scala
@@ -411,7 +411,7 @@ trait MatchTreeMaking extends MatchCodeGen with Debugging {
case TypeRef(pre, _, _) if !pre.isStable => // e.g. _: Outer#Inner
false
case TypeRef(pre, sym, args) =>
- val testedBinderClass = testedBinder.info.upperBound.typeSymbol
+ val testedBinderClass = testedBinder.info.baseClasses.find(_.isClass).getOrElse(NoSymbol)
val testedBinderType = testedBinder.info.baseType(testedBinderClass)
val testedPrefixIsExpectedTypePrefix = pre =:= testedBinderType.prefix |
||
val testedBinderType = testedBinder.info.baseType(testedBinderClass) | ||
|
||
val testedPrefixIsExpectedTypePrefix = pre =:= testedBinderType.prefix | ||
val testedPrefixAndExpectedPrefixAreStaticallyIdentical: Boolean = { | ||
val freshPrefix = pre match { | ||
case ThisType(thissym) => | ||
ThisType(thissym.cloneSymbol(thissym.owner)) | ||
case _ => | ||
val preSym = pre.termSymbol | ||
val freshPreSym = preSym.cloneSymbol(preSym.owner).setInfo(preSym.info) | ||
singleType(pre.prefix, freshPreSym) | ||
} | ||
val expectedTpFromFreshPrefix = TypeRef(freshPrefix, sym, args) | ||
val baseTypeFromFreshPrefix = expectedTpFromFreshPrefix.baseType(testedBinderClass) | ||
freshPrefix eq baseTypeFromFreshPrefix.prefix | ||
} | ||
testedPrefixAndExpectedPrefixAreStaticallyIdentical && testedPrefixIsExpectedTypePrefix | ||
case _ => | ||
false | ||
} | ||
} | ||
|
||
if ((expectedPrefix eq NoPrefix) | ||
|| expectedTp.typeSymbol.isJava | ||
|| definedInStaticLocation(expectedTp) | ||
|| testedBinder.info <:< expectedTp | ||
|| prefixAligns) orig | ||
else gen.mkAttributedQualifierIfPossible(expectedPrefix) match { | ||
case None => orig | ||
case Some(expectedOuterRef) => | ||
// ExplicitOuter replaces `Select(q, outerSym) OBJ_EQ expectedPrefix` | ||
// by `Select(q, outerAccessor(outerSym.owner)) OBJ_EQ expectedPrefix` | ||
// if there's an outer accessor, otherwise the condition becomes `true` | ||
// TODO: centralize logic whether there's an outer accessor and use here? | ||
val synthOuterGetter = expectedTp.typeSymbol.newMethod(nme.OUTER_SYNTH, newFlags = SYNTHETIC | ARTIFACT) setInfo expectedPrefix | ||
val outerTest = (Select(codegen._asInstanceOf(testedBinder, expectedTp), synthOuterGetter)) OBJ_EQ expectedOuterRef | ||
and(orig, outerTest) | ||
} | ||
} | ||
} | ||
} | ||
|
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,8 @@ | ||
// scalac: -Werror | ||
object Test1 { | ||
val g: scala.tools.nsc.Global = ??? | ||
import g._ | ||
def test(sym: Symbol) = sym.name match { | ||
case _: TermName => | ||
dwijnand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
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,24 @@ | ||
object Test { | ||
case class O(i: Int) { | ||
class A | ||
class B extends A { | ||
def bOuter = O.this | ||
} | ||
trait C { | ||
def cOuter = O.this | ||
} | ||
class D extends o2.B with C | ||
} | ||
val o1 = new O(1); | ||
val o2 = new O(2); | ||
def pat1(a: Test.o1.C) = a match { | ||
case b: Test.o1.B => | ||
assert(b.bOuter eq Test.o1, | ||
s"expected ${o1} as outer of value conforming to pattern `b: Test.o1.B`, but got ${b.bOuter}") | ||
case _ => | ||
|
||
} | ||
def main(args: Array[String]): Unit = { | ||
pat1(new o1.D) | ||
} | ||
} |
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,117 @@ | ||
// scalac: -unchecked | ||
import scala.util.Try | ||
|
||
object Test { | ||
class O(val i: Int) { | ||
class A { | ||
val aOuter = i | ||
} | ||
|
||
class B1 extends A { | ||
val b1Outer = i | ||
} | ||
} | ||
class M(i: Int) extends O(i) { | ||
class B2 extends m2.A { | ||
val b2Outer = i | ||
} | ||
|
||
def pat1(a: M.this.A) = a match { | ||
case b: M.this.B1 => // can elide outer check, (a : m1.A) && (a : O#B1) implies (a : m1.B1) | ||
assertOuter(m1.i, b.b1Outer) | ||
true | ||
case _ => | ||
false | ||
} | ||
def pat2(a: m2.A) = a match { | ||
case b: M.this.B2 => // needs runtime outer check | ||
assertOuter(m1.i, b.b2Outer) | ||
true | ||
case _ => | ||
false | ||
} | ||
def pat3(a: M.this.B1) = a match { | ||
case b: M.this.A => // can elide outer check, (a : m1.B1) && (a : O#A) implies (a : m1.B1) | ||
assertOuter(m1.i, b.aOuter) | ||
true | ||
case _ => | ||
false | ||
} | ||
def pat4(a: M.this.B2) = a match { | ||
case b: m2.A => // can elide outer check, (a : m1.B2) implies (a : m2.A) | ||
assertOuter(m2.i, b.aOuter) | ||
true | ||
case _ => | ||
false | ||
} | ||
} | ||
|
||
val m1 = new M(1); | ||
val m2 = new M(2); | ||
|
||
def pat1(a: m1.A) = a match { | ||
case b: m1.B1 => // can elide outer check, (a : m1.A) && (a : O#B1) implies (a : m1.B1) | ||
assertOuter(m1.i, b.b1Outer) | ||
true | ||
case _ => | ||
false | ||
} | ||
def pat2(a: m2.A) = a match { | ||
case b: m1.B2 => // needs runtime outer check | ||
assertOuter(m1.i, b.b2Outer) | ||
true | ||
case _ => | ||
false | ||
} | ||
def pat3(a: m1.B1) = a match { | ||
case b: m1.A => // can elide outer check, (a : m1.B1) && (a : O#A) implies (a : m1.B1) | ||
assertOuter(m1.i, b.aOuter) | ||
true | ||
case _ => | ||
false | ||
} | ||
def pat4(a: m1.B2) = a match { | ||
case b: m2.A => // can elide outer check, (a : m1.B2) implies (a : m2.A) | ||
assertOuter(m2.i, b.aOuter) | ||
true | ||
case _ => | ||
false | ||
} | ||
|
||
def pat5(a: M#B2) = a match { | ||
case b: m2.A => // can elide outer check, (a : A#B2) implies (a : m2.A) | ||
assertOuter(m2.i, b.aOuter) | ||
true | ||
case _ => | ||
false | ||
} | ||
def assertOuter(expected: Int, actual: Int): Unit = { | ||
if (expected != actual) throw WrongOuter(expected, actual) | ||
} | ||
case class WrongOuter(expected: Int, actual: Int) extends RuntimeException(s"expected: $expected, actual: $actual") | ||
|
||
def main(args: Array[String]): Unit = { | ||
assert(pat1(new m1.B1)) | ||
assert(m1.pat1(new m1.B1)) | ||
assert(Try(pat1((new m2.B1).asInstanceOf[m1.B1])).failed.get == WrongOuter(m1.i, m2.i)) | ||
assert(Try(m1.pat1((new m2.B1).asInstanceOf[m1.B1])).failed.get == WrongOuter(m1.i, m2.i)) | ||
|
||
assert(!pat2(new m2.B2)) | ||
assert(!m1.pat2(new m2.B2)) | ||
assert(pat2(new m1.B2)) | ||
assert(m1.pat2(new m1.B2)) | ||
|
||
assert(pat3(new m1.B1)) | ||
assert(m1.pat3(new m1.B1)) | ||
assert(Try(pat3((new m2.B1).asInstanceOf[m1.B1])).failed.get == WrongOuter(m1.i, m2.i)) | ||
assert(Try(m1.pat3((new m2.B1).asInstanceOf[m1.B1])).failed.get == WrongOuter(m1.i, m2.i)) | ||
|
||
assert(pat4(new m1.B2)) | ||
assert(m1.pat4(new m1.B2)) | ||
assert(pat4((new m2.B2).asInstanceOf[m1.B2])) | ||
assert(m1.pat4((new m2.B2).asInstanceOf[m1.B2])) | ||
|
||
assert(pat5(new m1.B2)) | ||
assert(pat5(new m2.B2)) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This failed, and I think it's because it's failing on
xsbti.ScalaProvider with sbt.internal.inc.ScalaInstance.ScalaProvider2 @unchecked
. See scala/community-build#1400 (comment). I'm going to try to fix this, but work-stealers welcome.