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

Add Expr.valueOrAbort and reflect.report.errorAndAbort #12056

Conversation

nicolasstucki
Copy link
Contributor

Provides a homogeneous and unambiguous concept for stopping the expansion of a macro.

The advantages of this naming are

  • Consistent name suffixes xyzAbort
  • report.e will show auto-completion for all variants of error and errorAndAbort
  • Abort cannot be confused with other kinds of error handling in this API

This is an alternative to #12022

@nicolasstucki nicolasstucki force-pushed the use-abort-as-concept-for-stopping-macro-expansion branch from 3514ee2 to 2705092 Compare April 12, 2021 09:08
@nicolasstucki nicolasstucki marked this pull request as ready for review April 12, 2021 12:06
@nicolasstucki
Copy link
Contributor Author

@liufengyun we also need to take into account the discussion in #12022

liufengyun
liufengyun previously approved these changes Apr 12, 2021
Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

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

LGTM

library/src/scala/quoted/Quotes.scala Outdated Show resolved Hide resolved
@nicolasstucki nicolasstucki force-pushed the use-abort-as-concept-for-stopping-macro-expansion branch from 51fd7af to 4586e7a Compare April 13, 2021 09:25
library/src/scala/quoted/Quotes.scala Show resolved Hide resolved
@nicolasstucki nicolasstucki force-pushed the use-abort-as-concept-for-stopping-macro-expansion branch from 4586e7a to 4d28db1 Compare April 13, 2021 16:03
@nicolasstucki nicolasstucki dismissed smarter’s stale review April 13, 2021 16:04

Updated deprecation to 3.1.0

@nicolasstucki nicolasstucki force-pushed the use-abort-as-concept-for-stopping-macro-expansion branch from 4d28db1 to cd70cb2 Compare April 13, 2021 16:05
Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@abgruszecki abgruszecki left a comment

Choose a reason for hiding this comment

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

LGTM!

@smarter smarter marked this pull request as draft April 14, 2021 16:06
@nicolasstucki nicolasstucki marked this pull request as ready for review April 14, 2021 16:53
@nicolasstucki nicolasstucki marked this pull request as draft April 14, 2021 16:53
@nicolasstucki nicolasstucki added this to the 3.1.0 milestone Apr 26, 2021
@nicolasstucki nicolasstucki force-pushed the use-abort-as-concept-for-stopping-macro-expansion branch 2 times, most recently from 65d2eb4 to 9d2fd94 Compare May 11, 2021 16:17
@nicolasstucki nicolasstucki removed this from the 3.1.0 milestone May 12, 2021
@nicolasstucki
Copy link
Contributor Author

Now the added methods are marked as experimental and we do not need to wait until 3.1 to add them.

@nicolasstucki nicolasstucki requested a review from liufengyun May 12, 2021 07:36
@nicolasstucki nicolasstucki marked this pull request as ready for review May 12, 2021 08:20
@nicolasstucki
Copy link
Contributor Author

Will need rebase on #12371

Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

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

LGTM

compiler/src/dotty/tools/dotc/transform/Splicer.scala Outdated Show resolved Hide resolved
* Otherwise returns the value.
*/
@experimental
def valueOrAbort(using FromExpr[T]): T

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest the method to take a message for indicating the reason why it's aborted. It helps end-users in debugging and saves the efforts of macro-authors in writing error-handling code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The question then is if we should keep the version with the default error message. The idea if this signature was also to make the authors not need to write the error message.

Currently the can do

x.value match
  case Some(n) => ...
  case None => reflect.report.error("......", x)

or just

if x.valueOrAbort == 0 then ...
else 

the proposed one would need to add the message

if x.valueOrAbort(".........") == 0 then
else ...

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, the version with the error message does not look better in code and can be a little annoying for prototyping. For macro authors, it's not clear that it's better.

Macros can be subtle to use, more debuggability to end-users will make them more friendly for usage. However, if the gain for end-users is small, maybe it's not worth the complication.

The question then is if we should keep the version with the default error message

We probably only want to keep one of the two.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could add an valueOrAbortWith("....") later or users will just define their own in the meantime. We should probably wait and see what users define themselves to know what is preferable in paractice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Meanwhile, they can just define something like this in their projects

extension [T: FromExpr](expr: Expr[T]) 
  def valueOrAbortWith(msg: String)(using Quotes) = 
    x.value match
      case Some(n) => n
      case None => reflect.report.errorAndAbort(msg, x)
Expr(1).valueOrAbortWith("expected a constant")

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, it's better to wait and do it later.

@nicolasstucki nicolasstucki force-pushed the use-abort-as-concept-for-stopping-macro-expansion branch 2 times, most recently from 55de88a to 21bc7e3 Compare May 17, 2021 12:45
@nicolasstucki nicolasstucki requested a review from liufengyun May 17, 2021 13:59
@nicolasstucki
Copy link
Contributor Author

@liufengyun could you review the changes. I had to move some implementations to make MiMa happy.

Provides a homogeneous and unambiguous concept for stopping the expansion of a macro.

The advantages of this naming are

* Consistent name suffixes `xyzAbort`
* `report.e` will show auto-completion for all variants of `error` and `errorAndAbort`
* `Abort` cannot be confused with other kinds of error handling in this API
@smarter smarter added the needs-minor-release This PR cannot be merged until the next minor release label May 17, 2021
@nicolasstucki nicolasstucki force-pushed the use-abort-as-concept-for-stopping-macro-expansion branch from 21bc7e3 to 43fec8f Compare May 17, 2021 15:41
Copy link
Contributor

@liufengyun liufengyun 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 removed the needs-minor-release This PR cannot be merged until the next minor release label May 17, 2021
@nicolasstucki nicolasstucki enabled auto-merge May 17, 2021 15:59
@nicolasstucki nicolasstucki merged commit 7e33289 into scala:master May 17, 2021
@nicolasstucki nicolasstucki deleted the use-abort-as-concept-for-stopping-macro-expansion branch May 17, 2021 19:01
@Kordyjan Kordyjan added this to the 3.0.1 milestone Aug 2, 2023
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.

5 participants