Skip to content

Fix #5386: Normalize unary operator expressions #5479

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 4 commits into from
Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 18 additions & 2 deletions compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
def isIdempotentRef(tree: Tree)(implicit ctx: Context): Boolean =
refPurity(tree) >= Idempotent

/** If `tree` is a constant expression, its value as a Literal,
/** (1) If `tree` is a constant expression, its value as a Literal,
* or `tree` itself otherwise.
*
* Note: Demanding idempotency instead of purity in literalize is strictly speaking too loose.
Expand Down Expand Up @@ -485,11 +485,27 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
* Ident
* Select
* TypeApply
*
* (2) A unary operator expression `pre.op` where `op` is one of `+`, `-`, `~`, `!`
* that has a constant type `ConstantType(v)` but that is not a constant expression
* (i.e. `pre` has side-effects) is translated to
*
* { pre; v }
*
* This avoids the situation where we have a Select node that does not have a symbol.
*/
def constToLiteral(tree: Tree)(implicit ctx: Context): Tree = {
val tree1 = ConstFold(tree)
tree1.tpe.widenTermRefExpr match {
case ConstantType(value) if isIdempotentExpr(tree1) => Literal(value)
case ConstantType(value) =>
if (isIdempotentExpr(tree1)) Literal(value)
else tree1 match {
case Select(qual, _) if tree1.tpe.isInstanceOf[ConstantType] =>
// it's a unary operator; Simplify `pre.op` to `{ pre; v }` where `v` is the value of `pre.op`
Copy link
Member

@smarter smarter Nov 20, 2018

Choose a reason for hiding this comment

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

If we lose op before pickling, then it'll be missing from the IDE in go-to-definition/find-all-references

Copy link
Member

Choose a reason for hiding this comment

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

Also I don't really get what's special about unary operators here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They do not get a symbol. So nothing is lost when doing the transform.

Copy link
Member

Choose a reason for hiding this comment

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

but why do they not get a symbol ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because constant folding replaces the type with a ConstantType.

Block(qual :: Nil, Literal(value))
case _ =>
tree1
}
case _ => tree1
}
}
Expand Down
25 changes: 25 additions & 0 deletions tests/pos/i5386.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
object Test {
~{
println("!")
1
}

+{
println("!")
1
}

-{
println("!")
1
}

!{
println("!")
true
}

{ println("1"); 1 } + { println("2"); 2}

!(try true finally{()})
}