Skip to content

unsafeNulls import doesn't work properly for java methods with generic return type #18775

@rochala

Description

@rochala

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions