scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Error on `sun.*` APIs when `-release` Java version is lower than the execution Java version #12866

Closed LuciferYang closed 1 year ago

LuciferYang commented 1 year ago

Reproduction steps

Scala version: 2.13.11, 2.13.12 Java version: 17.0.8, 21-ea

package personal.code

import sun.util.calendar.ZoneInfo

import java.util.{Date, TimeZone}

object DateTimeUtils {

  def rebaseGregorianToJulianDays(days: Int): Int = 100

  def toJavaDate(days: Int): Date = {
    val rebasedDays = rebaseGregorianToJulianDays(days)
    val localMillis = Math.multiplyExact(rebasedDays, 86400000)
    val timeZoneOffset = TimeZone.getDefault match {
      case zoneInfo: ZoneInfo => zoneInfo.getOffsetsByWall(localMillis, null)
      case timeZone: TimeZone => timeZone.getOffset(localMillis - timeZone.getRawOffset)
    }
    new Date(localMillis - timeZoneOffset)
  }
}

Problem

Run command with Java 17 & 21-ea

scalac personal/code/DateTimeUtils.scala -release 11 

or run command with Java 21-ea

scalac personal/code/DateTimeUtils.scala -release 17 

There are compilation errors:

personal/code/DateTimeUtils.scala:3: error: object util is not a member of package sun
import sun.util.calendar.ZoneInfo
           ^
personal/code/DateTimeUtils.scala:15: error: not found: type ZoneInfo
      case zoneInfo: ZoneInfo => zoneInfo.getOffsetsByWall(localMillis, null)
                     ^
personal/code/DateTimeUtils.scala:15: error: value getOffsetsByWall is not a member of java.util.TimeZone
      case zoneInfo: ZoneInfo => zoneInfo.getOffsetsByWall(localMillis, null)
                                          ^
3 errors

But if the command is changed to

scalac personal/code/DateTimeUtils.scala -target:11

or

scalac personal/code/DateTimeUtils.scala -release 17 // with Java 17
scalac personal/code/DateTimeUtils.scala -release 21 // with Java 21

the compilation will be successful.

LuciferYang commented 1 year ago

friendly ping @SethTisue for help

  1. I have not tested lower versions of Scala, but Scala 2.12.18 also gives a compilation error.
  2. The code uses a class from the sun.util package, this may be the key point.
LuciferYang commented 1 year ago

also cc @srowen @dongjoon-hyun to known, I'm doing some testing work for the Java and Scala upgrade for Spark 4.0 and encountered the above issue.

LuciferYang commented 1 year ago

hmm... It seems that the java.base module did not exports the sun.util.calendar package, so it can't access the corresponding API after adding the -release option? If that's the case, I'll close this issue, as it doesn't appear to be a bug.

lrytz commented 1 year ago

See https://github.com/scala/bug/issues/12643 and https://bugs.openjdk.org/browse/JDK-8206937

When using a new JDK (like 17) with -release 11, the classpath contains a special file (ct.sym) instead of the actual JDK jar. This file doesn't contain symbols for internal API.

SethTisue commented 1 year ago

duplicate of #12643. let's consolidate any discussion there

LuciferYang commented 1 year ago

Thanks @SethTisue @lrytz