Skip to content

Commit 66d15b2

Browse files
authored
Avoid false warning when synthesising deferred givens (#23087)
Fixes #23049
2 parents c481e91 + 733e253 commit 66d15b2

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ class DependencyRecorder {
577577
clazz
578578
}
579579

580-
private var _responsibleForImports: Symbol = uninitialized
580+
private[dotc] var _responsibleForImports: Symbol | Null = null
581581

582582
/** Top level import dependencies are registered as coming from a first top level
583583
* class/trait/object declared in the compilation unit. If none exists, issue a warning and return NoSymbol.

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3205,10 +3205,26 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
32053205
val usingParamAccessors = cls.paramAccessors.filter(_.is(Given))
32063206
val paramScope = newScopeWith(usingParamAccessors*)
32073207
val searchCtx = ctx.outer.fresh.setScope(paramScope)
3208+
3209+
// Before losing the reference to ctx.owner
3210+
// when calling implicitArgTree with searchCtx,
3211+
// let's store ctx.owner as the fallback "responsibleForImports"
3212+
// in DependencyRecorder. That way, if we end up recording any dependencies
3213+
// we use ctx.owner as the "fromClass" rather than emitting a warning
3214+
// (because ctx.compilationUnit.tpdTree is still EmptyTree during typer).
3215+
// For example, to record mirror dependencies, see i23049.
3216+
val depRecorder = ctx.compilationUnit.depRecorder
3217+
val responsibleForImports = depRecorder._responsibleForImports
3218+
if responsibleForImports == null then
3219+
depRecorder._responsibleForImports = ctx.owner
3220+
32083221
val rhs = implicitArgTree(target, cdef.span,
32093222
where = i"inferring the implementation of the deferred ${dcl.showLocated}"
32103223
)(using searchCtx)
32113224

3225+
if responsibleForImports == null then
3226+
depRecorder._responsibleForImports = null
3227+
32123228
val impl = dcl.copy(cls,
32133229
flags = dcl.flags &~ (HasDefault | Deferred) | Final | Override,
32143230
info = target,

tests/warn/i23049.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
trait TC[X]
2+
object TC {
3+
given [X: scala.deriving.Mirror.ProductOf]: TC[X] = ???
4+
}
5+
6+
trait Base[T] {
7+
given TC[T] = scala.compiletime.deferred
8+
}
9+
10+
case class P(x: Int)
11+
12+
object A extends Base[P]

0 commit comments

Comments
 (0)