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

Allow .asInstanceOf[Matchable] as this is erased to Object #1763

Open
seigert opened this issue May 18, 2023 · 1 comment
Open

Allow .asInstanceOf[Matchable] as this is erased to Object #1763

seigert opened this issue May 18, 2023 · 1 comment

Comments

@seigert
Copy link

seigert commented May 18, 2023

In Scala 3 declaration of equals is def equals(obj: Any): Boolean = ???. This means that any attempt to match on a class instance in equals body is met with

[error]    |                   pattern selector should be an instance of Matchable,,
[error]    |                   but it has unmatchable type Any instead

Official documentation suggest to fix this with obj.asInstanceOf[Matchable] as this is guaranteed to succeed.

But this triggers DisableSyntax.noAsInstanceOf rule for almost every custom .equals.

@bjaglin
Copy link
Collaborator

bjaglin commented Jun 2, 2023

Thanks for the report!

I am hesitant to add an exception that would effectively change the semantics of DisableSyntax.noAsInstanceOf, especially since forcing usage of asInstanceOf serves a purpose per the documentation:

The cast of that to Matchable serves as an indication that universal equality is unsafe in the presence of abstract types and opaque types since it cannot properly distinguish the meaning of a type from its representation

Instead, I am thinking of having a custom lint diagnostic when finding a .asInstanceOf[Matchable], perhaps referencing the documentation and/or suggesting to use asMatchable?

I'd be happy to review a PR! The change should be fairly easy here. The rule is syntactic so we can't be sure it's the right scala.Matchable type, but it's acceptable to have false positives I believe. A Scala 3 testkit input asserting the diagnostic (example) should be added to guard that new behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants