Revert "[java-psi] IDEA-377693 Support unbounded wildcards"

This reverts commit 5283895c887c59b3b4386c51ce2a06c67688e170.

GitOrigin-RevId: b56c4733616caed180dc170d6b73a30d0eb31321
This commit is contained in:
Tagir Valeev
2025-10-01 15:50:23 +00:00
committed by intellij-monorepo-bot
parent 246ef04856
commit ca97498b37
9 changed files with 18 additions and 74 deletions
@@ -39,14 +39,12 @@ final class CheckerFrameworkSupport implements AnnotationPackageSupport {
if (result == null) return ContextNullabilityInfo.EMPTY;
return ContextNullabilityInfo.constant(result)
.filtering(context -> {
// Unbounded type elements should not be processed
if (context instanceof PsiTypeElement typeElement && typeElement.isUnboundedWildcard()) return false;
// DefaultQualifier is not applicable to type parameter declarations
if (context instanceof PsiTypeParameter) return false;
// DefaultQualifier is not applicable to type parameter uses
return !(PsiUtil.getTypeByPsiElement(context) instanceof PsiClassType classType) ||
!(classType.resolve() instanceof PsiTypeParameter);
});
// DefaultQualifier is not applicable to type parameter declarations
if (context instanceof PsiTypeParameter) return false;
// DefaultQualifier is not applicable to type parameter uses
return !(PsiUtil.getTypeByPsiElement(context) instanceof PsiClassType classType) ||
!(classType.resolve() instanceof PsiTypeParameter);
});
}
return ContextNullabilityInfo.EMPTY;
}
@@ -28,14 +28,15 @@ public final class JSpecifyAnnotationSupport implements AnnotationPackageSupport
String name = anno.getQualifiedName();
if (name == null) return ContextNullabilityInfo.EMPTY;
if (ArrayUtil.contains(PsiAnnotation.TargetType.LOCAL_VARIABLE, types)) return ContextNullabilityInfo.EMPTY;
ContextNullabilityInfo base = switch (name) {
case DEFAULT_NOT_NULL -> ContextNullabilityInfo.constant(new NullabilityAnnotationInfo(anno, Nullability.NOT_NULL, true))
.withNullabilityInContext(context -> context instanceof PsiTypeElement typeElement && typeElement.isUnboundedWildcard(),
Nullability.NULLABLE);
case DEFAULT_NULLNESS_UNKNOWN -> ContextNullabilityInfo.constant(new NullabilityAnnotationInfo(anno, Nullability.UNKNOWN, true));
default -> ContextNullabilityInfo.EMPTY;
};
return base
Nullability nullability;
switch (name) {
case DEFAULT_NOT_NULL -> nullability = Nullability.NOT_NULL;
case DEFAULT_NULLNESS_UNKNOWN -> nullability = Nullability.UNKNOWN;
default -> {
return ContextNullabilityInfo.EMPTY;
}
}
return ContextNullabilityInfo.constant(new NullabilityAnnotationInfo(anno, nullability, true))
.disableInCast()
.filtering(context -> !resolvesToTypeParameter(context));
}
@@ -34,12 +34,6 @@ interface ContextNullabilityInfo {
public @NotNull ContextNullabilityInfo filtering(@NotNull Predicate<@NotNull PsiElement> contextFilter) {
return this;
}
@Override
public @NotNull ContextNullabilityInfo withNullabilityInContext(@NotNull Predicate<@NotNull PsiElement> contextFilter,
@NotNull Nullability nullability) {
return this;
}
};
/**
@@ -74,15 +68,6 @@ interface ContextNullabilityInfo {
default @NotNull ContextNullabilityInfo filtering(@NotNull Predicate<@NotNull PsiElement> contextFilter) {
return context -> contextFilter.test(context) ? forContext(context) : null;
}
default @NotNull ContextNullabilityInfo withNullabilityInContext(@NotNull Predicate<@NotNull PsiElement> contextFilter,
@NotNull Nullability nullability) {
return context -> {
NullabilityAnnotationInfo info = forContext(context);
if (info == null) return null;
return contextFilter.test(context) ? new NullabilityAnnotationInfo(info.getAnnotation(), nullability, info.isContainer()) : info;
};
}
/**
* @return a new {@code ContextNullabilityInfo} that filters out the cast contexts.
@@ -81,9 +81,7 @@ public final class TypeNullability {
if (this.nullability() == Nullability.NULLABLE && this.source() instanceof NullabilitySource.ExtendsBound) {
return nullability;
}
if ((nullability.nullability() == Nullability.NOT_NULL ||
nullability.nullability() == Nullability.UNKNOWN && !nullability.source().equals(NullabilitySource.Standard.NONE))
&& this.source() instanceof NullabilitySource.ExtendsBound) {
if (nullability.nullability() == Nullability.NOT_NULL && this.source() instanceof NullabilitySource.ExtendsBound) {
return nullability;
}
if (this.source() == NullabilitySource.Standard.NONE) {
@@ -58,18 +58,6 @@ public interface PsiTypeElement extends PsiElement, PsiAnnotationOwner {
return false;
}
/**
* Returns true if the type element represents an unbounded wildcard type like {@code ?} (possibly annotated).
* Strictly speaking, {@code isUnboundedWildcard() == getType() instanceof PsiWildcardType wt && !wt.isBounded()}.
* This method can be helpful for nullability annotation processing to avoid computing the type,
* as the type may in turn depend on the nullability.
*
* @return true if the type element represents an unbounded wildcard.
*/
default boolean isUnboundedWildcard() {
return getText().endsWith("?");
}
/**
* @return false if annotations cannot be added to this type element
* For example, the JVM language that doesn't support type-use annotations;
@@ -74,13 +74,11 @@ public final class PsiWildcardType extends PsiType.Stub implements JvmWildcardTy
@Override
public @NotNull PsiWildcardType withNullability(@NotNull TypeNullability nullability) {
if (nullability == getNullability()) return this;
return new PsiWildcardType(myManager, myIsExtending, myBound, getAnnotationProvider(), nullability);
}
@Override
public @NotNull PsiType annotate(@NotNull TypeAnnotationProvider provider) {
if (getAnnotationProvider() == provider) return this;
return new PsiWildcardType(myManager, myIsExtending, myBound, provider);
}
@@ -92,11 +92,6 @@ public class ClsTypeElementImpl extends ClsElementImpl implements PsiTypeElement
return null;
}
}
@Override
public boolean isUnboundedWildcard() {
return myVariance == VARIANCE_INVARIANT;
}
public String getCanonicalText() {
return decorateTypeText(myTypeText);
@@ -1,8 +1,6 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.psi.impl.source;
import com.intellij.codeInsight.NullabilityAnnotationInfo;
import com.intellij.codeInsight.NullableNotNullManager;
import com.intellij.codeInsight.daemon.impl.analysis.JavaGenericsUtil;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.util.RecursionGuard;
@@ -51,11 +49,6 @@ public class PsiTypeElementImpl extends CompositePsiElement implements PsiTypeEl
return CachedValuesManager.getProjectPsiDependentCache(this, __ -> calculateType());
}
@Override
public boolean isUnboundedWildcard() {
return PsiUtil.isJavaToken(getLastChild(), JavaTokenType.QUEST);
}
private @NotNull PsiType calculateType() {
PsiType inferredType = PsiAugmentProvider.getInferredType(this);
if (inferredType != null) {
@@ -125,17 +118,6 @@ public class PsiTypeElementImpl extends CompositePsiElement implements PsiTypeEl
type = PsiWildcardType.createUnbounded(getManager());
}
type = type.annotate(createProvider(annotations));
if (isUnboundedWildcard()) {
// For bounded wildcard, nullability is defined by the bound.
// For unbounded, however, the context default annotation may affect the nullability, so we have to take this into account.
NullableNotNullManager manager = NullableNotNullManager.getInstance(getProject());
if (manager != null) {
NullabilityAnnotationInfo nullability = manager.findDefaultTypeUseNullability(this);
if (nullability != null) {
type = type.withNullability(nullability.toTypeNullability());
}
}
}
break;
}
else {
@@ -29,9 +29,6 @@ public class JSpecifyFilteredAnnotationTest extends JSpecifyAnnotationTest {
new SkipIndividuallyFilter( //each case has its own reason (line number starts from 0)
Set.of(
new Pair<>("CastWildcardToTypeVariable.java", 21), // see: IDEA-377686
new Pair<>("CastOfCaptureOfUnboundedWildcardForNotNullMarkedObjectBoundedTypeParameter.java", 27), // see: jspecify issue #771
new Pair<>("NotNullMarkedTypeVariableBound.java", 51), // see: jspecify issue #771
new Pair<>("NotNullMarkedTypeVariableBound.java", 56), // see: jspecify issue #771
new Pair<>("ContravariantReturns.java", 32), // see: IDEA-377687
new Pair<>("ContravariantReturns.java", 36), // see: IDEA-377687
@@ -57,6 +54,8 @@ public class JSpecifyFilteredAnnotationTest extends JSpecifyAnnotationTest {
new Pair<>("TypeVariableUnionNullToSelf.java", 118), // see: IDEA-377691
new Pair<>("TypeVariableToParent.java", 94), // see: IDEA-377691
new Pair<>("NullUnmarkedUndoesNullMarkedForWildcards.java", 23), // see: IDEA-377693
new Pair<>("SuperObject.java", 31), // see: IDEA-377694
new Pair<>("SuperTypeVariable.java", 28), // see: IDEA-377694
new Pair<>("SuperTypeVariable.java", 57), // see: IDEA-377694