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

Only evaluate transparent inline unapply once #19380

Merged
merged 2 commits into from
Jan 30, 2024

Conversation

nicolasstucki
Copy link
Contributor

Fixes #19369

@@ -1446,7 +1446,9 @@ trait Applications extends Compatibility {
unapplyArgType

val dummyArg = dummyTreeOfType(ownType)
val unapplyApp = typedExpr(untpd.TypedSplice(Apply(unapplyFn, dummyArg :: Nil)))
val unapplyApp = withMode(Mode.NoInline) { // transparent inline unapplies are exapnded when indexing the pattern see `indexPattern` in Typer.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will not work. We need to refine the types for the transparent unapply at this point.

We might need to fully inline the call here, which is proving to be a bit more involved.

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 managed to make it work.

The trick is to type this application with the dummy argument without inlining a just after that inline the unapply function. We also need to adapt unapply function unapplyFn to make the new code use the inlined code. Note that the inlining of the unapply function must be done on the function that receives the dummy argument, trailing implicits are kept as is.

@nicolasstucki nicolasstucki force-pushed the fix-19369 branch 2 times, most recently from 38fea45 to 3edd16b Compare January 11, 2024 08:26
@nicolasstucki nicolasstucki marked this pull request as ready for review January 11, 2024 10:48
val unapplyApp = typedExpr(untpd.TypedSplice(Apply(unapplyFn, dummyArg :: Nil)))
val (newUnapplyFn, unapplyApp) =
val unapplyAppCall = withMode(Mode.NoInline) {
typedExpr(untpd.TypedSplice(Apply(unapplyFn, dummyArg :: Nil)))
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer fewerBraces style instead, with a :. Did you consider that?

* }.unapply(`dummy`)(using t1, ..)
* ```
*/
def inlinedUnapplyFnAndApp(unapp: Tree): (Tree, Tree) = unapp match {
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider moving this function further out, into the body of typedUnapply. It's a complicated operation for a special case, which makes it harder to follow the already complicated logic 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.

Moved out inlinedUnapplyFnAndApp. Also moved unapplyImplicits in a separate commit to cleanup the code even further.

@odersky
Copy link
Contributor

odersky commented Jan 17, 2024

Otherwise LGTM

@odersky odersky merged commit 997103d into scala:main Jan 30, 2024
19 checks passed
@odersky odersky deleted the fix-19369 branch January 30, 2024 16:40
@Kordyjan Kordyjan added this to the 3.4.1 milestone Feb 14, 2024
odersky added a commit that referenced this pull request Feb 27, 2024
)

We needed to delay the inlining of the transparent inline when typing
the unapply function application. We used the NoInline mode, but this
also stopped the inlining of the arguments of the unapply. To fix this
we target more precisely the inlining of the unapply method and not the
implicit arguments. To do this we detect the dummy argument that is used
type the unapply as an application, before it is transformed into a
pattern.

Fixes #19623
Fixes solution added in #19380
nicolasstucki added a commit that referenced this pull request Apr 4, 2024
These currently got inlined while typing. Therefore they used to
generate code that should not be pickled. The non-transparent inline
methods should be inlined in the inlining phase.

This was found while working on #19380.
WojciechMazur added a commit that referenced this pull request Jul 5, 2024
Backports #19380 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
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.

transparent inline unapply method macros are executed twice
3 participants