Skip to content

Fix #4732: Root the desugared call to StringContext #4779

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 2 commits into from
Jul 10, 2018

Conversation

lloydmeta
Copy link
Contributor

Closes #4732

  • Make desugared s"" call _root_.scala.StringContext instead of just
    StringContext
  • Adds a test

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

@lloydmeta lloydmeta force-pushed the feature/rooted-stringcontext branch 2 times, most recently from 1ac8205 to 32c82b5 Compare July 8, 2018 04:30
* Make desugared `s""` call `_root_.scala.StringContext` instead
  of just `StringContext` to fix the initial issue
* Add a unit test to lock in the change
* Update StringInterpolatorOpt#unapply so that the StringInterpolator
  optimistation can work with the new change
@lloydmeta lloydmeta force-pushed the feature/rooted-stringcontext branch from 6e5ebce to 426011b Compare July 8, 2018 04:58
@allanrenucci allanrenucci self-assigned this Jul 8, 2018
val scalaPkg = Select(rootPkg, nme.scala_)
Select(scalaPkg, nme.StringContext)
}
Apply(Select(Apply(rootedStringContext, strs), id), elems)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe rootedStringContext can be replaced with ref(defn.StringContextModule)

Copy link
Contributor

Choose a reason for hiding this comment

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

No, Desugar produces untyped trees

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I missed that. Indore my comment

val rootedStringContext = {
val rootPkg = Ident(nme.ROOTPKG)
val scalaPkg = Select(rootPkg, nme.scala_)
Select(scalaPkg, nme.StringContext)
Copy link
Member

Choose a reason for hiding this comment

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

As a shortcut, you can use scalaDot(nme.StringContext)

val scalaPkg = Select(rootPkg, nme.scala_)
Select(scalaPkg, nme.StringContext)
}
Apply(Select(Apply(rootedStringContext, strs), id), elems)
Copy link
Contributor

Choose a reason for hiding this comment

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

As suggested by @smarter, you can rewrite as

Apply(Select(Apply(scalaDot(nme.StringContext), strs), id), elems)

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

@@ -36,7 +36,7 @@ class StringInterpolatorOpt extends MiniPhase {
def unapply(tree: Select)(implicit ctx: Context): Boolean = {
tree.symbol.eq(defn.StringContextModule_apply) && {
val qualifier = tree.qualifier
qualifier.isInstanceOf[Ident] && qualifier.symbol.eq(defn.StringContextModule)
qualifier.isInstanceOf[Select] && qualifier.symbol.eq(defn.StringContextModule)
Copy link
Contributor

Choose a reason for hiding this comment

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

Thinking about it, the first test might not be needed. The method could be rewritten as:

def unapply(tree: Select)(implicit ctx: Context): Boolean =
  tree.symbol.eq(defn.StringContextModule_apply) &&
  tree.qualifier.symbol.eq(defn.StringContextModule)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, it seemed a bit fragile. Removed.


object Test {
def main(args: Array[String]): Unit = {
println(StringContext.s)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would remove the check file and simply do assert(StringContext.s == "Answer: 42")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

* Simplify grabbing of rooted handle to StringContext
* Simplify StringContext tree unapply
* Remove check file for test and use an assert instead
@lloydmeta
Copy link
Contributor Author

@allanrenucci Just addressed all your feedback + @smarter 's suggestions; please have another look when you can 😄

Copy link
Contributor

@allanrenucci allanrenucci left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks @lloydmeta!

@allanrenucci allanrenucci merged commit fe6ab99 into scala:master Jul 10, 2018
@lloydmeta lloydmeta deleted the feature/rooted-stringcontext branch July 10, 2018 11:39
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