forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix inline match on blocks with multiple statements (scala#20125)
Only the last expression of the block is considered as the inlined scrutinee. Otherwise we may not reduce as much as we should. We also need to make sure that side effects and bindings in the scrutinee are not duplicated. Inlined are converted into blocks to be able to apply the previous semantics without breaking the tree source files. Fixes scala#18151
- Loading branch information
Showing
5 changed files
with
154 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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 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 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 @@ | ||
case class El[A](attr: String, child: String) | ||
|
||
transparent inline def inlineTest(): String = | ||
inline { | ||
val el: El[Any] = El("1", "2") | ||
El[Any](el.attr, el.child) | ||
} match | ||
case El(attr, child) => attr + child | ||
|
||
def test: Unit = inlineTest() |
This file contains 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 @@ | ||
case class El[A](val attr: String, val child: String) | ||
|
||
transparent inline def tmplStr(inline t: El[Any]): String = | ||
inline t match | ||
case El(attr, child) => attr + child | ||
|
||
def test: Unit = tmplStr { | ||
val el = El("1", "2") | ||
El[Any](el.attr, null) | ||
} |
This file contains 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,39 @@ | ||
import scala.compiletime.* | ||
import scala.compiletime.ops.any.ToString | ||
|
||
trait Attr | ||
case object EmptyAttr extends Attr | ||
transparent inline def attrStr(inline a: Attr): String = inline a match | ||
case EmptyAttr => "" | ||
transparent inline def attrStrHelper(inline a: Attr): String = inline a match | ||
case EmptyAttr => "" | ||
trait TmplNode | ||
case class El[T <: String & Singleton, A <: Attr, C <: Tmpl](val tag: T, val attr: A, val child: C) | ||
extends TmplNode | ||
case class Sib[L <: Tmpl, R <: Tmpl](left: L, right: R) extends TmplNode | ||
type TmplSingleton = String | Char | Int | Long | Float | Double | Boolean | ||
type Tmpl = TmplNode | Unit | (TmplSingleton & Singleton) | ||
transparent inline def tmplStr(inline t: Tmpl): String = inline t match | ||
case El(tag, attr, child) => inline attrStr(attr) match | ||
case "" => "<" + tag + ">" + tmplStr(child) | ||
case x => "<" + tag + " " + x + ">" + tmplStr(child) | ||
case Sib(left, right) => inline tmplStr(right) match | ||
case "" => tmplStr(left) | ||
case right => tmplStrHelper(left) + right | ||
case () => "" | ||
case s: (t & TmplSingleton) => constValue[ToString[t]] | ||
transparent inline def tmplStrHelper(inline t: Tmpl): String = inline t match | ||
case El(tag, attr, child) => inline (tmplStr(child), attrStr(attr)) match | ||
case ("", "") => "<" + tag + "/>" | ||
case (child, "") => "<" + tag + ">" + child + "</" + tag + ">" | ||
case ("", attr) => "<" + tag + " " + attr + "/>" | ||
case (child, attr) => "<" + tag + " " + attr + ">" + child + "</" + tag + ">" | ||
case Sib(left, right) => tmplStrHelper(left) + tmplStrHelper(right) | ||
case () => "" | ||
case s: (t & TmplSingleton) => constValue[ToString[t]] | ||
transparent inline def el(tag: String & Singleton): El[tag.type, EmptyAttr.type, Unit] = | ||
El(tag, EmptyAttr, ()) | ||
extension [T <: String & Singleton, A <: Attr, C <: Tmpl](el: El[T, A, C]) | ||
transparent inline def >>[C2 <: Tmpl](child: C2) = El(el.tag, el.attr, el.child ++ child) | ||
|
||
extension [L <: Tmpl](left: L) transparent inline def ++[R <: Tmpl](right: R) = Sib(left, right) |