Skip to content

unsafeNulls doesn't allow T|Null when T required #15195

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

Open
olhotak opened this issue May 16, 2022 · 2 comments
Open

unsafeNulls doesn't allow T|Null when T required #15195

olhotak opened this issue May 16, 2022 · 2 comments

Comments

@olhotak
Copy link
Contributor

olhotak commented May 16, 2022

Compiler version

Scala compiler version 3.2.0-RC1-bin-SNAPSHOT-git-7fbbeef -- Copyright 2002-2022, LAMP/EPFL

Minimized code

Minimized from stdLib213/src/library/scala/collection/convert/JavaCollectionWrappers.scala

import language.unsafeNulls

trait Wrapper[K,V](val map: java.util.Map[K,V]):
  def put(k: K, v: V): Option[V] = 
    val result = map.put(k, v)
    Some(result)

Compile with scalac -Yexplicit-nulls Wrapper.scala

Output

-- [E007] Type Mismatch Error: Wrapper.scala:6:9 ------------------------------------------------------------------------------------------
6 |    Some(result)
  |         ^^^^^^
  |         Found:    (result : V | Null)
  |         Required: V
  |
  | longer explanation available when compiling with `-explain`
1 error found

Expectation

Should compile.

If result is inlined as follows, it compiles:

    Some(map.put(k, v))
@olhotak
Copy link
Contributor Author

olhotak commented May 16, 2022

Related to #13837

@olhotak
Copy link
Contributor Author

olhotak commented May 16, 2022

I understand that this is as specified because V is not a subtype of AnyRef, and that the reason for the specification is to avoid accepting code that would be rejected without -Yexplicit-nulls, but this particular minimized example does compile without -Yexplicit-nulls.

Is there perhaps a variation of the specification that would allow this to compile? Perhaps replace V is a subtype of AnyRef with V is not a subtype of AnyVal?

There are other similar examples throughout the community build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants