diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/InferenceSession.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/InferenceSession.java index f39835e944fd..7ad315c23716 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/InferenceSession.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/InferenceSession.java @@ -634,13 +634,17 @@ public class InferenceSession { PsiType targetType = getTargetTypeFromParent(context, false); if (targetType != null && !PsiType.VOID.equals(targetType)) { registerReturnTypeConstraints( - PsiUtil.isRawSubstitutor(method, mySiteSubstitutor) ? returnType : mySiteSubstitutor.substitute(returnType), targetType); + PsiUtil.isRawSubstitutor(method, mySiteSubstitutor) ? returnType : mySiteSubstitutor.substitute(returnType), targetType, true); } } } } public void registerReturnTypeConstraints(PsiType returnType, PsiType targetType) { + registerReturnTypeConstraints(returnType, targetType, false); + } + + public void registerReturnTypeConstraints(PsiType returnType, PsiType targetType, boolean toplevel) { returnType = substituteWithInferenceVariables(returnType); final InferenceVariable inferenceVariable = shouldResolveAndInstantiate(returnType, targetType); if (inferenceVariable != null) { @@ -656,10 +660,25 @@ public class InferenceSession { final PsiClass psiClass = resolveResult.getElement(); if (psiClass != null) { LOG.assertTrue(returnType instanceof PsiClassType); - final PsiTypeParameter[] typeParameters = psiClass.getTypeParameters(); - InferenceVariable[] copy = initBounds(null, typeParameters); - final PsiType substitutedCapture = PsiImplUtil.normalizeWildcardTypeByPosition(returnType, (PsiExpression)myContext); - myIncorporationPhase.addCapture(copy, (PsiClassType)substituteWithInferenceVariables(returnType)); + PsiClassType substitutedCapture = (PsiClassType)returnType; + if (!toplevel) { + substitutedCapture = (PsiClassType)PsiImplUtil.normalizeWildcardTypeByPosition(returnType, (PsiExpression)myContext); + final PsiTypeParameter[] typeParameters = psiClass.getTypeParameters(); + final InferenceVariable[] copy = initBounds(null, typeParameters); + + final PsiType[] parameters = substitutedCapture.getParameters(); + final PsiType[] newParameters = new PsiType[parameters.length]; + final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(myManager.getProject()); + for (int i = 0; i < parameters.length; i++) { + newParameters[i] = parameters[i]; + if (parameters[i] instanceof PsiCapturedWildcardType) { + newParameters[i] = elementFactory.createType(copy[i]); + } + } + substitutedCapture = elementFactory.createType(psiClass, newParameters); + + myIncorporationPhase.addCapture(copy, substitutedCapture); + } addConstraint(new TypeCompatibilityConstraint(targetType, substitutedCapture)); } } else {