Skip to content
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

fix(CtLineElementComparator): one can add two values to an annotation #3640

Merged
merged 5 commits into from
Oct 8, 2020

Conversation

nharrand
Copy link
Collaborator

@nharrand nharrand commented Oct 7, 2020

fix #3639 .

I seems that the entry set of the values contained an CtAnnotationImpl are ordered based on their source position. The issue is that so far CtLineElementComparator#compare() returns 0 if both element have NoSourcePosition. But this is the case when both elements are constructed programmatically. Hence the entry set arbitrarily retains only one of them.

My proposed solution is to make CtLineElementComparator#compare() returns 0 only if both elements have the same exact position or if both elements have NoSourcePosition and are equals. Otherwise we return -1 (i.e. order them aribtrarily). This way our entry set is pseudo order but does not delete unequals elements.

@MartinWitt
Copy link
Collaborator

MartinWitt commented Oct 7, 2020

I tried to write down the math why this is wrong in #3519. If we return -1, we have a sleeping but and wait till the sort algorithm finds this.

* Returns 1 if o2 is after o1 in the file, or o2 has no valid position.
*/
@Override
public int compare(CtElement o1, CtElement o2) {
if (!o1.getPosition().isValidPosition() && !o2.getPosition().isValidPosition()) {
return 0;
return o1.equals(o2) ? 0 : -1;
Copy link
Collaborator Author

@nharrand nharrand Oct 7, 2020

Choose a reason for hiding this comment

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

If you want to respect the antisymetric property, this line could be replaced with

return o1.equals(o2) ? 0 : ((o1.hashCode() < o2.hashCode()) ? -1 : 1);

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice fix. At first glance I see no problem with transitive and antisymmetric property. LGTM.

@monperrus monperrus changed the title fix(CtLineElementComparator): element with NoSourcePosition are no ordered (arbitrarily) fix(CtLineElementComparator): one can add two values to an annotation Oct 8, 2020
@monperrus monperrus merged commit 97e8752 into INRIA:master Oct 8, 2020
@monperrus
Copy link
Collaborator

Thanks @nharrand

@monperrus monperrus mentioned this pull request Oct 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Possible regression on adding values to CtAnnotation
3 participants