Skip to content
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

Deferred given in companion object causes infinite recursion #22589

Open
nkgm opened this issue Feb 12, 2025 · 1 comment · May be fixed by #22595
Open

Deferred given in companion object causes infinite recursion #22589

nkgm opened this issue Feb 12, 2025 · 1 comment · May be fixed by #22595

Comments

@nkgm
Copy link

nkgm commented Feb 12, 2025

Compiler version

3.6.3

Minimized code

import scala.compiletime.deferred

trait MyCodec[E]

object auto {
  trait CompanionEssentials[E] {
    given MyCodec[E] = deferred
  }
}

import auto.CompanionEssentials

case class Person(name: String, age: Int)
object Person extends CompanionEssentials[Person]
// deferred given implemented as:
// override final lazy given val given_MyCodec_E: MyCodec[Person] =
//   Person.given_MyCodec_E

Output

Due to module class Person being in the implicit scope of Person, the compiler resolves the deferred given to its unimplemented self resulting in infinite recursion. This happens regardless of presence of a valid given in scope or not.

Expectation

The compiler should detect the cycle and exclude Person.given_MyCodec_E as a viable candidate, and produce an error if the deferred given couldn't be synthesized from other candidates in scope.

Workaround

The infinite recursion problem can be avoided by explicitly importing the relevant givens (which is probably what you should be doing in the first place):

object auto {
  trait CompanionEssentials[E] {
    given MyCodec[E] = deferred
  }

  given [E] => MyCodec[E] = new MyCodec[E]{}
}

import auto.{CompanionEssentials, given}

case class Person(name: String, age: Int)
object Person extends CompanionEssentials[Person]
// deferred given implemented as:
// override final lazy given val given_MyCodec_E: MyCodec[Person] =
//   auto.given_MyCodec_E[Person]
@nkgm nkgm added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 12, 2025
@som-snytt
Copy link
Contributor

It's interesting that it doesn't trigger in CheckLoopingImplicits, as the RHS is derived from the enclosing environment. The explicit definition warns as normal.

@som-snytt som-snytt self-assigned this Feb 12, 2025
@som-snytt som-snytt added area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 12, 2025
@som-snytt som-snytt linked a pull request Feb 13, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants