-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed as not planned
Closed as not planned
Copy link
Description
Compiler version
3.4.0-RC1-bin-20231024-15033c7-NIGHTLY
Java methods with generic return type fails when we return them inside anonymous functions and rely on type inference.
Minimized code
Test.scala
//> using scala 3.4.0-RC1-bin-20231024-15033c7-NIGHTLY
//> using option "-Yexplicit-nulls"
//> using file Test.java
import scala.language.unsafeNulls
object A {
protected def decodeJson[T](test: Test): Option[T] =
val x = Option(???).map(_ => test.fromJson[T]())
x
}
Test.java
class Test {
public <T> T fromJson() {
return null;
}
}
And it is working for:
//> using scala 3.4.0-RC1-bin-20231024-15033c7-NIGHTLY
//> using option "-Yexplicit-nulls"
//> using file Test.java
import scala.language.unsafeNulls
object A {
protected def decodeJson[T](test: Test): Option[T] =
Option(???).map(_ => test.fromJson[T]())
}
The origin problem was found in: https://github.com/scalameta/metals/blob/0d2739f6dfd96c455dadeca56dc85581e3bc48de/mtags-shared/src/main/scala/scala/meta/internal/mtags/CommonMtagsEnrichments.scala#L41-L60
Output
Compilation fails even with import scala.language.unsafeNulls
❯ scala-cli compile Test.scala Test.java
Compiling project (Scala 3.4.0-RC1-bin-20231024-15033c7-NIGHTLY, JVM)
[error] ./Test.scala:10:5
[error] Found: (x : Option[T | Null])
[error] Required: Option[T]
[error] x
[error] ^
Error compiling project (Scala 3.4.0-RC1-bin-20231024-15033c7-NIGHTLY, JVM)
Compilation failed
Expectation
It should compile.