jdaugherty commented on PR #2690:
URL: https://github.com/apache/groovy/pull/2690#issuecomment-4957813540
On the drop return type note:
Because that "tie" is exactly the failure. Sorting only by name + parameters
means a bridge method and the method it bridges compare equal (return 0).
Arrays.sort is stable — TimSort — so equal elements keep their input order. And
the input order is getDeclaredMethods(), the very thing that's nondeterministic
and varies between JVM runs. So the tie doesn't get resolved; it silently
inherits the nondeterminism you're trying to eliminate.
Concretely, covariant returns / generic erasure produce this:
class Base<T> { T get() { ... } }
class Sub extends Base<String> {
String get() { ... } // real method
// Object get() // synthetic bridge — same name, same (empty)
params
}
Sub has two get methods: same name, identical parameter types, differing
only in return type (String vs Object). With a name+params comparator they tie,
and whichever the JVM happened to enumerate first wins — per build. Adding the
return-type key breaks that last tie, so no two distinct methods ever compare
equal.
That's what "total order" buys: the JVM forbids two methods in one class
sharing a name and descriptor, and the descriptor is precisely parameters +
return type. Compare on the full descriptor and every element has a unique sort
key, so the output is fully determined no matter what order
getDeclaredMethods() hands back. Leave out the return type and the ordering is
merely partial — total everywhere except the bridge pairs, which are the one
place reproducibility actually needed it.
(Constructors don't need it: name is always <init> and return type always
void, so parameters alone are a total order there — which is why
getDeclaredConstructorsSorted stops at compareParameterTypes.)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]