Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@artembakhanov
Copy link
Contributor

@artembakhanov artembakhanov commented Apr 15, 2025

Fix #9642

This PR fixes an issue in assertTrue macro that did not allow using compare operation (>, >=, <, <=) inside of it when they were extension methods.

The problem was that when the macro matched a tree with such an operation it always "tried" to summon an instance of Ordering[l] where l is the type of lhs. However, sometimes when having an ops class and implicit conversions to its instance does not mean that there is an Ordering instance for the type (actually I believe it's always the case). In other words, it is just an extension method that's why the macro would never work in Scala 3.

I have only changed the logic when implicitConversionDirection value is Some(false) (direction from the type on the rigth to the one on left) since all the comparison operators are left associative.

case (None, _) =>
val span = getSpan(tree)
'{ TestArrow.succeed($expr).span($span) }
case _ => throw new Error("NO")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
case _ => throw new Error("NO")
case _ => throw new IllegalArgumentException("Unsupported operation in 'assertTrue'\nPlease open an issue: https://github.com/zio/zio/issues/new")

Copy link
Contributor Author

@artembakhanov artembakhanov Apr 15, 2025

Choose a reason for hiding this comment

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

I just thought maybe it should not be the problem of the end user if they include some strange compare expressions? We can't really predict what kind of expressions are possible here, however, they might appear in the future, maybe we should simply allow all of them. And moreover it should not be the problem of this codebase to handle exception cases, such as what I wrote: case (None, _) => ... only for the specific conversion direction and only when a bug has been submitted.

What I mean is that instead of throwing this strange exception, which appears only in expressions with compare operations, simply do the '{ TestArrow.succeed($expr).span($span) } like it is already done when the tree does not match any other patterns (lines 383-385 in the original code)?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I am not sure I 100% understand, but I think you're saying the case you added should be a 'catchall'? Even if that's the case, I think it's better to leave a way to report bugs/issues and we can choose to fix/not fix them. ERROR("NO") is not descriptive.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is used in a macro, why not throw a proper compiler error with the error message suggested by @hearnadam?

Suggested change
case _ => throw new Error("NO")
case _ => scala.compiletime.error("Unsupported operation in 'assertTrue'\nPlease open an issue: https://github.com/zio/zio/issues/new")

Or is that not possible?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kyri-petrou, yes it's possible, I have changed the error message to the one suggested by @hearnadam and used the compiler error like you suggested. The only thing I changed is moved this error expression to a separate def, since it is reused three times in places where "NO" exception was before.

hearnadam
hearnadam previously approved these changes Apr 15, 2025
Copy link
Collaborator

@hearnadam hearnadam left a comment

Choose a reason for hiding this comment

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

Thank you so much 🙏

@hearnadam hearnadam merged commit 6299e27 into zio:series/2.x Apr 17, 2025
18 checks passed
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.

assertTrue macro fails on a simple duration expression

3 participants