Skip to content

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

Open
@olhotak

Description

@olhotak

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))

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions