scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Regression in short syntax method invocation introduced in 3.3.4

Open majk-p opened this issue 4 months ago • 5 comments

Compiler version

The attached code compiled well up to 3.3.3 and stopped compiling with 3.3.4. Compilation also fails on latest 3.5.1.

Minimized code

Also available as a gist https://gist.github.com/majk-p/b510b46cb38ecbb9bc7dd29c30a1055f

//> using dep "org.typelevel::cats-effect:3.5.4"
//> using scala "3.3.3"

import cats.Functor
import cats.effect.std.UUIDGen
import cats.syntax.all.*

import java.util.UUID

final class MyId private (val id: String)

object MyId {

  def fromUUID[F[_]: Functor: UUIDGen]: F[MyId] =
    UUIDGen[F].randomUUID.map(fromUUID)
    // UUIDGen[F].randomUUID.map(MyId.fromUUID)     // this also doesn't compile
    // UUIDGen[F].randomUUID.map(fromUUID(_))       // this on the other hand compiles
    // UUIDGen[F].randomUUID.map(MyId.fromUUID(_))  // this also compiles

  private def fromUUID(id: UUID): MyId =
    MyId(id.toString())
}

Notice how this only affects the short syntax without explicit parentheses. Adding (_) solves the issue. Changing the second method name to anything non-ambiguous also fixes the example, this means following example also works:

object MyId {

  def fromUUID[F[_]: Functor: UUIDGen]: F[MyId] =
    UUIDGen[F].randomUUID.map(otherMethodName)

  private def otherMethodName(id: UUID): MyId =
    MyId(id.toString())
}

Output

[error] ./main.scala:15:39
[error] Ambiguous given instances: both method catsFlatMapForSortedMap in object Invariant and method catsFlatMapForMap in object Invariant match type cats.Functor[F] of a context parameter of method fromUUID in object MyId
[error]     UUIDGen[F].randomUUID.map(fromUUID)
[error]    

Expectation

Compiles with no issues

Additional research

I found the great bisect tool in compiler sources and run it against my example code. Here's the output:

6f33c6128df2168f4e9fb3911491f466f6fe9f90 is the first bad commit
commit 6f33c6128df2168f4e9fb3911491f466f6fe9f90
Author: Wojciech Mazur <[email protected]>
Date:   Fri Jun 28 18:20:54 2024 +0200

    error when reading class file with unknown newer jdk version
    
    
    [Cherry-picked f430e449869d9d6b6cf05373086f3d52b0a11805][modified]

 .../dotc/core/classfile/ClassfileConstants.scala   |  1 +
 .../dotc/core/classfile/ClassfileParser.scala      | 34 +++++++++++++---------
 2 files changed, 21 insertions(+), 14 deletions(-)
bisect found first bad commit
Previous HEAD position was 6f33c6128d error when reading class file with unknown newer jdk version
HEAD is now at e76de95ff8 Release 3.3.4

This is how I configured the bisect tool:

scala-cli project/scripts/bisect.scala -- --releases 3.3.2-RC1-bin-20230724-ce1ce99-NIGHTLY...3.3.5-RC1-bin-20240712-4eb7668-NIGHTLY compile https://gist.github.com/majk-p/b510b46cb38ecbb9bc7dd29c30a1055f

It looks like https://github.com/scala/scala3/pull/20862 is the PR that introduced the regression. CC: @WojciechMazur @bishabosha

majk-p avatar Oct 08 '24 05:10 majk-p