-
Notifications
You must be signed in to change notification settings - Fork 39
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
Have ReverseOrder
rule flag Collections.reverseOrder(String::compareTo)
#398
base: master
Are you sure you want to change the base?
Conversation
Looks good. No mutations were possible for these changes. |
45d5480
to
cf38b37
Compare
Looks good. No mutations were possible for these changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you share a link on Slack to the code that gives trouble I'll try to have a look.
@@ -58,6 +58,7 @@ Comparator<T> after() { | |||
Comparator<T> before() { | |||
return Refaster.anyOf( | |||
Collections.reverseOrder(), | |||
Collections.reverseOrder(T::compareTo), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rule above is meant to replace T::compareTo
with naturalOrder()
, so this one shouldn't be necessary. 🤔 (And the build seems to agree 👀)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I shared it.
Still I think we should add this one. If we apply the rewrite suggested right now we would end up in a state where you can have:
.isSortedAccordingTo(Collections.reverseOrder(Comparator.naturalOrder()));
It gives the following error:
'reverseOrder(java.util.Comparator )' in 'java.util.Collections' cannot be applied to '(java.util.Comparator )'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the problem as I understand it is that:
org.assertj.core.api.AbstractListAssert#isSortedAccordingTo
accepts anyComparator<? super ELEMENT>
. (For the code at handELEMENT
isOffsetDateTime
.)java.util.Collections#reverseOrder
accepts anyComparable<T>
.java.util.Comparator.naturalOrder()
returns aComparator<T extends Comparable<? super T>>
.
As a result the compiler emits:
no suitable method found for reverseOrder(java.util.Comparator<T>)
method java.util.Collections.<T>reverseOrder(java.util.Comparator<T>) is not applicable
(inferred type does not conform to equality constraint(s)
inferred: T
equality constraints(s): T)
method java.util.Collections.<T>reverseOrder() is not applicable
(cannot infer type-variable(s) T
(actual and formal argument lists differ in length))
This is a general problem with Refaster. Just adding a T::compareTo
case here is not enough: the NaturalOrder
rule would also "break" e.g. the following expressions:
abstractListAssert.isSortedAccordingTo(reverseOrder(comparing(identity())))
abstractListAssert.isSortedAccordingTo(reverseOrder(comparing(v -> v)))
We could of course just add those cases as well, but:
- That doesn't solve the general problem of rules such as
NaturalOrder
breaking code. - There are likely other such problematic interactions between rules, and ideally we find a way to identify those, too. (Because otherwise a solution to (1) would prevent certain viable transformations from being applied.)
Yes, that's all much harder... :(
I did play around a bit with a @Placeholder @Matches(CustomMatcher.class)
approach, but it appears that the VisitorState
passed by Refaster's placeholder logic to the Matcher
is "too fake" to infer the symbols and type information we may need to solve problem (1).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, okay I didn't expect this. That's indeed a much bigger issue 🤔. Let's sync on this one offline tomorrow.
Moved it to the next milestone 0.7.0. |
Encountered this problem when testing downstream.
Without this rule this:
Would be replaced with:
Which is incompatible so it would lead to non compilable code.