Skip to content
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

Compiler crash using partial function that returns F[_] inside value class #18769

Closed
grzegorz-bielski opened this issue Oct 26, 2023 · 1 comment · Fixed by #18883
Closed

Comments

@grzegorz-bielski
Copy link
Contributor

Compiler version

3.3.1

Minimized code

//> using scala 3.3.1

trait Arb[F[_]]:
  def pure[A](x: A): F[A]

implicit class PfOps(self: Int) extends AnyVal {
    def pf[F[_]](using m: Arb[F]): PartialFunction[Int, F[Int]] = m.pure(_)
}

The example starts working again if either AnyVal or F[_] is removed, or PartialFunction is changed to Function1 or implicit class is converted to Scala 3 extension method, but it also fails without implicit keyword.

Output (click arrow to expand)

Compiling project (Scala 3.3.1, JVM)
Error: Error while emitting /Users/grzegorzbielski/projects/my/delete-me/Main.scala
assertion failed: Unexpected alias type: type F
Error compiling project (Scala 3.3.1, JVM)
Compilation failed

Full stack trace:

java.lang.AssertionError: assertion failed: Unexpected alias type: type F
        at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
        at dotty.tools.dotc.transform.GenericSignatures$.jsig$1(GenericSignatures.scala:261)
        at dotty.tools.dotc.transform.GenericSignatures$.boxedSig$1(GenericSignatures.scala:68)
        at dotty.tools.dotc.transform.GenericSignatures$.argSig$1(GenericSignatures.scala:195)
        at dotty.tools.dotc.transform.GenericSignatures$.classSig$1$$anonfun$1(GenericSignatures.scala:229)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
        at scala.collection.immutable.List.foreach(List.scala:333)
        at dotty.tools.dotc.transform.GenericSignatures$.classSig$1(GenericSignatures.scala:229)
        at dotty.tools.dotc.transform.GenericSignatures$.jsig$1(GenericSignatures.scala:288)
        at dotty.tools.dotc.transform.GenericSignatures$.boxedSig$1(GenericSignatures.scala:68)
        at dotty.tools.dotc.transform.GenericSignatures$.superSig$1$$anonfun$1(GenericSignatures.scala:65)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
        at scala.collection.immutable.List.foreach(List.scala:333)
        at dotty.tools.dotc.transform.GenericSignatures$.superSig$1(GenericSignatures.scala:65)
        at dotty.tools.dotc.transform.GenericSignatures$.jsig$1(GenericSignatures.scala:348)
        at dotty.tools.dotc.transform.GenericSignatures$.javaSig0(GenericSignatures.scala:365)
        at dotty.tools.dotc.transform.GenericSignatures$.javaSig(GenericSignatures.scala:40)
        at dotty.tools.backend.jvm.BCodeHelpers.dotty$tools$backend$jvm$BCodeHelpers$$getGenericSignatureHelper(BCodeHelpers.scala:773)
        at dotty.tools.backend.jvm.BCodeHelpers$BCJGenSigGen.getGenericSignature(BCodeHelpers.scala:400)
        at dotty.tools.backend.jvm.BCodeHelpers$BCJGenSigGen.getGenericSignature$(BCodeHelpers.scala:381)
        at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.getGenericSignature(BCodeSkelBuilder.scala:74)
        at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.initJClass(BCodeSkelBuilder.scala:273)
        at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.genPlainClass(BCodeSkelBuilder.scala:123)
        at dotty.tools.backend.jvm.CodeGen.genClass(CodeGen.scala:138)
        at dotty.tools.backend.jvm.CodeGen.genClassDef$1(CodeGen.scala:55)
        at dotty.tools.backend.jvm.CodeGen.genClassDefs$1(CodeGen.scala:101)
        at dotty.tools.backend.jvm.CodeGen.genClassDefs$1$$anonfun$1(CodeGen.scala:99)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
        at scala.collection.immutable.List.foreach(List.scala:333)
        at dotty.tools.backend.jvm.CodeGen.genClassDefs$1(CodeGen.scala:99)
        at dotty.tools.backend.jvm.CodeGen.genUnit(CodeGen.scala:104)
        at dotty.tools.backend.jvm.GenBCode.run(GenBCode.scala:76)
        at dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:327)
        at scala.collection.immutable.List.map(List.scala:246)
        at dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:331)
        at dotty.tools.backend.jvm.GenBCode.runOn(GenBCode.scala:87)
        at dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:246)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
        at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1321)
        at dotty.tools.dotc.Run.runPhases$1(Run.scala:262)
        at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:270)
        at dotty.tools.dotc.Run.compileUnits$$anonfun$adapted$1(Run.scala:279)
        at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:67)
        at dotty.tools.dotc.Run.compileUnits(Run.scala:279)
        at dotty.tools.dotc.Run.compileSources(Run.scala:194)
        at dotty.tools.dotc.Run.compile(Run.scala:179)
        at dotty.tools.dotc.Driver.doCompile(Driver.scala:37)
        at dotty.tools.dotc.Driver.process(Driver.scala:197)
        at dotty.tools.dotc.Driver.process(Driver.scala:165)
        at dotty.tools.dotc.Driver.process(Driver.scala:177)
        at dotty.tools.dotc.Driver.main(Driver.scala:207)
        at dotty.tools.dotc.Main.main(Main.scala)
Error while emitting /Users/grzegorzbielski/projects/my/dotty-error-delete-me/Main.scala
assertion failed: Unexpected alias type: type F
1 error found
Compilation failed
@sjrd
Copy link
Member

sjrd commented Oct 30, 2023

Very slightly minimized (no implicit), and with different names for F in Arb (Fx) than in def pf (Fy). The crash is about Fy:

trait Arb[Fx[_]]:
  def pure[A](x: A): Fx[A]

class PfOps(self: Int) extends AnyVal {
  def pf[Fy[_]](m: Arb[Fy]): PartialFunction[Int, Fy[Int]] = {
    case x => m.pure(x)
  }
}

This happens during the back-end, when generating Java generic signatures. I suspect it's trying to compute the type of a synthetic method that lacks proper erasure, for some reason.

@nicolasstucki nicolasstucki added area:erasure and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 30, 2023
@sjrd sjrd self-assigned this Oct 30, 2023
sjrd added a commit to dotty-staging/dotty that referenced this issue Nov 9, 2023
Contrary to what an earlier comment said, we do emit HK type
parameters in Java signatures. They are always unbounded and never
the type of values.

However, they can appear as type arguments to other higher-kinded
types. Previously, an assertion error would trigger in that
situation. We relax the assertion to allow this situation and
emit a correct Java signature.

I manually verified that the generated Java signatures are
consistent with what Scala 2 emits for the same code snippet.
@sjrd sjrd linked a pull request Nov 9, 2023 that will close this issue
sjrd added a commit that referenced this issue Nov 9, 2023
Contrary to what an earlier comment said, we do emit HK type parameters
in Java signatures. They are always unbounded and never the type of
values.

However, they can appear as type arguments to other higher-kinded types.
Previously, an assertion error would trigger in that situation. We relax
the assertion to allow this situation and emit a correct Java signature.

I manually verified that the generated Java signatures are consistent
with what Scala 2 emits for the same code snippet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants