Skip to content
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

nullability flow typing ignores Predef.eq/ne #18282

Closed
olhotak opened this issue Jul 24, 2023 · 1 comment · Fixed by #18299
Closed

nullability flow typing ignores Predef.eq/ne #18282

olhotak opened this issue Jul 24, 2023 · 1 comment · Fixed by #18299
Assignees
Labels
area:nullability itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label
Milestone

Comments

@olhotak
Copy link
Contributor

olhotak commented Jul 24, 2023

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

@olhotak olhotak added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label area:nullability labels Jul 24, 2023
@olhotak
Copy link
Contributor Author

olhotak commented Jul 24, 2023

I tried adding a search for this pattern to the CompareNull extractor, but got stuck. To determine whether the operation is eq or ne, we need to resolve the symbol, but that results in an overload resolution ambiguity, presumably because of not enough context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:nullability itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants