Skip to content

Various small improvements to prepare for transparent methods #4620

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 17 commits into from
Jun 6, 2018

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Jun 5, 2018

Supersedes #4589.

This PR contains some fixes accumulated while working on inlining untyped trees. It does not contain any code to inline, nor pickle untyped trees. We are currently considering an alternative design by @gsps and @OlivierBlanvillain which would not need to do this.

Main changes:

  • Regularization of modifier structure: Mods no longer add any new information, are kept
    only as carriers of positions for syntactic analysis in IDE and elsewhere.
  • Improvements in debug and printing options.
  • Drop inline as a modifier for function parameters, use by-name instead.
  • Fixes to inliner to make the previous change work, and in general to do a better job
    reducing the amount of code that's generated.

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! ☀️

private def paramBindingDef(name: Name, paramtp: Type, arg: Tree,
bindingsBuf: mutable.ListBuffer[ValOrDefDef]): ValOrDefDef = {
val argtpe = arg.tpe.dealias
def isByName = paramtp.dealias.isInstanceOf[ExprType]
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be a val. It is always evaluated

(flags, annots.reverse, privateWithin)
}

private val readAnnot: Context => Annotation = {
Copy link
Member

Choose a reason for hiding this comment

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

Why a val and not a def?

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 use it as a closure everytime we read a symbol. Making it a val avoids a lot of allocations.

super.typedApply(tree, pt)
override def typedApply(tree: untpd.Apply, pt: Type)(implicit ctx: Context) = {

def betaReduce(tree: Tree) = tree match {
Copy link
Member

Choose a reason for hiding this comment

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

Would be nice to document this method since it's non-trivial and the name is a bit vague.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought beta-reduce is as specific as it gets? 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just joking. I agree the details of this should be documented.

override def typedApply(tree: untpd.Apply, pt: Type)(implicit ctx: Context) = {

def betaReduce(tree: Tree) = tree match {
case Apply(Select(cl @ closureDef(ddef), nme.apply), args) =>
Copy link
Member

Choose a reason for hiding this comment

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

If the closure is a SAM type then there is no guarantee that the implement method is called apply, in fact it's possible that apply is a completely different method that has nothing to do with ddef so this optimization is incorrect in general, e.g.:

trait Foo {
  def apply(x: Int): Int = 42
  def realApply(x: Int): Int
}
object Test {
  def main(args: Array[String]): Unit = {
    println(((x => x): Foo).apply(10))
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right. We need to restrict this to function closures.

@odersky odersky requested a review from allanrenucci June 5, 2018 19:41
@odersky odersky requested a review from liufengyun June 5, 2018 19:42
odersky added 15 commits June 6, 2018 10:28
Use combination of Enum and Case instead.
It's useful to know something was an enum when it started, and it's necessary to pickle
this when serializing untyped trees.
 - Mark splices in code under -print-debug
 - Print owners of all definitions under -print-debug-owners
 - some fixes to format docs
 - some refactorings for better legibility
Everything should be reflected in flags and privateWithin already.
Avoid creation of `op1` methods if tracing is not enabled.
Rely on call-by-name parameters instead. Fix "unused defs" logic so
that referred-to cbn parameters are not eliminated.
Since all Mod values are now reflected in flags and privateWithin, there is
no need anymore to test whether a Mod exists.
@odersky odersky force-pushed the prepare-transparent branch from c169f27 to cd3e7e3 Compare June 6, 2018 08:30
Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

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

A small comment about doc, otherwise, LGTM

@@ -563,6 +563,9 @@ object Flags {
/** An inline parameter */
final val InlineParam = allOf(Inline, Param)

/** An enum case */
final val EnumCase = allOf(Enum, Case)
Copy link
Contributor

Choose a reason for hiding this comment

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

The documentation for Enum needs to be updated:

   /** Symbol is a Java enum */
   final val Enum = commonFlag(40, "<enum>")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's done in the latest commit.

Also, polishing and new test
@odersky odersky force-pushed the prepare-transparent branch from cd3e7e3 to d2c19ce Compare June 6, 2018 09:13
@odersky odersky merged commit efb8115 into scala:master Jun 6, 2018
@allanrenucci allanrenucci deleted the prepare-transparent branch June 6, 2018 12:12
* refs among the ei's directly without creating an intermediate binding.
*/
def betaReduce(tree: Tree) = tree match {
case Apply(Select(cl @ closureDef(ddef), nme.apply), args) if defn.isFunctionType(cl.tpe) =>
Copy link
Member

Choose a reason for hiding this comment

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

defn.isFunctionType(cl.tpe)

A more efficient way to check this is cl.tpt.isEmpty

@smarter
Copy link
Member

smarter commented Jun 6, 2018

Too late for this PR now, but I think we really need more tests to make sure inline behave as we think it does (to not repeat issues such as #4614). We currently only have one test in https://github.com/lampepfl/dotty/blob/master/compiler/test/dotty/tools/backend/jvm/InlineBytecodeTests.scala but it's fairly easy to add more.

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.

5 participants