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

Fix for macro annotation that resolves macro-based implicit crashing the compiler #20353

Merged
merged 3 commits into from
Nov 19, 2024

Conversation

pweisenburger
Copy link
Contributor

When (1) macro-annotating a class or object nested in another class or object and (2) the annotation macro tries to summon an implicit value using Implicits.search and (3) the implicit search needs to expand a given macro, the compiler crashes with:

Failed to evaluate macro.
  Caused by class dotty.tools.dotc.CompilationUnit$SuspendException

I found that the reason for the crash is macroDependencies returning locally defined variables, which, I believe, it should not. In particular it returns a list containing $anonfun for trees of the following shape that appear when resolving implicit macros:

{
  def $anonfun(using contextual$1: scala.quoted.Quotes): scala.quoted.Expr[<type for implicit search>] =
    <given macro for implicit value>(contextual$1)
  closure($anonfun)
}

This PR skips over such Closure nodes, which should be the correct behavior. Not sure if there are other cases that macroDependencies handles incorrectly and that should be fixed ...

@hamzaremmal
Copy link
Member

Hey, to fix the CI run, can you rebase your work on the latest commit in main ?

@pweisenburger pweisenburger force-pushed the anon-funs-in-macro-deps branch from 9206739 to 9c14610 Compare May 10, 2024 13:05
@pweisenburger
Copy link
Contributor Author

I rebased on the main branch.

@hamzaremmal
Copy link
Member

Hi, I've been investigating this for the past few days and I'm not sure why the closure is created. I wasn't able to reproduce the behavior with other constructs. I'm still looking into it and I will answer back here when I have a final answer.

@pweisenburger
Copy link
Contributor Author

Thanks! 🙂

I assumed this was normal behavior for expanding macro givens, as at some point you probably want a context function Quotes ?=> T (to inject the implicit Quotes argument), hence the given is eta-expanded, hence the closure. But this guess could be wrong ...

tests/pos-macros/i20353/Macro_1.scala Outdated Show resolved Hide resolved
tests/pos-macros/i20353/Test_2.scala Outdated Show resolved Hide resolved
@hamzaremmal
Copy link
Member

@pweisenburger Sorry for the very late review 😅 , I had some weird behaviour in the compiler when I was playing with your changes, everything should be fixed now !

@hamzaremmal hamzaremmal merged commit d86f44d into scala:main Nov 19, 2024
27 checks passed
hamzaremmal added a commit that referenced this pull request Nov 19, 2024
This changes makes the execution of the test case in #20353 stable
across different run
@WojciechMazur WojciechMazur added this to the 3.6.3 milestone Nov 25, 2024
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.

3 participants