Skip to content

Scastie compiler crash #24521

@Bersier

Description

@Bersier

Compiler version

3.8.0-RC1

Minimized code

import scala.compiletime.constValue
import scala.compiletime.ops.int.+

def foo(): Unit =
  Bits.withAddedBit(Bits.Some(Bit.I, Bits.None), Bit.I)

sealed trait Bits
object Bits:
  type None = None.type
  case object None extends Bits
  final case class Some[+First <: Bit, Rest <: Bits](first: First, rest: Rest) extends Bits

  type WithAddedBit[B <: Bits, C <: Bit] <: Bits = B match
    case None => AsBits[C]
    case Some[f, r] => Bit.Sum2[f, C] match
      case (Bit.O, bit) => Some[bit, r]
      case (Bit.I, Bit.O) => Some[Bit.O, WithAddedBit[r, Bit.I]]

  inline def withAddedBit[B <: Bits, C <: Bit](b: B, c: C): WithAddedBit[B, C] = b match
    case _: None => asBits(c)
    case s: Some[?, ?] =>
      val (f, r) = s
      Bit.sum2(f, c) match
        case s2: (Bit.O, ?) =>
          val (Bit.O, bit) = s2
          Some(bit, r)
        case _: (Bit.I, Bit.O) => Some(Bit.O, withAddedBit(r, Bit.I))

  type AsBits[B <: Bit] <: Bits = B match
      case Bit.O => None
      case Bit.I => Some[Bit.I, None]

  inline def asBits[B <: Bit](b: B): AsBits[B] = b match
    case _: Bit.O => None
    case _: Bit.I => Some(Bit.I, None)
end Bits

sealed trait Bit derives CanEqual
object Bit:
  type O = O.type
  case object O extends Bit

  type I = I.type
  case object I extends Bit

  type Sum2[B1 <: Bit, B2 <: Bit] <: (Bit, Bit) = AsInt[B1] + AsInt[B2] match
    case 0 => (O, O)
    case 1 => (O, I)
    case 2 => (I, O)

  inline def sum2[B1 <: Bit, B2 <: Bit](t1: B1, t2: B2): Sum2[B1, B2] =
    constValue[AsInt[B1] + AsInt[B2]] match
      case _: 0 => (O, O)
      case _: 1 => (O, I)
      case _: 2 => (I, O)

  type AsInt[B <: Bit] <: Int = B match
    case O => 0
    case I => 1
end Bit

Scastie Compiler Output

  unhandled exception while running inlining on /tmp/scastie1192066731423425022/src/main/scala/main.scala

  An unhandled exception was thrown in the compiler.
  Please file a crash report here:
  https://github.com/scala/scala3/issues/new/choose
  For non-enriched exceptions, compile with -Xno-enrich-error-messages.


     while compiling: /tmp/scastie1192066731423425022/src/main/scala/main.scala
        during phase: inlining
                mode: Mode(ImplicitsEnabled)
     library version: version 3.8.0-RC1
    compiler version: version 3.8.0-RC1
            settings: -classpath /tmp/scastie1192066731423425022/target/scala-3.8.0-RC1/classes:/scastie-cache/coursier/https/repo1.maven.org/maven2/org/scala-lang/scala3-library_3/3.8.0-RC1/scala3-library_3-3.8.0-RC1.jar:/home/sbtRunnerContainer/.ivy2/local/org.scastie/runtime-scala_3/1.0.0-SNAPSHOT/jars/runtime-scala_3.jar:/scastie-cache/coursier/https/repo1.maven.org/maven2/org/scala-lang/scala-library/3.8.0-RC1/scala-library-3.8.0-RC1.jar -d /tmp/scastie1192066731423425022/target/scala-3.8.0-RC1/classes -deprecation true -feature true

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions