Skip to content

Local suppression of errors raised by extra checks from -Xcheck-macros #13926

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

Open
cchantep opened this issue Nov 11, 2021 · 12 comments
Open

Local suppression of errors raised by extra checks from -Xcheck-macros #13926

cchantep opened this issue Nov 11, 2021 · 12 comments
Labels
area:documentation area:metaprogramming:quotes Issues related to quotes and splices

Comments

@cchantep
Copy link
Contributor

As indicated in the documentation, some scope extrusion case like '{ (x: Int) => ${ run('x); 1 } } are valid, but if compiled with -Xcheck-macros it will anyway raise a fatal error.

Being able to locally suppress such error, not to have to globally disable -Xcheck-macros would be nicer.

@nicolasstucki
Copy link
Contributor

The documentation states that scope extrusions are invalid. Specifically this case.

@cchantep
Copy link
Contributor Author

cchantep commented Nov 11, 2021

Sorry but then "This is again phase correct, but will lead us into trouble" is quite unclear.
Moreover, if that's invalid, indicating how to do it properly would be helpful.

@nicolasstucki
Copy link
Contributor

Then we will need to rephrase that sentence to make it clear.

The point is that any kind of scope extrusions can leed to unsound code. In particular a run should never capture expressions that where not created within it's context.

@cchantep
Copy link
Contributor Author

Honestly, the current scope/ownership rules make mixing quotes '{ .. }/Expr and Term/Tree quickly unusable.

@cchantep
Copy link
Contributor Author

An example where it compiled and run fine, but where -Xcheck-macros would raise a fatal error:

scala.quoted.runtime.impl.ScopeException: Expression created in a splice was used outside of that splice.
Created in: api/src/main/scala-3/MacroImpl.scala:869 at column 23
Used in: api/src/main/scala-3/MacroImpl.scala:858 at column 55
Expr: doc


Creation stack:
	api/src/main/scala-3/MacroImpl.scala:869 at column 23
	api/src/main/scala-3/Macros.scala:43 at column 5


Use stack:
	api/src/main/scala-3/MacroImpl.scala:858 at column 55
	api/src/main/scala-3/Macros.scala:43 at column 5

@nicolasstucki
Copy link
Contributor

It looks like you are missing a Quotes argument in appendDocCall.

- def appendDocCall(doc: Expr[BSONDocument]): Expr[MBuilder[BSONElement, Seq[BSONElement]]] = {
+ def appendDocCall(doc: Expr[BSONDocument])(using Quotes): Expr[MBuilder[BSONElement, Seq[BSONElement]]] = {

The check indicates that in

                      ${ appendDocCall('{ doc }) }

the '{ doc } was created within this ${ ... } but because you are capturing the outer Quotes in appendDocCall.

The -Xcheck-macros seems to have correctly detected this situation. The code will work but because of this inconsistency the compiler will need to perform extra work to fix it after the fact. This is an instance where the owners are not created correctly and need to be changed when splicing. This can lead to exponential behavior in the worst case.

@cchantep
Copy link
Contributor Author

cchantep commented Nov 15, 2021

If you mean that syntax like '{ ${ foo('{ v }) } } should not be used at all, then :

  • the compiler need to refuse that (even without-Xcheck-macros),
  • the alternatives need to be documented (which I guess will either require to switch the whole logic using Terms, or prevent factorisation of quotes, either way, quite unfortunate)

@nicolasstucki
Copy link
Contributor

No, I mean that foo should have a (using Quotes) parameter clause because in some cases the lack of it may cause performance issues. The compiler is resilient to this issue but -Xcheck-macros will complain to let the user know.

This is the most common case for this failure and we could give a hint in the message if the -Xcheck-macros. Usually, a simple way to fix it is to move the local method outside of the enclosing method and see that there is a missing Quotes.

We also need to add better documentation in the macro tutorial.

@cchantep
Copy link
Contributor Author

cchantep commented Nov 15, 2021

The using Quotes is in the enclosing function.

@marekzebrowski
Copy link

That bug is easily reproducible (3.1.1, 3.1.0) using documentation example Eq typeclass from https://dotty.epfl.ch/docs/reference/contextual/derivation-macro.html
removing -Xcheck-macros flag makes example work

@nicolasstucki
Copy link
Contributor

nicolasstucki commented Apr 25, 2022

Indeed, the example has a bug an -Xcheck-macros cough it. Improving the docs in #15026.

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Apr 25, 2022
Issue found in scala#13926 (comment)

Tested in tests/run-macros/i8007/Macro_3.scala (already up to date).
@nicolasstucki nicolasstucki added area:metaprogramming:quotes Issues related to quotes and splices and removed area:metaprogramming labels Jun 2, 2022
bishabosha pushed a commit to dotty-staging/dotty that referenced this issue Oct 18, 2022
Issue found in scala#13926 (comment)

Tested in tests/run-macros/i8007/Macro_3.scala (already up to date).
@ckipp01
Copy link
Member

ckipp01 commented May 15, 2023

@nicolasstucki I see you did update docs. Do you feel like those updates are enough to close this, or are more needed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:documentation area:metaprogramming:quotes Issues related to quotes and splices
Projects
None yet
Development

No branches or pull requests

5 participants