scala / scala3

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

Usage of Scala library compiled with newer version of JDK leads to compiler crash #20491

Open WojciechMazur opened 4 months ago

WojciechMazur commented 4 months ago

Based on OpenCB failure in getkyo/kyo - build logs

When a Scala 3 library (ox) is published using JDK 21 and uses API introduced in this version, usage of this artifact in other project using lower JDK version (kyo) would lead to compiler crash. The crash message contains phrase the classfile defining the type might be missing from the classpath - however we should try to handle the crash using error reporting instead of uncontrolled failure. Scala 2.13 does not crash at compile time, but rather fails at runtime

Compiler version

All Scala 3 versions

Minimized code

// defs_1.scala
//> using jvm 21

package ox

import java.util.concurrent.StructuredTaskScope

trait Ox {
  def scope: StructuredTaskScope[Any]
}
// usage_2.scala
//> using jvm 8

given ox.Ox = ???

Output

> scala-cli compile *.scala --test -S 3 --server=false    
Warning: setting /Users/wmazur/projects/sandbox as the project root directory for this run.

  unhandled exception while running MegaPhase{elimErasedValueType, pureStats, vcElideAllocations, etaReduce, arrayApply, elimPolyFunction, tailrec, completeJavaEnums, mixin, lazyVals, memoize, nonLocalReturns, capturedVars} on /Users/wmazur/projects/sandbox/test.test.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 -Yno-enrich-error-messages.

     while compiling: /Users/wmazur/projects/sandbox/test.test.scala
        during phase: MegaPhase{elimErasedValueType, pureStats, vcElideAllocations, etaReduce, arrayApply, elimPolyFunction, tailrec, completeJavaEnums, mixin, lazyVals, memoize, nonLocalReturns, capturedVars}
                mode: Mode(ImplicitsEnabled)
     library version: version 2.13.12
    compiler version: version 3.4.2
            settings: -classpath /Users/wmazur/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala3-library_3/3.4.2/scala3-library_3-3.4.2.jar:/Users/wmazur/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.12/scala-library-2.13.12.jar:/Users/wmazur/projects/sandbox/.scala-build/sandbox_81ef297d4b_37166ea454-6e667bf95c/classes/main:/Users/wmazur/projects/sandbox/.scala-build/sandbox_81ef297d4b_37166ea454-6e667bf95c/classes/main -d /Users/wmazur/projects/sandbox/.scala-build/sandbox_81ef297d4b_37166ea454-6e667bf95c/classes/test -sourceroot /Users/wmazur/projects/sandbox

cannot resolve reference to type java.util.concurrent.type.StructuredTaskScope
the classfile defining the type might be missing from the classpath
Compilation failed

Expectation

Compiler should provide meaningful message about incompatible JDK. In Scala 2.13 it compiles but fails at runtime.

SethTisue commented 4 months ago

It does seem surprising that it wouldn't complain that the bytecode version is too high.

But it's also surprising that Scala 2 doesn't complain. This is speculation, but I wonder if it has to do with Scala 2 being intentionally tolerant of missing indirect dependencies, as per SCP-009.