Skip to content

Type recursion not detected in opaque types, leads to genBCode crash #19372

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

Closed
WojciechMazur opened this issue Jan 4, 2024 · 2 comments · Fixed by #19453
Closed

Type recursion not detected in opaque types, leads to genBCode crash #19372

WojciechMazur opened this issue Jan 4, 2024 · 2 comments · Fixed by #19453

Comments

@WojciechMazur
Copy link
Contributor

WojciechMazur commented Jan 4, 2024

Compiler version

All Scala 3 versions

Minimized code

trait Ptr[T]

object aliases:
  import structs.*
  type UCharIteratorReserved = Ptr[UCharIterator]
  object UCharIteratorReserved: 
    def iterator: UCharIterator = ???

object structs:
  import aliases.{*, given}
  opaque type UCharIterator = Ptr[UCharIteratorReserved]
  object UCharIterator:
    def reservedFn: UCharIteratorReserved = ???

Output (click arrow to expand)

Error while emitting main.scala
java.lang.StackOverflowError while running genBCode on /Users/wmazur/projects/sandbox/main.scala
java.lang.StackOverflowError while compiling /Users/wmazur/projects/sandbox/main.scala
Exception in thread "main" java.lang.StackOverflowError
        at dotty.tools.dotc.core.Types$Type.dealias1(Types.scala:1384)
        at dotty.tools.dotc.core.Types$Type.dealias(Types.scala:1407)
        at dotty.tools.dotc.transform.GenericSignatures$.jsig$1(GenericSignatures.scala:238)
        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$.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)
       ...

Related issue

When types are defined in the same scope, the incremental compilation would fail

// Fails with    : `scala-cli compile main.scala`
// Compiles with : `scala-cli compile main.scala --server=false`

trait Ptr[T]
type UCharIteratorReserved = Ptr[UCharIterator]
opaque type UCharIterator = Ptr[UCharIteratorReserved]
Error: Encountered a StackOverflowError coming from the compiler. You might need to restart your Bloop build server:
dotty.tools.dotc.sbt.ExtractAPICollector.computeType(ExtractAPI.scala:466)
dotty.tools.dotc.sbt.ExtractAPICollector.apiType$$anonfun$1(ExtractAPI.scala:463)
scala.collection.mutable.HashMap.getOrElseUpdate(HashMap.scala:454)
dotty.tools.dotc.sbt.ExtractAPICollector.apiType(ExtractAPI.scala:463)
dotty.tools.dotc.sbt.ExtractAPICollector.processArg$1(ExtractAPI.scala:501)
dotty.tools.dotc.sbt.ExtractAPICollector.$anonfun$10(ExtractAPI.scala:505)
scala.collection.immutable.List.map(List.scala:246)
dotty.tools.dotc.sbt.ExtractAPICollector.computeType(ExtractAPI.scala:505)
dotty.tools.dotc.sbt.ExtractAPICollector.apiType$$anonfun$1(ExtractAPI.scala:463)
...
Error compiling project (Scala 3.3.0, JVM (17))
@WojciechMazur WojciechMazur added itype:bug itype:crash stat:needs triage Every issue needs to have an "area" and "itype" label labels Jan 4, 2024
@nicolasstucki nicolasstucki added area:backend area:opaque-types and removed area:backend stat:needs triage Every issue needs to have an "area" and "itype" label labels Jan 9, 2024
@nicolasstucki
Copy link
Contributor

Minimized

type A = List[bar.B]
def foo: A = ???
object bar:
  opaque type B = A

Alternative version that loops infinitely without crashing

type A = bar.B
def foo: A = ???
object bar:
  opaque type B = A

odersky added a commit to dotty-staging/dotty that referenced this issue Jan 15, 2024
Several improvements:

 - Follow opaque aliases as if they were aliases. Otherwise we
   risk crashing in phases where the `opaque` is dropped and these
   become regular aliases.
 - Update the `isInteresting` test to follow more types. Previously
   the test was too strict so some cycles were missed.
 - Make -explain-cyclic also explain for cycles detected by this check.

Fixes scala#19372
smarter added a commit that referenced this issue Jan 16, 2024
Several improvements:

- Follow opaque aliases as if they were aliases. Otherwise we risk
crashing in phases where the `opaque` is dropped and these become
regular aliases.
- Update the `isInteresting` test to follow more types. Previously the
test was too strict so some cycles were missed.
 - Make -explain-cyclic also explain for cycles detected by this check.

Fixes #19372
@Kordyjan Kordyjan added this to the 3.4.1 milestone Feb 14, 2024
WojciechMazur pushed a commit that referenced this issue Jun 27, 2024
Several improvements:

 - Follow opaque aliases as if they were aliases. Otherwise we
   risk crashing in phases where the `opaque` is dropped and these
   become regular aliases.
 - Update the `isInteresting` test to follow more types. Previously
   the test was too strict so some cycles were missed.
 - Make -explain-cyclic also explain for cycles detected by this check.

Fixes #19372

[Cherry-picked 756ae34]
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.

4 participants