-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #5386: Generate a synthetic val def and then apply the unary operator #5387
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
Changes from all commits
8479be5
28a42a5
6868d6c
3e9ce51
c83a362
3dba70f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import dotty.tools.dotc.core.Names.TermName | |
import dotty.tools.dotc.core.StdNames._ | ||
import dotty.tools.dotc.core.Symbols._ | ||
import dotty.tools.dotc.core.Types._ | ||
import dotty.tools.dotc.core.NameKinds._ | ||
import dotty.tools.dotc.transform.MegaPhase.MiniPhase | ||
|
||
object InterceptedMethods { | ||
|
@@ -22,6 +23,7 @@ object InterceptedMethods { | |
* - `x.##` for ## in Any becomes calls to ScalaRunTime.hash, | ||
* using the most precise overload available | ||
* - `x.getClass` for getClass in primitives becomes `x.getClass` with getClass in class Object. | ||
* - `!{...;true}` becomes val tmp = {...;true};!tmp (similar to other primitives/unary ops) | ||
*/ | ||
class InterceptedMethods extends MiniPhase { | ||
import tpd._ | ||
|
@@ -45,6 +47,16 @@ class InterceptedMethods extends MiniPhase { | |
ctx.log(s"$phaseName rewrote $tree to $rewritten") | ||
rewritten | ||
} | ||
// if the qualifier is constant folded then its type doesn't have a symbol | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but if its type doesn't have a symbol, does that always mean that the qualifier is constant-folded ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The qualifier is not constant folded due to the side effectful |
||
// in case a unary operator is applied to a block (potentially containing impure expressions) | ||
// we create a synthetic variable and then apply the operator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is special about unary operators here compared to any other method call on a block ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you mean something like that? {
println("!")
1
}+2 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It feels like this should have a more general solution, independent of the fact that these are operators. I.e. in general: { ... }.m where m has constant type v --> { ...; v } |
||
else if (!tree.qualifier.symbol.exists && tree.name.startsWith(nme.UNARY_PREFIX.toString)) { | ||
val tempDef = SyntheticValDef(UniqueName.fresh().toTermName, tree.qualifier) | ||
val rewritten = Block(tempDef :: Nil, ref(tempDef.symbol).select(tree.name)) | ||
ctx.log(s"$phaseName rewrote $tree to $rewritten") | ||
|
||
rewritten | ||
} | ||
else tree | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
object Test { | ||
~{ | ||
println("!") | ||
1 | ||
} | ||
|
||
+{ | ||
println("!") | ||
1 | ||
} | ||
|
||
-{ | ||
println("!") | ||
1 | ||
} | ||
|
||
!{ | ||
println("!") | ||
true | ||
} | ||
|
||
!(try true finally{()}) | ||
} |
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.
What other primitives ? Where is that defined ?
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 should have written literal constants instead of primitives.