Skip to content

Avoid false warning when synthesising deferred givens #23087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ class DependencyRecorder {
clazz
}

private var _responsibleForImports: Symbol = uninitialized
private[dotc] var _responsibleForImports: Symbol | Null = uninitialized
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're actually going to test whether it was initialized with an == null, then we should explicitly initialize it to null. uninitialized is supposed to be used only for things that will get assigned before every being read.

Suggested change
private[dotc] var _responsibleForImports: Symbol | Null = uninitialized
private[dotc] var _responsibleForImports: Symbol | Null = null


/** 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.
Expand Down
16 changes: 16 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions tests/warn/i23049.scala
Original file line number Diff line number Diff line change
@@ -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]
Loading