-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #8530: Support inline unapply #8542
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6fcd438
Fix #8530: Support inline unapply
nicolasstucki 181eaca
Use unapply tree fun as prefix
nicolasstucki 4f7f34c
Propagate targs to the unapply placeholder
nicolasstucki caff137
Remove placeholer inserted by inlined patterns
nicolasstucki 8b776bd
Factor out common β-reduction code
nicolasstucki aa755f5
Move inline unapply logic from Typer to Inliner
nicolasstucki efc258d
Allow more β-reductions
nicolasstucki 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
61 changes: 61 additions & 0 deletions
61
compiler/src/dotty/tools/dotc/transform/InlinePatterns.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,61 @@ | ||
package dotty.tools | ||
package dotc | ||
package transform | ||
|
||
import core._ | ||
import MegaPhase._ | ||
import Symbols._, Contexts._, Types._, Decorators._ | ||
import StdNames.nme | ||
import NameOps._ | ||
import Names._ | ||
import ast.Trees._ | ||
import ast.TreeTypeMap | ||
|
||
/** Rewrite an application | ||
* | ||
* {new { def unapply(x0: X0)(x1: X1,..., xn: Xn) = b }}.unapply(y0)(y1, ..., yn) | ||
* | ||
* where | ||
* | ||
* - the method is `unapply` or `unapplySeq` | ||
* - the method does not have type parameters | ||
* | ||
* to | ||
* | ||
* [xi := yi]b | ||
* | ||
* This removes placeholders added by inline `unapply`/`unapplySeq` patterns. | ||
*/ | ||
class InlinePatterns extends MiniPhase: | ||
import ast.tpd._ | ||
|
||
def phaseName: String = "inlinePatterns" | ||
|
||
// This phase needs to run after because it need to transform trees that are generated | ||
// by the pattern matcher but are still not visible in that group of phases. | ||
override def runsAfterGroupsOf: Set[String] = Set(PatternMatcher.name) | ||
|
||
override def transformApply(app: Apply)(using ctx: Context): Tree = | ||
if app.symbol.name.isUnapplyName && !app.tpe.isInstanceOf[MethodicType] then | ||
app match | ||
case App(Select(fn, name), argss) => | ||
val app1 = betaReduce(app, fn, name, argss.flatten) | ||
if app1 ne app then ctx.log(i"beta reduce $app -> $app1") | ||
app1 | ||
case _ => | ||
app | ||
else app | ||
|
||
private object App: | ||
def unapply(app: Tree): (Tree, List[List[Tree]]) = | ||
app match | ||
case Apply(App(fn, argss), args) => (fn, argss :+ args) | ||
case _ => (app, Nil) | ||
|
||
private def betaReduce(tree: Apply, fn: Tree, name: Name, args: List[Tree])(using ctx: Context): Tree = | ||
nicolasstucki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fn match | ||
case Block(TypeDef(_, template: Template) :: Nil, Apply(Select(New(_),_), Nil)) if template.constr.rhs.isEmpty => | ||
template.body match | ||
case List(ddef @ DefDef(`name`, _, _, _, _)) => BetaReduce(ddef, args) | ||
case _ => tree | ||
case _ => tree |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,26 @@ | ||
object MyBoooleanUnapply: | ||
inline def unapply(x: Int): Boolean = true | ||
|
||
object MyOptionUnapply: | ||
inline def unapply(x: Int): Option[Long] = Some(x) | ||
|
||
object MyUnapplyImplicits: | ||
inline def unapply(x: Int)(using DummyImplicit): Option[Long] = Some(x) | ||
|
||
object MyPolyUnapply: | ||
inline def unapply[T](x: T): Option[T] = Some(x) | ||
|
||
object MySeqUnapply: | ||
inline def unapplySeq(x: Int): Seq[Int] = Seq(x, x) | ||
|
||
object MyWhiteboxUnapply: | ||
transparent inline def unapply(x: Int): Option[Any] = Some(x) | ||
|
||
def test: Unit = | ||
val x = 5 match | ||
case MyBoooleanUnapply() => | ||
case MyOptionUnapply(y) => y: Long | ||
case MyUnapplyImplicits(y) => y: Long | ||
case MyPolyUnapply(a) => a: Int | ||
case MySeqUnapply(a, b) => (a: Int, b: Int) | ||
case MyWhiteboxUnapply(x) => x: Int |
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,5 @@ | ||
|
||
object Foo { | ||
inline def unapply(x: Any): Boolean = ??? | ||
inline def unapplySeq(x: Any): Seq[Any] = ??? | ||
} |
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,15 @@ | ||
object Test { | ||
|
||
class C(val x: Int, val y: Int) | ||
|
||
inline def unapply(c: C): Some[(Int, Int)] = Some((c.x, c.y)) | ||
|
||
} | ||
object Test2 { | ||
|
||
class C(x: Int, y: Int) | ||
|
||
inline def unapply(c: C): Option[(Int, Int)] = inline c match { | ||
case x: C => Some((1, 1)) | ||
} | ||
} |
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 @@ | ||
foo |
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,12 @@ | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
0 match | ||
case Succ(n) => ??? | ||
case _ => | ||
|
||
2 match | ||
case Succ(n) => assert(n == 1) | ||
} | ||
|
||
} |
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,8 @@ | ||
import scala.quoted._ | ||
|
||
object Succ: | ||
|
||
inline def unapply(n: Int): Option[Int] = ${ impl('n) } | ||
|
||
private def impl(n: Expr[Int])(using QuoteContext): Expr[Option[Int]] = | ||
'{ if $n == 0 then None else Some($n - 1)} |
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.compiletime.erasedValue | ||
|
||
class MyRegex[Pattern <: String & Singleton/*Literal constant*/]: | ||
inline def unapplySeq(s: CharSequence): Option[List[String]] = | ||
inline erasedValue[Pattern] match | ||
case "foo" => if s == "foo" then Some(Nil) else None | ||
case _ => valueOf[Pattern].r.unapplySeq(s) | ||
|
||
@main def Test: Unit = | ||
val myRegexp1 = new MyRegex["foo"] | ||
val myRegexp2 = new MyRegex["f(o+)"] | ||
"foo" match | ||
case myRegexp1() => // Match ok | ||
case myRegexp2(x) => ??? | ||
"foooo" match | ||
case myRegexp1() => ??? | ||
case myRegexp2(x) => | ||
assert(x == "oooo") |
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,5 @@ | ||
MyBoooleanUnapply | ||
2 | ||
3 | ||
(4,5) | ||
5 |
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,33 @@ | ||
object MyBoooleanUnapply: | ||
inline def unapply(x: Int): Boolean = true | ||
|
||
object MyOptionUnapply: | ||
inline def unapply(x: Int): Option[Long] = Some(x) | ||
|
||
object MyPolyUnapply: | ||
inline def unapply[T](x: T): Option[T] = Some(x) | ||
|
||
object MySeqUnapply: | ||
inline def unapplySeq(x: Int): Seq[Int] = Seq(x, x + 1) | ||
|
||
object MyWhiteboxUnapply: | ||
transparent inline def unapply(x: Int): Option[Any] = Some(x) | ||
|
||
|
||
@main def Test = | ||
1 match | ||
case MyBoooleanUnapply() => println("MyBoooleanUnapply") | ||
|
||
2 match | ||
case MyOptionUnapply(y) => println(y) | ||
|
||
3 match | ||
case MyPolyUnapply(a) => println(a) | ||
|
||
4 match | ||
case MySeqUnapply(a, b) => println((a, b)) | ||
|
||
5 match | ||
case MyWhiteboxUnapply(x) => println(x: Int) | ||
|
||
end Test |
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.
Uh oh!
There was an error while loading. Please reload this page.