scalafix icon indicating copy to clipboard operation
scalafix copied to clipboard

ExplicitResultTypes generate invalid code when use type lambda

Open xuwei-k opened this issue 4 years ago • 2 comments

step

project/build.properties

sbt.version=1.3.10

project/scalafix.sbt

addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.15")

build.sbt

scalaVersion := "2.12.11"

Main.scala

trait Functor[F[_]]

object Main {
  def foo[A] = new Functor[({type l[x] = Either[A, x]})#l]{}
}

run sbt scalafixEnable "scalafix ExplicitResultTypes" compile

result

trait Functor[F[_]]

object Main {
-  def foo[A] = new Functor[({type l[x] = Either[A, x]})#l]{}
+  def foo[A]: Functor[Either[A,x]] = new Functor[({type l[x] = Either[A, x]})#l]{}
}
[error] /Users/kenji/type-lambda-scalafix/Main.scala:4:32: not found: type x
[error]   def foo[A]: Functor[Either[A,x]] = new Functor[({type l[x] = Either[A, x]})#l]{}
[error]                                ^
[error] /Users/kenji/type-lambda-scalafix/Main.scala:4:23: Either[A,<error>] takes no type parameters, expected: one
[error]   def foo[A]: Functor[Either[A,x]] = new Functor[({type l[x] = Either[A, x]})#l]{}
[error]                       ^
[error] two errors found
[error] (Compile / compileIncremental) Compilation failed

xuwei-k avatar May 24 '20 01:05 xuwei-k

I think most people running into this will be using kind-projector - would be be out of scope for this project to have an option of using kind-projector syntax for type lambdas?

bplommer avatar Nov 13 '21 14:11 bplommer

I think most people running into this will be using kind-projector

scalacOptions are exposed to the rule implementation, so i believe it's possible to implement a decent heuristic to know whether kind-projector is available in the project.

would be be out of scope for this project to have an option of using kind-projector syntax for type lambdas?

Provided the potential heuristic mentioned above works, I am not sure a flag (no matter if it's opt-in or opt-out) is even necessary. When a partially-applied type is detected, we would either use kind-projector, or fallback to the anonymous type projection.

bjaglin avatar Nov 13 '21 21:11 bjaglin