scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.79k stars 1.04k forks source link

Failed to bind context-depended type into Aux when matcher is inline #13899

Open rssh opened 2 years ago

rssh commented 2 years ago

Compiler version

3.1.0, 3.1.1-RC1

Minimized code

trait CBContext:
 def foo() = ???

trait CB[T]

given ContextMonad[CB] with

   override type Context = CBContext

// inline am is significant - without inline this compild
inline def runContext[F[_]](using inline am: ContextMonad[F]) = InferAsyncArg(am)

class InferAsyncArg[F[_],C](val am: ContextMonad.Aux[F,C]):

   def apply[T](f: C ?=> T):F[T] = ???

object X:

  def bar = runContext[CB]{
     summon[CBContext].foo()
     1
  }

Output

[error] -- [E007] Type Mismatch Error: /Users/rssh/tests/dotty/summon-context/src/main/scala/x/M.scala:28:26 
[error] 28 |  def bar = runContext[CB]{
[error]    |                          ^
[error]    |            Found:    x#3983.given_ContextMonad_CB#4005.type
[error]    |            Required: x#3983.ContextMonad#3987.Aux#7385[x#3983.CB#3990, 
[error]    |              x#3983.ContextMonad#3985[x#3983.CB#3990]#Context#6121
[error]    |            ]
[error] one error found
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 4 s, completed 7 Nov 2021, 19:04:20

Expectation

Should be compiled.

nicolasstucki commented 2 years ago

Missing the definition of ContextMonad

rssh commented 2 years ago

oops, here is the full text:

package x

trait ContextMonad[F[_]]:
 type Context

object ContextMonad:
 type Aux[F[_],C] = ContextMonad[F] { type Context = C }

trait CBContext:
 def foo() = ???

trait CB[T]

given ContextMonad[CB] with
   override type Context = CBContext

// inline am is significant - without inline this compild
inline def runContext[F[_]](using inline am: ContextMonad[F]) = InferAsyncArg(am)

class InferAsyncArg[F[_],C](val am: ContextMonad.Aux[F,C]):
   def apply[T](f: C ?=> T):F[T] = ???

object X:

  def bar = runContext[CB]{
     summon[CBContext].foo()
     1
  }
nicolasstucki commented 1 year ago

The current failure is

-- [E007] Type Mismatch Error: t/Test.scala:19:78 ------------------------------
19 |inline def runContext[F[_]](using inline am: ContextMonad[F]) = InferAsyncArg(am)
   |                                                                              ^^
   |  Found:    (am : x.ContextMonad[F])
   |  Required: x.ContextMonad.Aux[F², C]
   |
   |  where:    C  is a type variable
   |            F  is a type in method runContext with bounds <: [_] =>> Any
   |            F² is a type variable with constraint <: [_²] =>> Any
   |
   | longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: t/Test.scala:27:5 -------------------------------
27 |     summon[CBContext].foo()
   |     ^^^^^^^^^^^^^^^^^
   |Found:    (evidence$1 : Nothing)
   |Required: ?{ foo: ? }
   |Note that implicit conversions were not tried because the result of an implicit conversion
   |must be more specific than ?{ foo: [applied to () returning <?>] }
   |
   | longer explanation available when compiling with `-explain`

Not sure if that is the expected behavior.