Skip to content

Commit fdcebfd

Browse files
authored
Merge pull request scala#6068 from milessabin/topic/si-9122
Only cache ImplicitInfo erroneous status in isCyclicOrErroneous.
2 parents d2b2eed + 8e28f3f commit fdcebfd

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,24 @@ trait Implicits {
207207
*/
208208
class ImplicitInfo(val name: Name, val pre: Type, val sym: Symbol) {
209209
private var tpeCache: Type = null
210-
private var isCyclicOrErroneousCache: TriState = TriState.Unknown
210+
private var isErroneousCache: TriState = TriState.Unknown
211211

212212
/** Computes member type of implicit from prefix `pre` (cached). */
213213
def tpe: Type = {
214214
if (tpeCache eq null) tpeCache = pre.memberType(sym)
215215
tpeCache
216216
}
217217

218-
def isCyclicOrErroneous: Boolean = {
219-
if (!isCyclicOrErroneousCache.isKnown) isCyclicOrErroneousCache = computeIsCyclicOrErroneous
220-
isCyclicOrErroneousCache.booleanValue
221-
}
218+
def isCyclicOrErroneous: Boolean =
219+
if(sym.hasFlag(LOCKED)) true
220+
else {
221+
if(!isErroneousCache.isKnown)
222+
isErroneousCache = computeErroneous
223+
isErroneousCache.booleanValue
224+
}
222225

223-
private[this] final def computeIsCyclicOrErroneous =
224-
try sym.hasFlag(LOCKED) || containsError(tpe)
226+
private[this] final def computeErroneous =
227+
try containsError(tpe)
225228
catch { case _: CyclicReference => true }
226229

227230
var useCountArg: Int = 0

test/files/pos/t9122.scala

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class X[A](a: A)
2+
object Test {
3+
implicit val ImplicitBoolean: Boolean = true
4+
def local = {
5+
implicit object X extends X({ def local2 = implicitly[Boolean] ; "" })
6+
implicitly[X[String]]
7+
}
8+
}

test/files/run/implicit-caching.scala

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
trait Foo[T]
2+
3+
trait FooSub[T] extends Foo[T] {
4+
type Super = Foo[T]
5+
}
6+
7+
object FooSub {
8+
implicit def fooSub[T](implicit ft: Bar[T]): FooSub[T] =
9+
new FooSub[T] {}
10+
}
11+
12+
trait Bar[T]
13+
14+
class Quux
15+
16+
object Quux {
17+
implicit val barQuux: Bar[Quux] = new Bar[Quux] {}
18+
19+
val fooSubQuux = implicitly[FooSub[Quux]]
20+
implicit val fooQuux: fooSubQuux.Super = fooSubQuux
21+
}
22+
23+
object Test extends App {
24+
implicitly[Foo[Quux]]
25+
}

0 commit comments

Comments
 (0)