scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.89k stars 1.06k forks source link

case class overriding a type causes sbt component used by compiler to throw stack overflow #22026

Open tribbloid opened 5 days ago

tribbloid commented 5 days ago

Compiler version

3.5.2

Minimized code


trait TypeBound {

  type Min
  type Max >: Min
}

object TypeBound {

  type Pinpoint = TypeBound { type Max = Min }
}

object HasPoly1 {

  trait Poly1[B <: TypeBound] {

    type Refined[Sub <: B]

    def refine[Sub <: B](sub: Sub): Refined[Sub]
  }

  object Poly1 {

    type Concrete = Poly1[? <: TypeBound.Pinpoint] // can only refine using a concrete type, not a type bound

    case class Example1() extends Poly1[TypeBound.Pinpoint] {

      case class Refined[Sub <: TypeBound.Pinpoint](sub: Sub) {

        final val fn: sub.Max => Seq[sub.Max] = { v =>
          Seq(v)
        }
      }

      def refine[Sub <: TypeBound.Pinpoint](sub: Sub): Refined[Sub] = Refined[Sub](sub)
    }
  }
}

Output

Cause: java.lang.StackOverflowError
    at sbt.internal.inc.binary.converters.ProtobufWriters.toStructure(ProtobufWriters.scala:377)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toType(ProtobufWriters.scala:444)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toProjection$1(ProtobufWriters.scala:393)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toType(ProtobufWriters.scala:449)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toTypeAlias$1(ProtobufWriters.scala:553)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toClassDefinition(ProtobufWriters.scala:580)
    at sbt.internal.inc.binary.converters.ProtobufWriters.$anonfun$toStructure$1(ProtobufWriters.scala:377)
    at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toStructure(ProtobufWriters.scala:377)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toType(ProtobufWriters.scala:444)
    at sbt.internal.inc.binary.converters.ProtobufWriters.$anonfun$toStructure$3(ProtobufWriters.scala:379)
    at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toStructure(ProtobufWriters.scala:379)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toType(ProtobufWriters.scala:444)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toProjection$1(ProtobufWriters.scala:393)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toType(ProtobufWriters.scala:449)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toTypeAlias$1(ProtobufWriters.scala:553)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toClassDefinition(ProtobufWriters.scala:580)
    at sbt.internal.inc.binary.converters.ProtobufWriters.$anonfun$toStructure$1(ProtobufWriters.scala:377)
    at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toStructure(ProtobufWriters.scala:377)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toType(ProtobufWriters.scala:444)
    at sbt.internal.inc.binary.converters.ProtobufWriters.$anonfun$toStructure$3(ProtobufWriters.scala:379)
    at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toStructure(ProtobufWriters.scala:379)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toType(ProtobufWriters.scala:444)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toProjection$1(ProtobufWriters.scala:393)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toType(ProtobufWriters.scala:449)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toTypeAlias$1(ProtobufWriters.scala:553)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toClassDefinition(ProtobufWriters.scala:580)
    at sbt.internal.inc.binary.converters.ProtobufWriters.$anonfun$toStructure$1(ProtobufWriters.scala:377)
    at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toStructure(ProtobufWriters.scala:377)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toType(ProtobufWriters.scala:444)
    at sbt.internal.inc.binary.converters.ProtobufWriters.$anonfun$toStructure$3(ProtobufWriters.scala:379)
    at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toStructure(ProtobufWriters.scala:379)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toType(ProtobufWriters.scala:444)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toProjection$1(ProtobufWriters.scala:393)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toType(ProtobufWriters.scala:449)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toTypeAlias$1(ProtobufWriters.scala:553)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toClassDefinition(ProtobufWriters.scala:580)
    at sbt.internal.inc.binary.converters.ProtobufWriters.$anonfun$toStructure$1(ProtobufWriters.scala:377)
    at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toStructure(ProtobufWriters.scala:377)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toType(ProtobufWriters.scala:444)
    at sbt.internal.inc.binary.converters.ProtobufWriters.$anonfun$toStructure$3(ProtobufWriters.scala:379)
    at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toStructure(ProtobufWriters.scala:379)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toType(ProtobufWriters.scala:444)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toProjection$1(ProtobufWriters.scala:393)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toType(ProtobufWriters.scala:449)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toTypeAlias$1(ProtobufWriters.scala:553)
    at sbt.internal.inc.binary.converters.ProtobufWriters.toClassDefinition(ProtobufWriters.scala:580)
    at sbt.internal.inc.binary.converters.ProtobufWriters.$anonfun$toStructure$1(ProtobufWriters.scala:377)
...

on scastie it will time out:

https://scastie.scala-lang.org/tribbloid/XNl7bfiyRpyqNzbhZOnI8w/2

Expectation

Simple program, should compile in 3 seconds. Maybe a hidden bug in Protobuf

Gedochao commented 5 days ago

Seems to compile excessively long, but succeed on my machine. Additionally, it seems to hang when compiled with Bloop. So something is definitely up.

Gedochao commented 5 days ago

Side note, compiling this with Scala CLI using the build server (Bloop) will make Bloop hang, forcing one to restart it. This is quite bad. 😐 cc @tgodzik

tgodzik commented 5 days ago

Might be oom before the stack overflow actually catches up.

tgodzik commented 5 days ago

Looks like compilation indeed hangs, but you don't have to kill Bloop, at least in my case it wasn't necessary.