Skip to content

Derived instances fail to discover givens in the type's companion object #23897

@mrdziuban

Description

@mrdziuban

Compiler version

3.5.2, 3.6.4, 3.7.3, and latest nightly 3.8.0-RC1-bin-20250822-658c8bd-NIGHTLY

Minimized code

import scala.deriving.Mirror

class Test[A]
object Test {
  def derived[A](using m: Mirror.Of[A], t: Test[Int]): Test[A] = new Test[A]
}

case class Foo(i: Int) derives Test
object Foo {
  given i: Test[Int] = new Test[Int]
}

Output

-- [E172] Type Error: ----------------------------------------------------------
8 |case class Foo(i: Int) derives Test
  |                               ^
  |No given instance of type Test[Int] was found for parameter t of method derived in object Test

Expectation

Since the derived$Test instance is synthesized and defined at the bottom of object Foo, the given i: Test[Int] instance should be in implicit scope. This appears to be an issue with the given prioritization change in Scala 3.5 since it works as expected in Scala 3.3 and 3.4.

The problem is that Deriving.scala assigns pos.span as the position of derived$Test, where pos is the position at which Test appears in the derives clause on line 8 -- the same position that the error points to. This causes Implicits.scala to consider given i to come too late to be considered a valid candidate for the implicit search, specifically because sym.span.start <= candSym.span.start returns true here.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions