Fix class ordering when propagating type annotations, take two #352
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
To propagate type annotations across nested classes, we sort the entire set of indexed classes so that outer classes come before their inner classes. We don't really care about ordering between classes that are not members of the same nest (using the JEP 181 terminology). That is, we only need a partial order on classes.
However, to perform that sort, we use the sorting routine in the JDK, which uses the order defined by a
Comparator
, and that order must be total.To define a total order, this commit takes a completely opposite approach. We compute a nesting level for each class (where a top-level class has a nesting level of 0, a class nested in a top-level class has a nesting level of 1, etc.) and sort by the nesting level. For equal nesting levels, we sort by the class name. This means that all top-level classes come first, all classes nested in top-level classes come second, etc.
To verify that a given
Comparator
establishes a total order for a given set of values, this commit includes a simple utility classTotalOrderChecker
, which was used to reproduce (and understand) the problem of previous implementation of this ordering.Fixes #349
Follows up on #290