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

Emit feature warnings for implicit conversions. #4229

Merged
merged 3 commits into from Apr 27, 2018

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Apr 2, 2018

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.

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.
Copy link
Member

@dottybot dottybot left a 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:

  1. Separate subject from body with a blank line
  2. When fixing an issue, start your commit message with Fix #<ISSUE-NBR>:
  3. Limit the subject line to 72 characters
  4. Capitalize the subject line
  5. Do not end the subject line with a period
  6. Use the imperative mood in the subject line ("Add" instead of "Added")
  7. Wrap the body at 80 characters
  8. 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
Copy link
Contributor

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?

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 don't want to force loading the symbol.

@odersky odersky merged commit fd7baff into scala:master Apr 27, 2018
@allanrenucci allanrenucci deleted the change-implicit-convs branch April 27, 2018 13:06
@Glavo
Copy link
Contributor

Glavo commented Jul 6, 2018

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

odersky added a commit to dotty-staging/dotty that referenced this pull request Feb 9, 2019
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.
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.

None yet

4 participants