Skip to content

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/InterceptedMethods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Copy link
Member

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 ?

Copy link
Contributor Author

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.

*/
class InterceptedMethods extends MiniPhase {
import tpd._
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The qualifier is not constant folded due to the side effectful println.

// in case a unary operator is applied to a block (potentially containing impure expressions)
// we create a synthetic variable and then apply the operator
Copy link
Member

Choose a reason for hiding this comment

The 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 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mean something like that?

  {
    println("!")
    1
  }+2

Copy link
Contributor

Choose a reason for hiding this comment

The 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
}

Expand Down
23 changes: 23 additions & 0 deletions tests/pos/i5386.scala
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{()})
}