Skip to content

Commit

Permalink
8303526: Changing "arbitrary" Name.compareTo() ordering breaks the re…
Browse files Browse the repository at this point in the history
…gression suite

Reviewed-by: vromero
  • Loading branch information
archiecobbs authored and Vicente Romero committed Mar 28, 2023
1 parent 6aec6f3 commit 426025a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ public final boolean precedes(TypeSymbol that, Types types) {
return
types.rank(that.type) < types.rank(this.type) ||
(types.rank(that.type) == types.rank(this.type) &&
that.getQualifiedName().compareTo(this.getQualifiedName()) < 0);
this.getQualifiedName().compareTo(that.getQualifiedName()) < 0);
} else if (type.hasTag(TYPEVAR)) {
return types.isSubtype(this.type, that.type);
}
Expand Down
39 changes: 35 additions & 4 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/util/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* This code and its internal interfaces are subject to change or
* deletion without notice.</b>
*/
public abstract class Name implements javax.lang.model.element.Name, PoolConstant {
public abstract class Name implements javax.lang.model.element.Name, PoolConstant, Comparable<Name> {

public final Table table;

Expand Down Expand Up @@ -102,10 +102,41 @@ public Name append(char c, Name n) {
return table.fromUtf(bs, 0, bs.length);
}

/** An arbitrary but consistent complete order among all Names.
/** Order names lexicographically.
*
* <p>
* The ordering defined by this method must match the ordering
* defined by the corresponding {@link #toString()} values.
* @see String#compareTo
*/
public int compareTo(Name other) {
return other.getIndex() - this.getIndex();
@Override
public int compareTo(Name name) {
byte[] buf1 = getByteArray();
byte[] buf2 = name.getByteArray();
int off1 = getByteOffset();
int off2 = name.getByteOffset();
int len1 = getByteLength();
int len2 = name.getByteLength();
while (len1 > 0 && len2 > 0) {
int val1 = buf1[off1++] & 0xff;
int val2 = buf2[off2++] & 0xff;
if (val1 == 0xc0 && (buf1[off1] & 0x3f) == 0) {
val1 = 0; // char 0x0000 encoded in two bytes
off1++;
len1--;
}
if (val2 == 0xc0 && (buf2[off2] & 0x3f) == 0) {
val2 = 0; // char 0x0000 encoded in two bytes
off2++;
len2--;
}
int diff = val1 - val2;
if (diff != 0)
return diff;
len1--;
len2--;
}
return len1 > 0 ? 1 : len2 > 0 ? -1 : 0;
}

/** Return true if this is the empty name.
Expand Down

1 comment on commit 426025a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.