-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Improve constant folding logic #12080
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 1 commit
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import scala.quoted.* | ||
|
||
object M { | ||
|
||
transparent inline def f(inline s: String): String | Null = | ||
${ f('s) } | ||
|
||
def f(s: Expr[String])(using Quotes): Expr[String | Null] = { | ||
s.valueOrError // required | ||
'{ null } | ||
} | ||
} |
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 @@ | ||
object T2 { | ||
import M.f | ||
|
||
private inline val V = "V" | ||
private inline def D = "D" | ||
|
||
trait Trait { def s: String } | ||
|
||
object MatchFV extends Trait { | ||
override transparent inline def s: String = | ||
inline f(V) match { case "V" => "o"; case _ => "x" } // error in RC1 | ||
} | ||
|
||
object MatchFD extends Trait { | ||
override transparent inline def s: String = | ||
inline f(D) match { case "D" => "o"; case _ => "x" } | ||
} | ||
} |
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,9 @@ | ||
transparent inline def f: Null = null | ||
|
||
inline def g: Unit = | ||
inline if f == "V" then 1 else 2 | ||
inline if f != "V" then 3 else 4 | ||
inline if "v" == f then 5 else 6 | ||
inline if "v" != f then 7 else 8 | ||
|
||
def test = g |
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,86 @@ | ||
object T { | ||
|
||
transparent inline def f(inline s: String): String | Null = | ||
null | ||
|
||
inline val V = "V" | ||
inline def D = "D" | ||
|
||
trait Trait { def s: String } | ||
|
||
// =========================================================================== | ||
// inline {if,match} over inline {val,def} | ||
|
||
transparent inline def if_v: String = | ||
inline if V == "V" then "o" else "x" | ||
|
||
transparent inline def if_d: String = | ||
inline if D == "D" then "o" else "x" | ||
|
||
transparent inline def match_v: String = | ||
inline V match { case "V" => "o"; case _ => "x" } | ||
|
||
transparent inline def match_d: String = | ||
inline D match { case "D" => "o"; case _ => "x" } | ||
|
||
// =========================================================================== | ||
// inline {if,match} over inline f(inline {val,def}) | ||
|
||
transparent inline def if_fv: String = | ||
inline if f(V) == "V" then "o" else "x" | ||
|
||
transparent inline def if_fd: String = | ||
inline if f(D) == "D" then "o" else "x" | ||
|
||
transparent inline def match_fv: String = | ||
inline f(V) match { case "V" => "o"; case _ => "x" } | ||
|
||
transparent inline def match_fd: String = | ||
inline f(D) match { case "D" => "o"; case _ => "x" } | ||
|
||
// =========================================================================== | ||
// inline {if,match} over inline {val,def} in overridden method | ||
|
||
object IfV extends Trait { | ||
override transparent inline def s: String = | ||
inline if V == "V" then "o" else "x" | ||
} | ||
|
||
object IfD extends Trait { | ||
override transparent inline def s: String = | ||
inline if D == "D" then "o" else "x" // <--------------------------- error | ||
} | ||
|
||
object MatchV extends Trait { | ||
override transparent inline def s: String = | ||
inline V match { case "V" => "o"; case _ => "x" } | ||
} | ||
|
||
object MatchD extends Trait { | ||
override transparent inline def s: String = | ||
inline D match { case "D" => "o"; case _ => "x" } | ||
} | ||
|
||
// =========================================================================== | ||
// inline {if,match} over inline f(inline {val,def}) in overridden method | ||
|
||
object IfFV extends Trait { | ||
override transparent inline def s: String = | ||
inline if f(V) == "V" then "o" else "x" // <------------------------ error | ||
} | ||
|
||
object IfFD extends Trait { | ||
override transparent inline def s: String = | ||
inline if f(D) == "D" then "o" else "x" // <------------------------ error | ||
} | ||
|
||
object MatchFV extends Trait { | ||
override transparent inline def s: String = | ||
inline f(V) match { case "V" => "o"; case _ => "x" } | ||
} | ||
|
||
object MatchFD extends Trait { | ||
override transparent inline def s: String = | ||
inline f(D) match { case "D" => "o"; case _ => "x" } | ||
} | ||
} |
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,4 @@ | ||
class Test: | ||
def n: Null = null | ||
def test1: Boolean = n == null | ||
def test2: Boolean = null == n |
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,3 @@ | ||
def test: Boolean = nn(42) == 42 | ||
|
||
def nn(x: Int): x.type & 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,8 @@ | ||
inline def c: Int = 2 | ||
|
||
trait A: | ||
def f: Unit | ||
|
||
class B extends A: | ||
override inline def f: Unit = | ||
inline if c == 2 then () else () |
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.
I think it would be more elegant if
ConstantTree
was an extractor.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.
Moved to
ConstantTree
extractor