-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Emit feature warnings for implicit conversions. #4229
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
Conversation
We now emit feature warnings when encountering an implicit conversion def, just like Scala 2 does. In addition, we also emit a feature warning when an implicit conversion is used, unless the conversion is an implicit class, or otherwise co-defined with the type to which it converts, or the conversion is predefined. Predefined are - all members of `Predef`, - the `scala.reflect.Selectable.reflectiveSelect` conversion. We might need to extend this to more conversions. The motivation for going down this route is the following: Implicit conversions are easily the most mis-used feature in Scala. If I should venture a guess I'd say that more than 90% of the uses are mis-guided. The danger of implicit conversions is that they seem simple. It's easy to grok what they are and in particular new users see the possibilities before realizing the downsides. So far we put definitions of implicit conversions under a feature flag but this is not enough. Library writers tend not to be deterred by it, they just add the language import. (Or, worse, IntelliJ does it for them. Dear IntelliJ Scala team: please change things so that language imports are not inserted automatically, They are required for a reason, and automatic insertion does not help!) Either way, it's the users of a library with implicit conversions that need to be warned by the compiler, since it is them who will run into troubles if implicit conversions have surprising behavior. In what concerns the precise rules, I believe we will have to try this out in the wild for a while to see how it shapes up and whether we should tweak the rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello, and thank you for opening this PR! 🎉
All contributors have signed the CLA, thank you! ❤️
Commit Messages
We want to keep history, but for that to actually be useful we have
some rules on how to format our commit messages (relevant xkcd).
Please stick to these guidelines for commit messages:
- Separate subject from body with a blank line
- When fixing an issue, start your commit message with
Fix #<ISSUE-NBR>:
- Limit the subject line to 72 characters
- Capitalize the subject line
- Do not end the subject line with a period
- Use the imperative mood in the subject line ("Add" instead of "Added")
- Wrap the body at 80 characters
- Use the body to explain what and why vs. how
adapted from https://chris.beams.io/posts/git-commit
Have an awesome day! ☀️
sym.is(Synthetic) || | ||
sym.info.finalResultType.classSymbols.exists(_.owner.isLinkedWith(sym.owner)) || | ||
defn.isPredefClass(sym.owner) || | ||
sym.name == nme.reflectiveSelectable && sym.maybeOwner.maybeOwner.maybeOwner == defn.ScalaPackageClass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not compare symbol
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want to force loading the symbol.
I don't think there is no need to warn the user when an implicit conversion function is imported separately. In this case, the user clearly expresses that he needs to use implicit conversion.. |
Implement feature warnings for implicit conversion use according to scala#4229 also for new style implicit conversions. Change the doc comment for languge.implicitConversion to explain why it is needed for use site conversions.
We now emit feature warnings when encountering an implicit conversion def,
just like Scala 2 does.
In addition, we also emit a feature warning when an implicit conversion is used,
unless the conversion is an implicit class, or otherwise co-defined with the type
to which it converts, or the conversion is predefined.
Predefined are
Predef
,scala.reflect.Selectable.reflectiveSelect
conversion.We might need to extend this to more conversions.
The motivation for going down this route is the following: Implicit conversions are easily the
most mis-used feature in Scala. If I should venture a guess I'd say that more than 90% of the
uses are mis-guided. The danger of implicit conversions is that they seem simple. It's easy
to grok what they are and in particular new users see the possibilities before realizing the
downsides. So far we put definitions of implicit conversions under a feature flag but this is
not enough. Library writers tend not to be deterred by it, they just add the language import.
(Or, worse, IntelliJ does it for them. Dear IntelliJ Scala team: please change things so that
language imports are not inserted automatically, They are required for a reason, and automatic
insertion does not help!)
Either way, it's the users of a library with implicit conversions that need to be warned
by the compiler, since it is them who will run into troubles if implicit conversions have
surprising behavior.
In what concerns the precise rules, I believe we will have to try this out in the wild for a
while to see how it shapes up and whether we should tweak the rules.