-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Lift all non trivial prefixes for default parameters #19739
Conversation
08b1e9a
to
c708ecf
Compare
* unless `pre` is idempotent reference, a `this` reference, a literal value, or a or the prefix of an `init` (`New` tree). | ||
* | ||
* Note that default arguments will refer to the prefix, we do not want | ||
* to re-evaluate a complex expression each time we access a getter. | ||
*/ | ||
def liftPrefix(defs: mutable.ListBuffer[Tree], tree: Tree)(using Context): Tree = |
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 we get the old behavior when there are no default arguments? We'd need a boolean passed to liftPrefix
indicating whether the enclosing method has the HasDefaultParams
flag.
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 split this definition into liftNonIdempotentPrefix
and liftPrefix
instead. Compining them made the documentation to convoluted. HasDefaultParams
is used to choose which of those methods to call.
Checking if the prefix is pure is not enough to know if we need to list the prefix. In the case of default parameters, the prefix tree might be used several times to compute the default values. This expression should only be computed once and therefore it should be lifted if there is some computation/allocation involved. Furthermore, if the prefix contains a local definition, it must be lifted to avoid duplicating the definition. A similar situation could happen with dependent default parameters. This currently works as expected. Fixes scala#15315
c708ecf
to
71b983b
Compare
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.
LGTM
Checking if the prefix is pure is not enough to know if we need to list the prefix. In the case of default parameters, the prefix tree might be used several times to compute the default values. This expression should only be computed once and therefore it should be lifted if there is some computation/allocation involved. Furthermore, if the prefix contains a local definition, it must be lifted to avoid duplicating the definition.
A similar situation could happen with dependent default parameters. This currently works as expected.
Fixes #15315