Skip to content

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

Open
@symingz

Description

@symingz

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions