scala / scala3

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

Consider nullable annotations in explicit nulls #21629

Open noti0na1 opened 6 days ago

noti0na1 commented 6 days ago

The @NotNull and @NonNullable type annotations are currently used by explicit nulls to not nullify (or add flexible type to) a reference type from Java signatures.

In addition to these annotations, I suggest we also consider the @Nullable annotation. It indicates a value can indeed be null. In this case, we will always nullify the type (adding | Null), instead of adding a flexible type.

// in Java
@Nullable String f(String s)

// in Scala with explicit nulls currently
def f(s: (String)?): (String)?

// in Scala with explicit nulls considering the annotation
def f(s: (String)?): String | Null

A list of @Nullable annotations we may want to consider:

javax.annotation.Nullable
org.jetbrains.annotations.Nullable
org.jspecify.annotations.Nullable
...
olhotak commented 6 days ago

There is already some support for this (or was it disabled at some point?). See NotNullAnnots in Definitions.scala and uses of that. def hasNotNullAnnot in JavaNullInterop.

Oh, sorry, I misunderstood.

noti0na1 commented 6 days ago

Not sure about the current status of Java ecosystem. I guess jspecify is the latest standard for this kind of annotations?

olhotak commented 6 days ago

Perhaps this could be a good introductory task for @HarrisL2

noti0na1 commented 6 days ago

This is a list of annotations considered by Kotlin: https://github.com/JetBrains/kotlin/blob/a9ff22693479cabd201909a06e6764c00eddbf7b/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/java/JvmAnnotationNames.kt

We should update our existing list as well.