Skip to content

Commit

Permalink
*maybe* fix LatticeTest rare failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakobeha committed Jun 6, 2024
1 parent dde6bb6 commit 6d1cb12
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/main/java/org/prlprg/ir/type/OverloadRType.java
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,43 @@ public int hashCode() {
parameters, fallbackEffects, genericEffects, fallbackReturnType, genericReturnType);
}

/**
* If the types are equal, returns 0. Otherwise, returns a positive or negative integer, so that
* this function is commutative and associative.
*/
int rawCompareTo(OverloadRTypeImpl other) {
// Compare hashCode, but in the rare case they are equal, we must do a deep comparison.
// Specifically, we do `toString` and compare generic effects because that finds any
// differences.
var hashCodeCmp = Integer.compare(hashCode(), other.hashCode());
if (hashCodeCmp != 0) {
return hashCodeCmp;
}
var toStringCmp = toString().compareTo(other.toString());
if (toStringCmp != 0) {
return toStringCmp;
}
if (genericEffects != null && other.genericEffects != null) {
var genericEffectsCmp =
Integer.compare(genericEffects.hashCode(), other.genericEffects.hashCode());
if (genericEffectsCmp != 0) {
return genericEffectsCmp;
}
} else if (genericEffects != null) {
return 1;
} else if (other.genericEffects != null) {
return -1;
}
if (genericReturnType != null && other.genericReturnType != null) {
return Integer.compare(genericReturnType.hashCode(), other.genericReturnType.hashCode());
} else if (genericReturnType != null) {
return 1;
} else if (other.genericReturnType != null) {
return -1;
}
return 0;
}

@Override
public String toString() {
return "("
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/prlprg/ir/type/RFunctionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ static ImmutableList<OverloadRType> normalizeOverloadsWrtSubsuming(
if (o2SupersetsO1 && !o1SupersetsO2) {
return -1;
}
return Integer.compare(o1.hashCode(), o2.hashCode());

return ((OverloadRTypeImpl) o1).rawCompareTo((OverloadRTypeImpl) o2);
});
return ImmutableList.copyOf(list);
}
Expand Down

0 comments on commit 6d1cb12

Please sign in to comment.