Skip to content

Type alias results in confusing type mismatch #8067

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

Closed
travisbrown opened this issue Jan 22, 2020 · 1 comment
Closed

Type alias results in confusing type mismatch #8067

travisbrown opened this issue Jan 22, 2020 · 1 comment

Comments

@travisbrown
Copy link
Contributor

minimized code

trait Tc[F[_]]
type OfString[F[_]] = F[String]

def foo[F[_]](fs: F[String])(implicit F: Tc[F]): F[String] = ???
def bar[F[_]](fs: OfString[F]): F[String] = foo(fs)(new Tc[F] {})
Compilation output
-- [E007] Type Mismatch Error: Tc.scala:5:64 -----------------------------------
5 |def bar[F[_]](fs: OfString[F]): F[String] = foo(fs)(new Tc[F] {})
  |                                                                ^
  |Found:    Object with Tc[F] {...}
  |Required: Tc[F²]
  |
  |where:    F  is a type in method bar with bounds <: [_$4] => Any
  |          F² is a type variable with constraint >: [X0] => F[X0] | X0[String] and <: [_$3] => Any
1 error found

expectation

This compiles on Scala 2, and I ran into it while trying to cross-build cats-effect. I don't think it's the same variance issue as #7993, but I could be wrong about that?

@smarter
Copy link
Member

smarter commented Jan 22, 2020

Using different type parameter names to make the error clearer:

trait Tc[F[_]]
type OfString[G[_]] = G[String]

def foo[A[_]](fs: A[String])(implicit f: Tc[A]): A[String] = ???
def bar[B[_]](fs: OfString[B]): B[String] = foo(fs)(new Tc[B] {})
ary-bootstrapped/scala-0.22/dotty-library_0.22-0.22.0-bin-SNAPSHOT.jar -Xprint-types -Xprint:typer try/i8067.scala
-- [E007] Type Mismatch Error: try/i8067.scala:5:64 ----------------------------
5 |def bar[B[_]](fs: OfString[B]): B[String] = foo(fs)(new Tc[B] {})
  |                                                                ^
  |Found:    Object with Tc[B] {...}
  |Required: Tc[A]
  |
  |where:    A is a type variable with constraint >: [X0] => B[X0] | X0[String] and <: [_$3] => Any
  |          B is a type in method bar with bounds <: [_$4] => Any

Something is seriously wrong here. In [X0] => B[X0] | X0[String], X0 is simply-kinded but it appears as a type constructor.
In fact, if we add a variance annotation we can get a fun error message involving the novel type String[String] :) :

trait Tc[+F[_]]
// ... same as before
5 |def bar[B[_]](fs: OfString[B]): B[String] = foo(fs)(new Tc[B] {})
  |                                            ^^^^^^^^^^^^^^^^^^^^^
  |           Found:    B[String] | String[String]
  |           Required: B[String]
  |
  |           where:    B is a type in method bar with bounds <: [_$4] => Any

Unassigning myself because I've got enough things in my todo list for now 😵

@smarter smarter removed their assignment Jan 22, 2020
odersky added a commit that referenced this issue Jan 31, 2020
Fix #8067: Don't constrain with types of wrong kinds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants