scala / bug

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

Exhaustivity checker emits spurious warning when matching on Java enum type (2.13.11 regression) #12800

Closed vasilmkd closed 1 year ago

vasilmkd commented 1 year ago

Reproduction steps

git clone git@github.com:vasilmkd/scala-java-enum-regression.git
cd scala-java-enum-regression
sbt compile

https://github.com/vasilmkd/scala-java-enum-regression

Scala version: 2.13.11

Problem (-Werror flag is on)

[error] <path>/scala-java-enum-regression/src/main/scala/main.scala:5:23: match may not be exhaustive.
[error] It would fail on the following inputs: (_ : com.intellij.openapi.compiler.CompilerMessageCategory$1), (_ : com.intellij.openapi.compiler.CompilerMessageCategory$2), (_ : com.intellij.openapi.compiler.CompilerMessageCategory$3), (_ : com.intellij.openapi.compiler.CompilerMessageCategory$4)
[error]     val messageType = category match {
[error]                       ^
[error] one error found

Further context

  1. Scala 2.13.10 does not exhibit this behavior.
  2. Compiling using Scala 3.3.0 and Scala 2.12.18 works fine.
  3. The warning/error is not issued if the same Java source code is copied into a .java file and compiled using sbt. It is necessary for this enum to be published in a library. (Might be a problem with classfile decompilation?)
som-snytt commented 1 year ago

A more complete repro would be helpful to me. At least a link to the java source? Maybe a simple enum suffices? or maybe not? Let me remember how to write an enum in Java, etc.

SethTisue commented 1 year ago

It is necessary for this enum to be published in a library

Perhaps it would be sufficient to use CompileOrder.JavaThenScala instead of CompileOrder.Mixed?

SethTisue commented 1 year ago

@som-snytt https://github.com/scala/scala/pull/10105 seems like an obvious place to look? any other ideas of what PR might be involved?

vasilmkd commented 1 year ago

Sorry, here's the source of the enum https://github.com/JetBrains/intellij-community/blob/master/java/compiler/openapi/src/com/intellij/openapi/compiler/CompilerMessageCategory.java.

som-snytt commented 1 year ago

@SethTisue yes with sinking heart that was also my first thought.

@vasilmkd thanks, I couldn't find it right away on github, and this is after my second coffee.

I see it's an "interesting" enum. So much to explore today, I hardly miss being at Scala Days. 😿

vasilmkd commented 1 year ago

I confirm, this is compiled using JDK 17, I haven't tried changing the JDK yet.

CompilerMessageCategory is not a "simple" Java enum, it defines an abstract method and then each enum case implements it.

A "simple" Java enum is for example com.intellij.compiler.ModuleSourceSet. Testing the same code by matching on that enum compiles just fine. I have pushed a new commit to the reproduction repo with this enum.

vasilmkd commented 1 year ago

I tried using JDK 11 and JDK 8, same result, the warning/error is reproducible as in JDK 17.

vasilmkd commented 1 year ago

@SethTisue You're right! Copying the enum source code to a Java file, then using compileOrder := CompileOrder.JavaThenScala does manage to reproduce the exhaustivity warning. I'm pushing a commit to the reproduction repo with that change, the library dependency is no longer necessary.