diff --git a/compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala b/compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala index 154d50f8ebc2..1ff008970b85 100644 --- a/compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala +++ b/compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala @@ -577,7 +577,7 @@ class DependencyRecorder { clazz } - private var _responsibleForImports: Symbol = uninitialized + private[dotc] var _responsibleForImports: Symbol | Null = uninitialized /** Top level import dependencies are registered as coming from a first top level * class/trait/object declared in the compilation unit. If none exists, issue a warning and return NoSymbol. diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 428568ecc795..d6de78f84f82 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -3158,10 +3158,26 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer val usingParamAccessors = cls.paramAccessors.filter(_.is(Given)) val paramScope = newScopeWith(usingParamAccessors*) val searchCtx = ctx.outer.fresh.setScope(paramScope) + + // Before losing the reference to ctx.owner + // when calling implicitArgTree with searchCtx, + // let's store ctx.owner as the fallback "responsibleForImports" + // in DependencyRecorder. That way, if we end up recording any dependencies + // we use ctx.owner as the "fromClass" rather than emitting a warning + // (because ctx.compilationUnit.tpdTree is still EmptyTree during typer). + // For example, to record mirror dependencies, see i23049. + val depRecorder = ctx.compilationUnit.depRecorder + val responsibleForImports = depRecorder._responsibleForImports + if responsibleForImports == null then + depRecorder._responsibleForImports = ctx.owner + val rhs = implicitArgTree(target, cdef.span, where = i"inferring the implementation of the deferred ${dcl.showLocated}" )(using searchCtx) + if responsibleForImports == null then + depRecorder._responsibleForImports = null + val impl = dcl.copy(cls, flags = dcl.flags &~ (HasDefault | Deferred) | Final | Override, info = target, diff --git a/tests/warn/i23049.scala b/tests/warn/i23049.scala new file mode 100644 index 000000000000..30797ea61689 --- /dev/null +++ b/tests/warn/i23049.scala @@ -0,0 +1,12 @@ +trait TC[X] +object TC { + given [X: scala.deriving.Mirror.ProductOf]: TC[X] = ??? +} + +trait Base[T] { + given TC[T] = scala.compiletime.deferred +} + +case class P(x: Int) + +object A extends Base[P]