Skip to content

Inapplicable given causing ambiguity #23240

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
OlegYch opened this issue May 22, 2025 · 2 comments
Open

Inapplicable given causing ambiguity #23240

OlegYch opened this issue May 22, 2025 · 2 comments
Labels
area:implicits related to implicits itype:bug

Comments

@OlegYch
Copy link
Contributor

OlegYch commented May 22, 2025

Compiler version

3.6.3-3.7.0

Minimized code

//> using scala 3.6.3
class Test {
  trait Typeclass[T] extends Typeclass.Base[T]
  object Typeclass {
    trait Base[T]
    trait Derived[T] extends Typeclass[T]
    // this shouldn't be applicable to A
    given [T: Base]: Typeclass[T] = null
  }

  trait Tag[T]
  given [T]: Tag[T] = null

  class A[T]
  object A {
    // removing Tag somehow resolves ambiguity
    given [T: Tag]: Typeclass.Derived[A[T]] = null
  }

  summon[Typeclass[A[Int]]]
}

Output

[warn] 19 |  summon[Typeclass[A[Int]]]
[warn]    |                           ^
[warn]    |Given search preference for Test.this.Typeclass[Test.this.A[Int]] between alternatives
[warn]    |  (Test.this.A.given_Derived_A :
[warn]    |  [T]
[warn]    |    (using evidence$1: Test.this.Tag[T]):
[warn]    |      Test.this.Typeclass.Derived[Test.this.A[T]]
[warn]    |)
[warn]    |and
[warn]    |  (Test.this.Typeclass.given_Typeclass_T :
[warn]    |  [T]
[warn]    |    (using evidence$1: Test.this.Typeclass.Base[T]):
[warn]    |      Test.this.Typeclass[T]
[warn]    |)
[warn]    |will change in the future release.
[warn]    |Current choice        : the first alternative
[warn]    |Choice from Scala 3.7 : none - it's ambiguous
[warn]    |                                                               
[warn]    |Suppress this warning by choosing -source 3.5, -source 3.7, or 
[warn]    |by using @annotation.nowarn("id=205")
[warn] one warning found                         

Expectation

code compiles without warnings (or errors on 3.7)

looks related to #22913 but here there is no real ambiguity

@OlegYch OlegYch added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels May 22, 2025
@OlegYch
Copy link
Contributor Author

OlegYch commented May 22, 2025

another example, this time causing error even on 3.5

//> using scala 3.5.0
@main def Test = {
  trait Typeclass[T] extends Typeclass.Base[T]
  object Typeclass {
    trait Base[T]
    trait Derived[T] extends Typeclass[T]
    given [T: Base]: Typeclass[T] = ???
  }

  trait Tag[T]
  given [T]: Tag[T] = null

  class A[T]
  object A {
    // removing Tag somehow resolves ambiguity
    given [T: Tag]: Typeclass.Base[A[T]] = null
  }

  println(summon[Typeclass[A[Int]]])
}

produces

[error] 18 |  println(summon[Typeclass[A[Int]]])
[error]    |                                   ^
[error]    |No best given instance of type Typeclass[A[Int]] was found for parameter x of method summon in object Predef.
[error]    |I found:
[error]    |                                                              
[error]    |    Typeclass.given_Typeclass_T[A[Int]](
[error]    |      /* ambiguous: both given instance given_Typeclass_T in o
bject Typeclass and given instance given_Base_A in object A match type Typeclass.Base[A[Int]] */
[error]    |        summon[Typeclass.Base[A[Int]]]
[error]    |    )
[error]    |                                                              
[error]    |But both given instance given_Typeclass_T in object Typeclass 
and given instance given_Base_A in object A match type Typeclass.Base[A[Int]].
[error] one error found

@OlegYch
Copy link
Contributor Author

OlegYch commented May 22, 2025

somehow adding an additional constraint prevents compiler from detecting infinite recursion?

@Gedochao Gedochao added area:implicits related to implicits and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels May 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:implicits related to implicits itype:bug
Projects
None yet
Development

No branches or pull requests

2 participants