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

error during expansion of this match (this is a scalac bug) after upgrade from 2.13.10 to 2.13.11 #12814

Closed
danielleontiev opened this issue Jun 27, 2023 · 8 comments · Fixed by scala/scala#10626
Assignees
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) patmat
Milestone

Comments

@danielleontiev
Copy link

The following code causes compilation error under 2.13.11

File bug.sc

//> using scala "2.13.11"

sealed trait Common
sealed trait One extends Common
sealed trait Two extends Common


trait Module[C <: Common] {
  val name: String
  type Narrow = C
  def narrow: PartialFunction[Common, C]
}

object ModuleA extends Module[One] {
  val name = "A"
  val narrow: PartialFunction[Common, Narrow] = {
    case cc: Narrow => cc
  }
}

object ModuleB extends Module[ModuleA.Narrow] {
  val name = "B"
  val narrow: PartialFunction[Common, Narrow] = {
    case cc: Narrow => cc
  }
}

object ModuleC extends Module[Two] {
  val name = "C"
  val narrow: PartialFunction[Common, Narrow] = {
    case cc: Narrow => cc
  }
}

object ModuleD extends Module[One with Two] {
  val name = "D"
  val narrow: PartialFunction[Common, Narrow] = {
    case cc: Narrow => cc
  }
}

val one    = new One {}
val two    = new Two {}
val oneTwo = new One with Two {}

Seq(ModuleA, ModuleB, ModuleC, ModuleD).foreach { module =>
  println(s"${module.name} at One    = ${module.narrow.isDefinedAt(one)}")
  println(s"${module.name} at Two    = ${module.narrow.isDefinedAt(two)}")
  println(s"${module.name} at OneTwo = ${module.narrow.isDefinedAt(oneTwo)}")
  println("-" * 10)
}

Run with:

scala-cli bug.sc

Compiler error:

Compiling project (Scala 2.13.11, JVM)
[error] ./bug.sc:23:49
[error] error during expansion of this match (this is a scalac bug).
[error] The underlying error was: cyclic aliasing or subtyping involving type Narrow
[error]   val narrow: PartialFunction[Common, Narrow] = {
[error]                                                 ^
Error compiling project (Scala 2.13.11, JVM)
Compilation failed

It was not the case under 2.13.10, where code compiles successfully.

Run under 2.13.10, change the first line to

//> using scala "2.13.10"

and run as before

scala-cli bug.sc
@danielleontiev danielleontiev changed the title error during expansion of this match (this is a scalac bug) after upgrade from 2.13.10 to 2.13.11 error during expansion of this match (this is a scalac bug) after upgrade from 2.13.10 to 2.13.11 Jun 27, 2023
@lrytz
Copy link
Member

lrytz commented Jun 28, 2023

Caused by scala/scala#10247 (tested by reverting it on top of current 2.13.x)

@lrytz
Copy link
Member

lrytz commented Nov 22, 2023

Workaround: use the -Yrecursion 1 compiler flag. Default is 0.

@lrytz
Copy link
Member

lrytz commented Nov 22, 2023

Canonical example:

trait A[X] { type T = X }
object B extends A[String]
object C extends A[B.T] {
  def f: C.T = "hai"
}

The problem is in checkNonCyclic(C.T), which locks on the symbol for the T twice. The alias T is expanded twice (C.T => B.T => String) but in the second iteration it's already locked.

@lrytz lrytz added fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) and removed has PR regression labels Nov 22, 2023
@ghost ghost mentioned this issue Nov 23, 2023
@som-snytt
Copy link

It needs a label revertible to mean the behavior change was specific to a commit, while reserving regression for "specified" or "guaranteed" behaviors which changed in a release but need not be specific to a commit (or PR).

In addition, of course, having a workaround (of some sort) mitigates the need for a fix (which may cause revertible effects in turn).

And perhaps a more optimal fix will be forthcoming than could be achieved by the next release. A regression has an urgency attached to it that drags upon any alleviation. "Haste makes waste."

@som-snytt
Copy link

@abebeos I was suggesting why "regression" is not appropriate. You may disagree, of course. Such is the open in open source.

@som-snytt
Copy link

@abebeos there is no such protocol. I was trying to answer your question so Lukas wouldn't have to. You are free to be dissatisfied with my answer on its merits, of course. Either Lukas made a mistake in removing the regression label, or he was not mistaken but you are asking to prioritize this issue.

@lrytz
Copy link
Member

lrytz commented Nov 27, 2023

It's the canonical example in the sense that it shows the underlying problem, which indeed has been around for a long time (also 2.12).

My example fails in the type checking phase. The example given in this ticket fails in a later phase (patmat) which generates code similar to my example and then fails to type check it. So the change in patmat (scala/scala#10247) exposed the underlying issue in a new way, it did not cause it. That's why I removed the "regression" label, but one can argue either way.

@lrytz
Copy link
Member

lrytz commented Nov 27, 2023

It seems @retronym has a branch for it: #8252 (comment) (haven't looked, but it seems that).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) patmat
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants