Skip to content

Compiler incorrectly tries to invoke context function passed as argument #10825

Closed
@elfprince13

Description

@elfprince13

Minimized code

trait A { def apply():Int }
trait B { def apply():Int }

class MWE2 {
  type Op = (A, B) ?=> Int

  def doOp(op:Op):Int = {
    implicit val a:A = new A{ def apply():Int = 2}
    implicit val b:B = new B{ def apply():Int = 3}
    op
  }
  
  def main(args:Array[String]):Unit = {
    val op:Op = {
      summon[A]() + summon[B]()
    }
    implicit val a:A = new A{ def apply():Int = 6}
    implicit val b:B = new B{ def apply():Int = 7}
    Console.println(doOp(op))
  }
}

Output

[error] 17 |      summon[A]() + summon[B]()
[error]    |               ^
[error]    |ambiguous implicit arguments: both value evidence$1 and value a match type A of parameter x of method summon in object Predef
[error] -- Error: src/main/scala/MWE2.scala:21:27
[error] 21 |    Console.println(doOp(op))
[error]    |                           ^
[error]    |ambiguous implicit arguments: both value evidence$5 and value a match type A of parameter of MWE2.this.Op
[error]    |which is an alias of: (A, B) ?=> Int
[error] two errors found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 8 s, completed Dec 16, 2020 5:48:13 PM

Expectation

op should not be invoked in main but should instead be passed to doOp.
Additionally, if the compiler is supposed to invoke it in main (which would be very surprising), the result should be a type error (found Int, required Op).
Further a should not be ambiguous with some anonymous evidence when I'm not even using any context bounds or anything else implicits / givens related that would be generating evidence.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions