-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #6253: Handle repeated args in quoted patterns #6254
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
odersky
merged 2 commits into
scala:master
from
dotty-staging:add-quote-pattern-on-repeated-args
Apr 11, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package scala.quoted.matching | ||
|
||
import scala.quoted.Expr | ||
|
||
import scala.tasty.Reflection // TODO do not depend on reflection directly | ||
|
||
/** Matches a literal sequence of expressions */ | ||
object Repeated { | ||
|
||
def unapply[T](expr: Expr[Seq[T]])(implicit reflect: Reflection): Option[Seq[Expr[T]]] = { | ||
import reflect.{Repeated => RepeatedTree, _} // TODO rename to avoid clash | ||
def repeated(tree: Term): Option[Seq[Expr[T]]] = tree match { | ||
case Typed(RepeatedTree(elems, _), _) => Some(elems.map(x => x.seal.asInstanceOf[Expr[T]])) | ||
case Block(Nil, e) => repeated(e) | ||
case Inlined(_, Nil, e) => repeated(e) | ||
case _ => None | ||
} | ||
repeated(expr.unseal) | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import scala.quoted._ | ||
import scala.tasty.Reflection | ||
object Macros { | ||
def impl(self: Expr[StringContext]) given Reflection: Expr[String] = self match { | ||
case '{ StringContext() } => '{""} | ||
case '{ StringContext($part1) } => part1 | ||
case '{ StringContext($part1, $part2) } => '{ $part1 + $part2 } | ||
case '{ StringContext($parts: _*) } => '{ $parts.mkString } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Hello World | ||
Hello World |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import scala.quoted._ | ||
import scala.quoted.matching._ | ||
|
||
import scala.tasty.Reflection | ||
|
||
object Macros { | ||
|
||
inline def (self: => StringContext) xyz(args: => String*): String = ${impl('self, 'args)} | ||
|
||
private def impl(self: Expr[StringContext], args: Expr[Seq[String]])(implicit reflect: Reflection): Expr[String] = { | ||
self match { | ||
case '{ StringContext($parts: _*) } => | ||
'{ | ||
val p: Seq[String] = $parts | ||
val a: Seq[Any] = $args ++ Seq("") | ||
p.zip(a).map(_ + _.toString).mkString | ||
} | ||
case _ => | ||
'{ "ERROR" } | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Macros._ | ||
|
||
object Test { | ||
|
||
def main(args: Array[String]): Unit = { | ||
println(xyz"Hello World") | ||
println(xyz"Hello ${"World"}") | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Hello World | ||
Hello World |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import scala.quoted._ | ||
import scala.quoted.matching._ | ||
|
||
import scala.tasty.Reflection | ||
|
||
object Macros { | ||
|
||
inline def (self: => StringContext) xyz(args: => String*): String = ${impl('self, 'args)} | ||
|
||
private def impl(self: Expr[StringContext], args: Expr[Seq[String]])(implicit reflect: Reflection): Expr[String] = { | ||
self match { | ||
case '{ StringContext($parts: _*) } => | ||
'{ StringContext($parts: _*).s($args: _*) } | ||
case _ => | ||
'{ "ERROR" } | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Macros._ | ||
|
||
object Test { | ||
|
||
def main(args: Array[String]): Unit = { | ||
println(xyz"Hello World") | ||
println(xyz"Hello ${"World"}") | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
tests/run-with-compiler/quote-matcher-string-interpolator-2.check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dlroW olleH | ||
olleHWorld |
21 changes: 21 additions & 0 deletions
21
tests/run-with-compiler/quote-matcher-string-interpolator-2/quoted_1.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import scala.quoted._ | ||
import scala.quoted.matching._ | ||
|
||
import scala.tasty.Reflection | ||
|
||
object Macros { | ||
|
||
inline def (self: => StringContext) xyz(args: => String*): String = ${impl('self, 'args)} | ||
|
||
private def impl(self: Expr[StringContext], args: Expr[Seq[String]])(implicit reflect: Reflection): Expr[String] = { | ||
(self, args) match { | ||
case ('{ StringContext(${Repeated(parts)}: _*) }, Repeated(args1)) => | ||
val strParts = parts.map { case Literal(str) => str.reverse } | ||
val strArgs = args1.map { case Literal(str) => str } | ||
StringContext(strParts: _*).s(strArgs: _*).toExpr | ||
case _ => ??? | ||
} | ||
|
||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
tests/run-with-compiler/quote-matcher-string-interpolator-2/quoted_2.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Macros._ | ||
|
||
object Test { | ||
|
||
def main(args: Array[String]): Unit = { | ||
println(xyz"Hello World") | ||
println(xyz"Hello ${"World"}") | ||
} | ||
|
||
} |
2 changes: 2 additions & 0 deletions
2
tests/run-with-compiler/quote-matcher-string-interpolator.check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dlroW olleH | ||
olleHWorld |
21 changes: 21 additions & 0 deletions
21
tests/run-with-compiler/quote-matcher-string-interpolator/quoted_1.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import scala.quoted._ | ||
import scala.quoted.matching._ | ||
|
||
import scala.tasty.Reflection | ||
|
||
object Macros { | ||
|
||
inline def (self: => StringContext) xyz(args: => String*): String = ${impl('self, 'args)} | ||
|
||
private def impl(self: Expr[StringContext], args: Expr[Seq[String]])(implicit reflect: Reflection): Expr[String] = { | ||
self match { | ||
case '{ StringContext(${Repeated(parts)}: _*) } => | ||
val parts2 = parts.map(x => '{ $x.reverse }).toList.toExprOfList | ||
'{ StringContext($parts2: _*).s($args: _*) } | ||
case _ => | ||
'{ "ERROR" } | ||
} | ||
|
||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
tests/run-with-compiler/quote-matcher-string-interpolator/quoted_2.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Macros._ | ||
|
||
object Test { | ||
|
||
def main(args: Array[String]): Unit = { | ||
println(xyz"Hello World") | ||
println(xyz"Hello ${"World"}") | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The change here and above looks dubious to me. @odersky could you have a look?
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.
I am not sure a global substitution is the right thing here. I fear it would also affect nested varargs methods that would then become Seq methods. I'd do instead:
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.
I use this code now and also updated another place that had the
subst
to useunderlyingIfRepeated
.