Skip to content

Commit cf16664

Browse files
committed
Remove variance check for escaping locals
1 parent 804e31f commit cf16664

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,8 +1566,6 @@ abstract class RefChecks extends Transform {
15661566

15671567
if (!sym.exists)
15681568
devWarning("Select node has NoSymbol! " + tree + " / " + tree.tpe)
1569-
else if (sym.isLocalToThis)
1570-
varianceValidator.checkForEscape(sym, currentClass)
15711569

15721570
def checkSuper(mix: Name) =
15731571
// term should have been eliminated by super accessors

src/reflect/scala/reflect/internal/Variances.scala

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ trait Variances {
2828
* TODO - eliminate duplication with varianceInType
2929
*/
3030
class VarianceValidator extends InternalTraverser {
31-
private[this] val escapedLocals = mutable.HashSet[Symbol]()
3231
// A flag for when we're in a refinement, meaning method parameter types
3332
// need to be checked.
3433
private[this] var inRefinement = false
@@ -38,17 +37,6 @@ trait Variances {
3837
try body finally inRefinement = saved
3938
}
4039

41-
/** Is every symbol in the owner chain between `site` and the owner of `sym`
42-
* either a term symbol or private[this]? If not, add `sym` to the set of
43-
* escaped locals.
44-
* @pre sym.isLocalToThis
45-
*/
46-
@tailrec final def checkForEscape(sym: Symbol, site: Symbol): Unit = {
47-
if (site == sym.owner || site == sym.owner.moduleClass || site.hasPackageFlag) () // done
48-
else if (site.isTerm || site.isPrivateLocal) checkForEscape(sym, site.owner) // ok - recurse to owner
49-
else escapedLocals += sym
50-
}
51-
5240
protected def issueVarianceError(base: Symbol, sym: Symbol, required: Variance, tpe: Type): Unit = ()
5341

5442
// Flip occurrences of type parameters and parameters, unless
@@ -62,10 +50,9 @@ trait Variances {
6250
)
6351

6452
// Is `sym` is local to a term or is private[this] or protected[this]?
65-
def isExemptFromVariance(sym: Symbol): Boolean = !sym.owner.isClass || (
66-
(sym.isLocalToThis || sym.isSuperAccessor) // super accessors are implicitly local #4345
67-
&& !escapedLocals(sym)
68-
)
53+
def isExemptFromVariance(sym: Symbol): Boolean =
54+
// super accessors are implicitly local #4345
55+
!sym.owner.isClass || sym.isLocalToThis || sym.isSuperAccessor
6956

7057
private object ValidateVarianceMap extends VariancedTypeMap {
7158
private[this] var base: Symbol = _

test/files/pos/variance-holes.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,19 @@ object Test {
2929
class RefinedUpper2[+A, x <: { type T[_ <: A] }]
3030
trait RefinedLower[+A, x <: { type T[_ >: A] }]
3131

32-
class PrivateThis[+A] {
32+
class PrivateThis1[+A] {
3333
private[this] object Foo { var x: A = _ }
3434
}
3535

36+
class PrivateThis2[-A] {
37+
private[this] val x: Set[A] = Set.empty
38+
private[this] var y: Set[A] = Set.empty
39+
40+
class Escape {
41+
println(x)
42+
println(y)
43+
}
44+
}
45+
3646
def generic[A]: Unit = ()
3747
}

0 commit comments

Comments
 (0)