Skip to content

Commit 44976dc

Browse files
committed
Cache only when the context corresponds to prefix
Caching a lookup of c in a.b.c is ok at a.b. A lookup from a.p should not see the cached value.
1 parent 88a741b commit 44976dc

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

Diff for: compiler/src/dotty/tools/dotc/transform/CheckUnused.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
330330
def addCached(where: Context, result: Precedence): Unit =
331331
if where.moreProperties ne null then
332332
where.property(resolvedKey) match
333-
case Some(resolved) =>
333+
case Some(resolved) if where.owner.isLocalToBlock || where.owner.info =:= prefix =>
334334
resolved.record(sym, name, prefix, result)
335335
case none =>
336336

@@ -361,7 +361,7 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
361361
cur.property(resolvedKey) match
362362
case Some(resolved) =>
363363
// conservative, cache must be nested below the result context
364-
if precedence.isNone then
364+
if precedence.isNone && (cur.owner.isLocalToBlock || cur.owner.info =:= prefix) then
365365
cachePoint = cur // no result yet, and future result could be cached here
366366
resolved.hasRecord(sym, name, prefix)
367367
case none => NoPrecedence

Diff for: tests/warn/i22971.scala

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//> using options -Wunused:imports
2+
3+
package p:
4+
5+
trait Base
6+
class Class extends Base
7+
8+
abstract class Entity[T: GetType]
9+
10+
class Thing extends Entity[Class]
11+
12+
trait GetType[T]
13+
14+
object GetType {
15+
//implicit object GetTypeClass extends GetType[Class]
16+
implicit val GetTypeClass: GetType[Class] = new GetType[Class] {}
17+
}
18+
object Main {
19+
def main(args: Array[String]): Unit = {
20+
import GetType.*
21+
val e = GetTypeClass
22+
}
23+
}
24+
25+
package q:
26+
27+
class C:
28+
def f =
29+
import p.*
30+
GetType.GetTypeClass
31+
def g =
32+
import p.GetType.*
33+
GetTypeClass
34+
class D extends p.Entity[p.Class]

0 commit comments

Comments
 (0)