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

WIP Reconsider contracts for derived type bindings #2211

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -2323,7 +2323,17 @@ public void addResolutionListener(IQualifiedTypeResolutionListener resolutionLis


public TypeBinding getUnannotatedType(TypeBinding typeBinding) {
return this.typeSystem.getUnannotatedType(typeBinding);
TypeBinding unannotatedType = this.typeSystem.getUnannotatedType(typeBinding);
if (typeBinding.getClass() == TypeVariableBinding.class && unannotatedType.hasTypeAnnotations()) {
for (TypeBinding derived : this.typeSystem.getAnnotatedTypes(unannotatedType)) {
if (!derived.hasTypeAnnotations())
return derived;
}
TypeBinding clone = ((TypeVariableBinding) unannotatedType).clone(null);
this.typeSystem.cacheDerivedType(unannotatedType, unannotatedType, clone);
return clone;
}
return unannotatedType;
}

// Given a type, return all its variously annotated versions.
Expand Down
Expand Up @@ -20,7 +20,6 @@
package org.eclipse.jdt.internal.compiler.lookup;

import java.util.HashMap;
import java.util.function.Consumer;

import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
Expand Down Expand Up @@ -83,26 +82,11 @@ public PTBKey(ReferenceBinding type, TypeBinding[] arguments, ReferenceBinding e
if (type instanceof UnresolvedReferenceBinding)
((UnresolvedReferenceBinding) type).addWrapper(this, environment);
if (arguments != null) {
for (int i = 0; i < arguments.length; i++) {
TypeBinding argument = arguments[i];
for (TypeBinding argument : arguments) {
if (argument instanceof UnresolvedReferenceBinding)
((UnresolvedReferenceBinding) argument).addWrapper(this, environment);
if (argument.hasNullTypeAnnotations())
this.tagBits |= TagBits.HasNullTypeAnnotation;
if (argument.getClass() == TypeVariableBinding.class) {
final int idx = i;
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;
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)
previousConsumer.accept(newTvb);
};
}
}
}
}
Expand Down Expand Up @@ -240,6 +224,18 @@ public final TypeBinding getUnannotatedType(TypeBinding type) {
type = resolvedType;
}
}
if (type.getClass() == TypeVariableBinding.class) {
// register the type as normal ...
if (type.id == TypeIds.NoId) {
int typesLength = this.types.length;
if (this.typeid == typesLength)
System.arraycopy(this.types, 0, this.types = new TypeBinding[typesLength * 2][], 0, typesLength);
this.types[type.id = this.typeid++] = new TypeBinding[4];
this.types[type.id][0] = type;
}
// ... but rely on prototype() for getting the "original" type as per the type parameter declaration
return type.prototype();
}
try {
if (type.id == TypeIds.NoId) {
if (type.hasTypeAnnotations())
Expand Down Expand Up @@ -269,16 +265,13 @@ public final TypeBinding getUnannotatedType(TypeBinding type) {
* If it itself is already registered as the key unannotated type of its family,
* create a clone to play that role from now on and swap types in the types cache.
*/
public void forceRegisterAsDerived(TypeVariableBinding derived) {
public void forceRegisterAsDerived(TypeBinding derived) {
int id = derived.id;
if (id != TypeIds.NoId && this.types[id] != null) {
TypeBinding unannotated = this.types[id][0];
if (unannotated == derived) { //$IDENTITY-COMPARISON$
// was previously registered as unannotated, replace by a fresh clone to remain unannotated:
this.types[id][0] = unannotated = derived.clone(null);
if (derived.updateWhenSettingTypeAnnotations != null) {
derived.updateWhenSettingTypeAnnotations.accept((TypeVariableBinding) unannotated);
}
}
// proceed as normal:
cacheDerivedType(unannotated, derived);
Expand Down
Expand Up @@ -44,7 +44,6 @@

import java.util.Arrays;
import java.util.Set;
import java.util.function.Consumer;

import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
Expand Down Expand Up @@ -78,13 +77,10 @@
public char[] genericTypeSignature;
LookupEnvironment environment;

/*
* In one particular situation a TVB will be cloned and the clone will be used as the 'naked' type
* within TypeSystem. This may require some updating inside TypeSystem's hash structure.
*/
Consumer<TypeVariableBinding> updateWhenSettingTypeAnnotations;
private ReferenceBinding prototype;

public TypeVariableBinding(char[] sourceName, Binding declaringElement, int rank, LookupEnvironment environment) {
this.prototype = this;
this.sourceName = sourceName;
this.declaringElement = declaringElement;
this.rank = rank;
Expand All @@ -97,6 +93,7 @@

// for subclass CaptureBinding
protected TypeVariableBinding(char[] sourceName, LookupEnvironment environment) {
this.prototype = this;
this.sourceName = sourceName;
this.modifiers = ClassFileConstants.AccPublic | ExtraCompilerModifiers.AccGenericSignature; // treat type var as public
this.tagBits |= TagBits.HasTypeVariable;
Expand All @@ -122,8 +119,13 @@
this.environment = prototype.environment;
prototype.tagBits |= TagBits.HasAnnotatedVariants;
this.tagBits &= ~TagBits.HasAnnotatedVariants;
this.prototype = prototype.prototype;
}

@Override
public TypeBinding prototype() {
return this.prototype;
}
/**
* Returns true if the argument type satisfies all bounds of the type parameter
* @param location if non-null this may be used for reporting errors relating to null type annotations (if enabled)
Expand Down Expand Up @@ -955,7 +957,7 @@
typeVariables = ((MethodBinding) this.declaringElement).typeVariables();
}
if (typeVariables != null && typeVariables.length > this.rank) {
TypeVariableBinding prototype = typeVariables[this.rank];

Check warning on line 960 in org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeVariableBinding.java

View check run for this annotation

Jenkins - Eclipse JDT / Compiler and API Tools

Name Shadowing Conflict

NORMAL: The local variable prototype is hiding a field from type TypeVariableBinding
if (prototype != this)//$IDENTITY-COMPARISON$
prototype.appendNullAnnotation(nameBuffer, options);
}
Expand Down