Skip to content

Commit 8e87917

Browse files
"var" type inferrence bug with generic wrapping
+ Ignore type bound of "? extends Object" + Globally change expected errors from "? extends Object" to "?" + Changes in AnnotatableTypeSystem are a placeholder for now Fixes #5028
1 parent 4ea0cd7 commit 8e87917

6 files changed

Lines changed: 134 additions & 51 deletions

File tree

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/AnnotatableTypeSystem.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.eclipse.jdt.internal.compiler.lookup;
1818

1919
import org.eclipse.jdt.core.compiler.CharOperation;
20+
import org.eclipse.jdt.internal.compiler.ast.Wildcard;
2021
import org.eclipse.jdt.internal.compiler.util.Util;
2122

2223
/* AnnotatableTypeSystem: Keep track of annotated types so as to provide unique bindings for identically annotated versions identical underlying "naked" types.
@@ -187,6 +188,20 @@ public WildcardBinding getWildcard(ReferenceBinding genericType, int rank, TypeB
187188
if (genericType.hasTypeAnnotations())
188189
throw new IllegalStateException();
189190

191+
checkUnbounded: if (boundKind == Wildcard.EXTENDS && bound != null && bound.id == TypeIds.T_JavaLangObject) {
192+
if ((bound.tagBits & TagBits.AnnotationNullMASK) != 0)
193+
break checkUnbounded;
194+
if (otherBounds != null) {
195+
for (TypeBinding otherBound : otherBounds) {
196+
if ((otherBound.tagBits & TagBits.AnnotationNullMASK) != 0)
197+
break checkUnbounded;
198+
}
199+
}
200+
boundKind = Wildcard.UNBOUND;
201+
bound = null;
202+
otherBounds = null;
203+
}
204+
190205
WildcardBinding nakedType = null;
191206
boolean useDerivedTypesOfBound = bound instanceof TypeVariableBinding || (bound instanceof ParameterizedTypeBinding && !(bound instanceof RawTypeBinding)) ;
192207
TypeBinding[] derivedTypes = getDerivedTypes(useDerivedTypesOfBound ? bound : genericType);

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeSystem.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.HashMap;
2323
import java.util.function.Supplier;
2424
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
25+
import org.eclipse.jdt.internal.compiler.ast.Wildcard;
2526
import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
2627
import org.eclipse.jdt.internal.compiler.util.Util;
2728

@@ -373,6 +374,10 @@ public RawTypeBinding getRawType(ReferenceBinding genericType, ReferenceBinding
373374
public WildcardBinding getWildcard(ReferenceBinding genericType, int rank, TypeBinding bound, TypeBinding[] otherBounds, int boundKind) {
374375
if (genericType == null) // pseudo wildcard denoting composite bounds for lub computation
375376
genericType = ReferenceBinding.LUB_GENERIC;
377+
if (boundKind == Wildcard.EXTENDS && bound != null && bound.id == TypeIds.T_JavaLangObject && otherBounds == null) {
378+
boundKind = Wildcard.UNBOUND;
379+
bound = null;
380+
}
376381

377382
ReferenceBinding unannotatedGenericType = (ReferenceBinding) getUnannotatedType(genericType);
378383
int otherBoundsLength = otherBounds == null ? 0: otherBounds.length;

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
package org.eclipse.jdt.internal.compiler.problem;
8282

8383
import java.io.CharConversionException;
84+
import java.util.Arrays;
8485
import java.util.HashMap;
8586
import java.util.List;
8687
import java.util.Map;
@@ -10080,7 +10081,7 @@ public void anonymousDiamondWithNonDenotableTypeArguments(TypeReference type, Ty
1008010081
type.sourceStart,
1008110082
type.sourceEnd);
1008210083
}
10083-
public void redundantSpecificationOfTypeArguments(ASTNode location, TypeBinding[] argumentTypes) {
10084+
public void redundantSpecificationOfTypeArguments(TypeReference location, TypeBinding[] argumentTypes) {
1008410085
int severity = computeSeverity(IProblem.RedundantSpecificationOfTypeArguments);
1008510086
if (severity != ProblemSeverities.Ignore) {
1008610087
int sourceStart = -1;
@@ -10090,10 +10091,36 @@ public void redundantSpecificationOfTypeArguments(ASTNode location, TypeBinding[
1009010091
} else {
1009110092
sourceStart = location.sourceStart;
1009210093
}
10094+
String problemArguments;
10095+
String messageArguments;
10096+
class FindWildcard extends TypeBindingVisitor {
10097+
boolean found;
10098+
@Override
10099+
public boolean visit(WildcardBinding wildcardBinding) {
10100+
this.found = true;
10101+
return false;
10102+
}
10103+
}
10104+
FindWildcard find = new FindWildcard();
10105+
TypeBindingVisitor.visit(find, argumentTypes);
10106+
TypeReference[] typeArguments = null;
10107+
if (find.found) {
10108+
// when wildcards are in the mix then prefer showing type references, rather than processed bindings:
10109+
if (location instanceof ParameterizedSingleTypeReference pstr)
10110+
typeArguments = pstr.typeArguments;
10111+
else if (location instanceof ParameterizedQualifiedTypeReference pqtr)
10112+
typeArguments = pqtr.typeArguments[pqtr.typeArguments.length-1];
10113+
}
10114+
if (typeArguments != null) {
10115+
problemArguments = messageArguments = Arrays.stream(typeArguments).map(TypeReference::toString).collect(Collectors.joining(", ")); //$NON-NLS-1$
10116+
} else {
10117+
problemArguments = typesAsString(argumentTypes, false);
10118+
messageArguments = typesAsString(argumentTypes, true);
10119+
}
1009310120
this.handle(
1009410121
IProblem.RedundantSpecificationOfTypeArguments,
10095-
new String[] {typesAsString(argumentTypes, false)},
10096-
new String[] {typesAsString(argumentTypes, true)},
10122+
new String[] {problemArguments},
10123+
new String[] {messageArguments},
1009710124
severity,
1009810125
sourceStart,
1009910126
location.sourceEnd);

0 commit comments

Comments
 (0)