Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip Nulls from Java members when unsafeNulls is enabled #13337

Merged
merged 3 commits into from Oct 5, 2021

Conversation

noti0na1
Copy link
Member

@noti0na1 noti0na1 commented Aug 19, 2021

The unsafeNulls feature makes Null a subtype of AnyRef in a certain scope.
In the scope, we can assign a nullable value with non-nullable type, or select members on it without .nn.

However, unsafeNulls still couldn't handle Java methods or fields if the type is a value type or a type variables.

J.java

public class J {
  public <T> T g() {
    return null;
  }
}

S.scala

import scala.language.unsafeNulls

val j: J = new J

// After JavaNullInterop, the signature of `J.g` becomes: `def g[T]: T | Null`

val s: String = j.g[String]() // ok
// because `String | Null` is not a subtype of `String`

val i: Int = j.g[Int]() // error
// the type of `g[Int]()` is `Int | Null`
// `Int | Null` is not a subtype of `Int` even if `Null` a subtype of `AnyRef`

def col[T]: T = j.g[T]() // error
// similarly,  we can't say `T| Null` is a subtype of `T`

The solution is to cast all Java applications and selections to non-nullable in adaptToSubtype when unsafeNulls is enabled. This will not affect Scala applications and selections.

The new test is in tests/explicit-nulls/unsafe-common/unsafe-java-call/

@noti0na1 noti0na1 requested a review from odersky August 19, 2021 11:55
@noti0na1 noti0na1 changed the title Strip Nulls from Java applications when unsafeNulls is enabled Strip Nulls from Java members when unsafeNulls is enabled Aug 20, 2021
@noti0na1
Copy link
Member Author

Hi @odersky , the PR passes all tests now. Could you review it?

Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@odersky odersky merged commit 9dd0968 into scala:master Oct 5, 2021
@odersky odersky deleted the unsafenulls-java-call branch October 5, 2021 16:00
@Kordyjan Kordyjan added this to the 3.1.1 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants