-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #3912: Account for units and blocks in inline and macros #4012
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
Conversation
tree match { | ||
case expansion: Select if expansion.symbol.isSplice => | ||
Some(expansion) | ||
case Block(List(stat), Literal(Constant(()))) => unapply(stat) |
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.
Can you give an example of when this happen?
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.
Yes, this is related to the implicit conversion related to value discarding (simply put when an e
returns a value and it's called from a function of expected type Unit
and it is converted into { e; () }
). This manifested in quoted code as well.
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.
For example inline foo: Unit = 4
will become inline foo: Unit = { 4; () }
and then be inlined as { 4; () }
. In our case we have a ~xyz
instead of 4
.
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.
Got it! I would add a comment explaining why this is needed (i.e. the compiler can insert ()
).
/** InlineSplice is used to detect cases where the expansion | ||
* consists of a (possibly multiple & nested) block or a sole expression. | ||
*/ | ||
object InlineSplice { |
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 don't know how performance sensitive this code is but you could return a Tree
instead of Option[Select]
to avoid boxing (and EmptyTree
instead of None
)
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.
We need that .get
method on unapply
's return type so isn't Option
unavoidable?
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 believe Tree
qualifies for both product pattern and name based pattern. And scalac supports name based pattern
What's the relationship with #4019? |
No relationship. |
Needs rebase |
// To maintain phase consistency, we move the binding of the this parameter into the spliced code | ||
val (splicedBindings, stagedBindings) = bindings.partition { | ||
case vdef: ValDef => vdef.symbol.is(Synthetic) // Assume that only _this bindings are tagged with Synthetic | ||
case _ => false | ||
} | ||
|
||
val tree1 = | ||
if (level == 0) cpy.Inlined(tree)(call, stagedBindings, Splicer.splice(seq(splicedBindings, body))) | ||
else seq(stagedBindings, cpy.Select(expansion)(cpy.Inlined(tree)(call, splicedBindings, body), name)) | ||
transform(tree1) |
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.
This should be atree2
val tree2 = transform(tree1)
val tree1 = | ||
if (level == 0) cpy.Inlined(tree)(call, stagedBindings, Splicer.splice(seq(splicedBindings, body))) | ||
else seq(stagedBindings, cpy.Select(expansion)(cpy.Inlined(tree)(call, splicedBindings, body), name)) | ||
transform(tree1) | ||
|
||
// due to value-discarding which converts an { e } into { e; () }) | ||
if (tree.tpe =:= defn.UnitType) Block(tree1 :: Nil, Literal(Constant(()))) |
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.
And use tree2
here and in next line
Approve @nicolasstucki? ⭐️ |
No description provided.