Skip to content

Fix #2570: Part 3 - The great () insert #2716

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 19 commits into from
Jun 23, 2017
Merged

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Jun 8, 2017

Require explicit () for nullary method applications. Fix a lot of files that were in violation.

This is the final part of addressing #2570.

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 ("Added" instead of "Add")
  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! ☀️

@odersky
Copy link
Contributor Author

odersky commented Jun 8, 2017

Migration strategy: We don't complain if the function was compiled by Scala 2 (this is because the Scala libraries need to be cleaned up first, so that they have () in the right places.).

We issue a migration warning under -language:Scala2, and fix the problem under -rewrite. We
issue an error otherwise.

So, to migrate code to Dotty, just compile it with

dotc <files> -language:Scala2 -rewrite

It will insert the missing () arguments. ScalaFix should do the same. /cc @olafurpg.

def paramIndices(param: untpd.ValDef, args: List[untpd.Tree], start: Int): List[Int] = args match {
case arg :: args1 =>
if (refersTo(arg, param))
if (paramIndices(param, args1, start + 1).isEmpty) start :: Nil
Copy link
Contributor

@senia-psm senia-psm Jun 8, 2017

Choose a reason for hiding this comment

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

This method returns a singleton list if param appears in args any odd number of times, not only "exactly one".
Please see this code snippet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch.

@odersky
Copy link
Contributor Author

odersky commented Jun 8, 2017

We seem to have an error early on in the CI. The building dotc script fails with mysterious errors.

@olafurpg
Copy link
Contributor

olafurpg commented Jun 8, 2017

@odersky Noted. scalacenter/scalafix#204

@smarter
Copy link
Member

smarter commented Jun 9, 2017

This PR needs to be rebased because scala-backend was updated in #2719

@odersky
Copy link
Contributor Author

odersky commented Jun 9, 2017

We also need to tighten the rules about overriding. It should be no longer legal to override def f(): T with def f: T and vice versa, except if one of the definitions is in Java or Scala 2 code.

@odersky odersky force-pushed the fix-#2570-2 branch 2 times, most recently from a78a559 to eedb461 Compare June 13, 2017 13:05
* apply of a function class? (implicit functions are excluded)
*/
def isSyntheticApply(tree: Tree): Boolean = tree match {
case Select(qual, nme.apply) => tree.pos.end == qual.pos.end
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems dangerous to me that the logic completely depends on positions -- if we have macros or synthetic trees from other sources, this assumption no longer holds.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but even for macro-generated apply's I would assume that we should not eta-expand (since the eta-expansion would simply undo what was generated).

matchLoosely && {
val this1 = widenNullary(this)
val that1 = widenNullary(that)
((this1 `ne` this) || (that1 `ne` that)) && this1.overrides(this1, matchLoosely = false)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why use backquotes around ne?

odersky added 16 commits June 23, 2017 11:07
Follows the scheme outlined in scala#2570. The reason not to flag bad calls to
methods compiled from Scala 2 is that some Scala 2 libraries are not yet in
a form where the parentheses are as they should be. For instance

    def isWhole()

in some of the numeric libraries should not have the ().
Fix errors everywhere where a nullary method is called
without a () argument.
Fix situations where a ()T method overrides a => T one, and vice versa.
The reason to do this only under -strict mode for now is so that
we can still compile the same code under both Scala 2 and Dotty.
If we rejected trailing `_` outright, Scala 2 code would need to
get either explicitly eta-expanded or get an expected type to still work.
But in Dotty, we can simply drop `_` for functions with arity >= 1.
So to keep code dual compilable, we'd need to add boilerplate
which under Dotty would be no longer needed.
Add sections to reference that explain the new
rules for eta expansion and auto-application.

Existing Scala code with inconsistent parameters can still be compiled
in Dotty under `-language:Scala2`. When paired with the `-rewrite`
option, the code will be automatcally rewritten to conforrm to Dotty's
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo: conforrm -> conform.

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.

6 participants