Skip to content

Fix #9740: harden type checking for pattern match on objects #11327

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

Merged
merged 3 commits into from
Feb 23, 2021

Conversation

liufengyun
Copy link
Contributor

@liufengyun liufengyun commented Feb 5, 2021

Fix #9740: harden type checking for pattern match on objects

We enforce that when pattern match on an object, the object should be
a subtype of the scrutinee type. It aligns with Scala 2 behavior.

Reasons for doing so:

  • such code patterns usually implies hidden errors in the code
  • it's always safe/sound to reject the code

We could check whether equals is overridden in the object, but

  • it complicates the protocol
  • overriding equals of object is also a bad practice
  • there is no sign that the slightly improved completeness is useful

@liufengyun liufengyun force-pushed the fix-9740b branch 2 times, most recently from 2c72a6c to ea11087 Compare February 5, 2021 14:37
@liufengyun liufengyun marked this pull request as ready for review February 5, 2021 14:37
@liufengyun
Copy link
Contributor Author

test performance please

@dottybot
Copy link
Member

dottybot commented Feb 5, 2021

performance test scheduled: 13 job(s) in queue, 1 running.

@dottybot
Copy link
Member

dottybot commented Feb 5, 2021

@liufengyun liufengyun requested a review from odersky February 8, 2021 12:36

// Case 3: Same as above; should match the second case and print the length of the string
any match {
case C(IsInt, i) if i < 10 => println(s"An Int less than 10")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no checkfile for neg tests?? Sounds very unstable to not verify the failure reasons...

I wanted to see on what grounds this now fails to compile.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We tend to avoid check files as they incur maintenance overhead. In contrast, we check the line numbers of reported errors agree with the comments // error in the source code.

dwijnand
dwijnand previously approved these changes Feb 8, 2021
Copy link
Member

@dwijnand dwijnand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with 1 jaw-dropping realisation...

We enforce that when pattern match on an object, the object should be
a subtype of the scrutinee type.

Reasons for doing so:

- such code patterns usually implies hidden errors in the code
- it's always safe/sound to reject the code

We could check whether `equals` is overridden in the object, but

- it complicates the protocol
- overriding `equals` of object is also a bad practice
- there is no sign that the slightly improved completeness is useful
mapOver(tp)
}

if tree.symbol.is(Module)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to check this also for other values that are not modules?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot check non-module values due to aliasing.

@odersky odersky assigned liufengyun and unassigned odersky Feb 23, 2021
@liufengyun liufengyun merged commit e90c953 into scala:master Feb 23, 2021
@liufengyun liufengyun deleted the fix-9740b branch February 23, 2021 18:29
liufengyun added a commit to dotty-staging/dotty that referenced this pull request Feb 24, 2021
liufengyun added a commit to dotty-staging/dotty that referenced this pull request Feb 24, 2021
This is a follow-up of scala#11327. We cannot generalize further without
complications:

```
val b = 'b'

56 match
case 'a' =>
case `b` =>
```
liufengyun added a commit to dotty-staging/dotty that referenced this pull request Mar 5, 2021
This is a follow-up of scala#11327. We cannot generalize further without
complications:

```
val b = 'b'

56 match
case 'a' =>
case `b` =>
```
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No warning/error when matching unrelated object types
5 participants