Skip to content

nullability flow typing ignores Predef.eq/ne #18282

@olhotak

Description

@olhotak

Compiler version

Scala compiler version 3.3.2-RC1-bin-SNAPSHOT-git-ce1ce99 -- Copyright 2002-2023, LAMP/EPFL

Minimized code

def f(s: String|Null): String = {
  if(s eq null) "foo" else s
}

def f2(s: String|Null): String = {
  if(s ne null) s else "foo"
}

Compile with scalac -Yexplicit-nulls.

Output

-- [E007] Type Mismatch Error: tests/explicit-nulls/pos/flow-predef-eq.scala:2:27 ---------------------------------------------------------
2 |  if(s eq null) "foo" else s
  |                           ^
  |                           Found:    (s : String | Null)
  |                           Required: String
  |
  | longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: tests/explicit-nulls/pos/flow-predef-eq.scala:6:16 ---------------------------------------------------------
6 |  if(s ne null) s else "foo"
  |                ^
  |                Found:    (s : String | Null)
  |                Required: String
  |
  | longer explanation available when compiling with `-explain`
4 errors found

Expectation

Compiles.

The problem is that since s is of type AnyRef | Null, the eq/ne are actually extension methods in Predef. The flow typing code looks for the pattern l.eq(r) and l.==(r) but not the more complicated (Predef.eq(l))(r).

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions