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

Flow typing leads to incorrect "pattern type is incompatible with expected type" error #18083

Closed
smarter opened this issue Jun 27, 2023 · 0 comments · Fixed by #18093
Closed

Comments

@smarter
Copy link
Member

smarter commented Jun 27, 2023

Compiler version

3.3.1-RC1 with -Yexplicit-nulls

Minimized code

sealed trait A
case class Sub1() extends A
case object Sub2 extends A

def test(x: A | Null): Int =
  if x == null then return 0
  x match
    case Sub1() => 1
    case Sub2 => 2

Output

-- [E007] Type Mismatch Error: try/ft.scala:9:9 --------------------------------
9 |    case Sub2 => 2
  |         ^^^^
  |         Found:    Sub2.type
  |         Required: (x : A | Null) & A
  |         pattern type is incompatible with expected type
  |
  | longer explanation available when compiling with `-explain`

Expectation

No error since Sub2 is a valid subtype of A. The problem is that the selector x gets typed as x.type & A instead of x.type (in toNotNullTermRef) which means the call to .widen in https://github.com/lampepfl/dotty/blob/484be60846be17acee74cfe1c6ea16c10894303f/compiler/src/dotty/tools/dotc/typer/Typer.scala#L1750 doesn't actually widen the singleton.

odersky added a commit to dotty-staging/dotty that referenced this issue Jun 28, 2023
Improve logic when to emit "pattern type is incompatible with expected type"
error.

Fixes scala#18083

The whole thing is not very satisfactory. We have an approximation which has both
false positives and false negatives.

False positives: We are too lenient for numeric types and and/or types
False negatives: We ignore user-generated equals methods

The new approximation seems to be somewhat better than the old, but it's still an
approximation. It begs the question why we attempt to do this at all.
odersky added a commit to dotty-staging/dotty that referenced this issue Jun 28, 2023
Improve logic when to emit "pattern type is incompatible with expected type"
error.

Fixes scala#18083

The whole thing is not very satisfactory. We have an approximation which has both
false positives and false negatives.

False positives: We are too lenient for numeric types and and/or types
False negatives: We ignore user-generated equals methods

The new approximation seems to be somewhat better than the old, but it's still an
approximation. It begs the question why we attempt to do this at all.
odersky added a commit to dotty-staging/dotty that referenced this issue Jun 28, 2023
Improve logic when to emit "pattern type is incompatible with expected type"
error.

Fixes scala#18083

The whole thing is not very satisfactory. We have an approximation which has both
false positives and false negatives.

False positives: We are too lenient for numeric types and and/or types
False negatives: We ignore user-generated equals methods

The new approximation seems to be somewhat better than the old, but it's still an
approximation. It begs the question why we attempt to do this at all.
odersky added a commit to dotty-staging/dotty that referenced this issue Jun 29, 2023
Improve logic when to emit "pattern type is incompatible with expected type"
error.

Fixes scala#18083

The whole thing is not very satisfactory. We have an approximation which has both
false positives and false negatives.

False positives: We are too lenient for numeric types and and/or types
False negatives: We ignore user-generated equals methods

The new approximation seems to be somewhat better than the old, but it's still an
approximation. It begs the question why we attempt to do this at all.
odersky added a commit to dotty-staging/dotty that referenced this issue Jul 5, 2023
Improve logic when to emit "pattern type is incompatible with expected type"
error.

Fixes scala#18083

The whole thing is not very satisfactory. We have an approximation which has both
false positives and false negatives.

False positives: We are too lenient for numeric types and and/or types
False negatives: We ignore user-generated equals methods

The new approximation seems to be somewhat better than the old, but it's still an
approximation. It begs the question why we attempt to do this at all.
odersky added a commit that referenced this issue Jul 6, 2023
Improve logic when to emit "pattern type is incompatible with expected
type" error.

Fixes #18083

The whole thing is not very satisfactory. We have an approximation which
has both false positives and false negatives.

False positives: We are too lenient for numeric types and and/or types
False negatives: We ignore user-generated equals methods

The new approximation seems to be somewhat better than the old, but it's
still an approximation. It begs the question why we attempt to do this
at all.
@Kordyjan Kordyjan added this to the 3.4.0 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants