Skip to content
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

Merged
merged 1 commit into from
Feb 29, 2024

Conversation

nicolasstucki
Copy link
Contributor

@nicolasstucki nicolasstucki commented Feb 20, 2024

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

* 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 =
Copy link
Contributor

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.

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 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.

@odersky odersky assigned nicolasstucki and unassigned odersky Feb 27, 2024
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
Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

LGTM

@nicolasstucki nicolasstucki merged commit 1191671 into scala:main Feb 29, 2024
19 checks passed
@nicolasstucki nicolasstucki deleted the fix-15315 branch February 29, 2024 15:08
@Kordyjan Kordyjan added this to the 3.4.2 milestone Mar 28, 2024
WojciechMazur added a commit that referenced this pull request Jul 3, 2024
#20967)

Backports #19739 to the LTS branch.

PR submitted by the release tooling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

calling methods with default arguments on anonymous instances crashes the compiler
3 participants