diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2015-09-30 16:09:04 +1000 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2015-09-30 16:09:04 +1000 |
commit | fbdfc83b5f0b3a2d2962c1adad8875a7e58d6497 (patch) | |
tree | 39c2ded6c0a4426af883a3e47a08a8d502b80245 /src | |
parent | d770340a25562eecf6b0aa576f412530668fcdce (diff) | |
download | scala-fbdfc83b5f0b3a2d2962c1adad8875a7e58d6497.tar.gz scala-fbdfc83b5f0b3a2d2962c1adad8875a7e58d6497.tar.bz2 scala-fbdfc83b5f0b3a2d2962c1adad8875a7e58d6497.zip |
SI-9498 Avoid caching bug with pattern type variables
Typechecking a pattern that defines a pattern type variable
initially assigns abstract type symbol with open type bounds.
Later on, pattern type inference kicks in to sharpen the type
of the variable based on constraints imposed by the expected
type (ie, the type of scrutinee of the pattern.)
However, before inference does this, a `TypeRef` to the abstract
type symbol can be queried for its base type with respect to some
class, which leads to it populating an internal cache. This cache
becomes stale when the underlying symbol has its type mutated.
The repercussions of this meant that a subsequent call to `baseType`
gave the wrong result (`NoType`), which lead to an `asSeenFrom`
operation to miss out of substitution of a type variable. Note the
appearance of `A` in the old type errors in the enclosed test case.
This commit takes an approach similar to 286dafbd to invalidate
caches after the mutation. I've routed both bandaids through the
same first aid kit: I'm sure over time we'll add additional calls
to this method, and additional cache invalidations within it.
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/Infer.scala | 1 | ||||
-rw-r--r-- | src/reflect/scala/reflect/internal/Trees.scala | 14 | ||||
-rw-r--r-- | src/reflect/scala/reflect/internal/Types.scala | 20 |
3 files changed, 20 insertions, 15 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Infer.scala b/src/compiler/scala/tools/nsc/typechecker/Infer.scala index a5fdbb5148..684cf788a4 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Infer.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Infer.scala @@ -1207,6 +1207,7 @@ trait Infer extends Checkable { } } tvars foreach instantiateTypeVar + invalidateTreeTpeCaches(tree0, tvars.map(_.origin.typeSymbol)) } /* If the scrutinee has free type parameters but the pattern does not, * we have to flip the arguments so the expected type is treated as more diff --git a/src/reflect/scala/reflect/internal/Trees.scala b/src/reflect/scala/reflect/internal/Trees.scala index bbd9df05d2..49554d6d0f 100644 --- a/src/reflect/scala/reflect/internal/Trees.scala +++ b/src/reflect/scala/reflect/internal/Trees.scala @@ -1617,21 +1617,9 @@ trait Trees extends api.Trees { } def apply[T <: Tree](tree: T): T = { val tree1 = transform(tree) - invalidateSingleTypeCaches(tree1) + invalidateTreeTpeCaches(tree1, mutatedSymbols) tree1.asInstanceOf[T] } - private def invalidateSingleTypeCaches(tree: Tree): Unit = { - if (mutatedSymbols.nonEmpty) - for (t <- tree if t.tpe != null) - for (tp <- t.tpe) { - tp match { - case s: SingleType if mutatedSymbols contains s.sym => - s.underlyingPeriod = NoPeriod - s.underlyingCache = NoType - case _ => - } - } - } override def toString() = "TreeSymSubstituter/" + substituterString("Symbol", "Symbol", from, to) } diff --git a/src/reflect/scala/reflect/internal/Types.scala b/src/reflect/scala/reflect/internal/Types.scala index 2ae95c98e5..1ac772fb70 100644 --- a/src/reflect/scala/reflect/internal/Types.scala +++ b/src/reflect/scala/reflect/internal/Types.scala @@ -1989,8 +1989,8 @@ trait Types * several times. Hence, no need to protected with synchronized in a multi-threaded * usage scenario. */ - private var relativeInfoCache: Type = _ - private var relativeInfoPeriod: Period = NoPeriod + private[Types] var relativeInfoCache: Type = _ + private[Types] var relativeInfoPeriod: Period = NoPeriod private[Types] def relativeInfo = /*trace(s"relativeInfo(${safeToString}})")*/{ if (relativeInfoPeriod != currentPeriod) { @@ -4569,6 +4569,22 @@ trait Types if (!phase.erasedTypes && tp.typeSymbol == ObjectClass) AnyTpe else tp + def invalidateTreeTpeCaches(tree: Tree, updatedSyms: List[Symbol]) = if (updatedSyms.nonEmpty) + for (t <- tree if t.tpe != null) + for (tp <- t.tpe) { + invalidateCaches(tp, updatedSyms) + } + + def invalidateCaches(t: Type, updatedSyms: List[Symbol]) = t match { + case st: SingleType if updatedSyms.contains(st.sym) => + st.underlyingCache = NoType + st.underlyingPeriod = NoPeriod + case tr: NonClassTypeRef if updatedSyms.contains(tr.sym) => + tr.relativeInfoCache = NoType + tr.relativeInfoPeriod = NoPeriod + case _ => + } + val shorthands = Set( "scala.collection.immutable.List", "scala.collection.immutable.Nil", |