Skip to content

Commit

Permalink
fix PTBKey updateWhenSettingTypeAnnotations
Browse files Browse the repository at this point in the history
Changing PTBKey.arguments[idx] changes PTBKey.equals() and
PTBKey.hashCode().
So the old PTBKey has to be removed and a new has to be inserted.
It was wrong to keep the PTBKey in hashedParameterizedTypes. Found by
codereview. It is not clear if that could produce an error other then
having bad performance by having garbage entries in the HashMap that
could furthermore not found because the hash/equals contract was
violated.

Code is executed for example during
compiler.apt.tests.Java8ElementsTests.testTypeAnnotations()

relates to
eclipse-jdt#3412
  • Loading branch information
EcljpseB0T committed Dec 11, 2024
1 parent c2a99e6 commit 0372f90
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ public PTBKey(ReferenceBinding type, TypeBinding[] arguments, ReferenceBinding e
TypeVariableBinding typeVariableBinding = (TypeVariableBinding) argument;
Consumer<TypeVariableBinding> previousConsumer = typeVariableBinding.updateWhenSettingTypeAnnotations;
typeVariableBinding.updateWhenSettingTypeAnnotations = (newTvb) -> {
// update the TVB argument and simulate a re-hash:
ParameterizedTypeBinding[] value = HashedParameterizedTypes.this.hashedParameterizedTypes.get(this);
arguments[idx] = newTvb;
// Changing arguments[idx] changes PTBKey.equals() and PTBKey.hashCode().
// So the old PTBKey has to be removed and a new has to be inserted:
ParameterizedTypeBinding[] value = HashedParameterizedTypes.this.hashedParameterizedTypes.remove(this);
this.arguments[idx] = newTvb;
HashedParameterizedTypes.this.hashedParameterizedTypes.put(this, value);
// for the unlikely case of multiple PTBKeys referring to this TVB chain to the next consumer:
if (previousConsumer != null)
Expand Down

0 comments on commit 0372f90

Please sign in to comment.