Skip to content

infinite recursion in Scala.js using 3.0.0-M3 due to compiler error in dotty. #11064

Closed
@scalavision

Description

@scalavision

Minimized code

In a Scala.js project add the following code snippet :

import scala.scalajs.js
import scala.scalajs.js.typedarray._
import scala.scalajs.js.typedarray.TypedArrayBufferOps._
import java.nio.ByteBuffer

import java.nio._

object ArrayFailure{
  def test() = {
      val tempBuffer = ByteBuffer.allocateDirect(1000)
      tempBuffer.typedArray()
  }
}

Output

[error] -- Error: /../scala-js-dom/src/main/scala/org/scalajs/dom/ext/Extensions.scala:219:6 
[error] 219 |      tempBuffer.typedArray()
[error]     |      ^
[error]     |Recursion limit exceeded.
[error]     |Maybe there is an illegal cyclic reference?
[error]     |If that's not the case, you could also try to increase the stacksize using the -Xss JVM option.
[error]     |A recurring operation is (inner to outer):

[error]     |  subtype scala.scalajs.js.typedarray.TypedArray[?, 
[error]     |  LazyRef(
[error]     |    scala.scalajs.js.typedarray.TypedArrayBufferOps[?
[error]     |       <: scala.scalajs.js.typedarray.TypedArray[?, ?]
[error]     |    ]#TypedArrayType
[error]     |  )
[error]     |] <:< scala.scalajs.js.typedarray.TypedArray[?, ?]
[error]     |  subtype Nothing <:< LazyRef(
[error]     |  scala.scalajs.js.typedarray.TypedArrayBufferOps[?
[error]     |     <: scala.scalajs.js.typedarray.TypedArray[?, ?]
[error]     |  ]#TypedArrayType
[error]     |)
[error]     |  subtype scala.scalajs.js.typedarray.TypedArray[?, 
[error]     |  LazyRef(
[error]     |    scala.scalajs.js.typedarray.TypedArrayBufferOps[?
[error]     |       <: scala.scalajs.js.typedarray.TypedArray[?, ?]
[error]     |    ]#TypedArrayType
[error]     |  )
[error]     |]

Expectation

This should compile. Looking at several Scala.js libraries, building structures like done in the TypedArray seems to be used a lot.

Other comments

I encountered this when trying to update scalajs-dom library to Scala 3.0.0-M3, the working branch is here:

I tried to make a minimized example based on the code in Scala.js, not sure how accurate (or if it is even valid code), but this makes dotty compiler crash, while it compiles in Scala 2.

trait Buffer
trait TypedArray[T, Repr]

case class MyType() extends TypedArray[Int, String]

object TypedArrayBridge {
  def Buffer_typedArray(buffer: Buffer): TypedArray[_,_] = MyType()
}

final class TypedArrayBufferOps[
    TypedArrayType <: TypedArray[_, TypedArrayType]] private (
    private val buffer: Buffer)
    extends AnyVal {

  def typedArray(): TypedArrayType =
    TypedArrayBridge.Buffer_typedArray(buffer).asInstanceOf[TypedArrayType]
}

It is a best effort reimplementation of the code in Scala.js. I also encountered something that looks similar when trying to upgrade the scala-dom-types library. This does not compile:

// Does not compile
trait ErrorEventProps[EP[_ <: DomEvent], DomEvent, DomErrorEvent <: DomEvent] { this: EventPropBuilder[EP, DomEvent] =>
// Defining it like this compiles
trait ErrorEventProps[A, EP[A <: DomEvent], DomEvent, DomErrorEvent <: DomEvent] { this: EventPropBuilder[EP, DomEvent] =>

working on the branch here:

I will try to find time looking more deeply into the language specification, so that I can make these bug reports a bit more precise. Thank you very much for the very supportive feedback so far.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions