scala / scala3

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

inconsistent behavior regarding implicit conversion from `Int` to `AnyRef` vs `Int` to `AnyRef | Null` #21304

Open symingz opened 1 month ago

symingz commented 1 month ago

Compiler version

3.4.2

Minimized code

File1:

def foo(x: AnyRef): Unit =
  println(x)

@main def main(): Unit =
  foo(10)

File2:

def foo(x: AnyRef | Null): Unit =
  println(x)

@main def main(): Unit =
  foo(10)

Output

File1:

-- [E007] Type Mismatch Error: Main.scala:5:6 ----------------------------------
5 |  foo(10)
  |      ^^
  |Found:    (10 : Int)
  |Required: AnyRef
  |Note that implicit conversions were not tried because the result of an implicit conversion
  |must be more specific than AnyRef
  |
  | longer explanation available when compiling with `-explain`
1 error found

File2: passed compilation.

Expectation

File2 should fail compilation as well because AnyRef | Null is not more specific than AnyRef.

noti0na1 commented 1 month ago

I also test with explicit nulls. With and without the flag, the tests have the same behavior.