[ann] Exceptions with interfaces in multi-catch: assignability and control flow fix

This commit is contained in:
Roman Shevchenko
2011-05-18 17:15:49 +04:00
parent 46b715b02d
commit 49e9988a40
4 changed files with 56 additions and 21 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@ import com.intellij.psi.jsp.JavaJspElementVisitor;
import com.intellij.psi.jsp.JspFile;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.containers.Stack;
import gnu.trove.THashMap;
import gnu.trove.TIntArrayList;
@@ -947,10 +946,8 @@ class ControlFlowAnalyzer extends JavaJspElementVisitor {
for (int i = myCatchParameters.size() - 1; i >= 0; i--) {
ProgressManager.checkCanceled();
PsiParameter parameter = myCatchParameters.get(i);
final PsiType type = parameter.getType();
PsiClass caughtClass = PsiUtil.resolveClassInType(type);
if (caughtClass == null) continue;
if (type.isAssignableFrom(throwType) || throwType.isAssignableFrom(type)) {
PsiType catchType = parameter.getType();
if (catchType.isAssignableFrom(throwType) || throwType.isAssignableFrom(catchType)) {
blocks.add(myCatchBlocks.get(i));
}
}
@@ -1,3 +1,18 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
abstract class C {
private static class NE { }
private static class E extends Exception { public String s; }
@@ -6,9 +21,9 @@ abstract class C {
private static class E3 extends E { }
private static class E4 extends E { }
private static class RE extends RuntimeException { }
private interface I<T> { }
private static class IE1 extends E implements I<Integer> { }
private static class IE2 extends E implements I<Long> { }
private interface I<T> { void i(); }
private static class IE1 extends E implements I<Integer> { public void i() { } }
private static class IE2 extends E implements I<Long> { public void i() { } }
private static class F<X> { F(X x) { } }
abstract void f() throws E1, E2;
@@ -18,8 +33,7 @@ abstract class C {
try { f(); } catch (E1 | E2 e) { }
try { f(); } catch (E2 | E1 e) { e.printStackTrace(); System.out.println(e.s); }
try { f(); } catch (E2 | E1 e) { } catch (E e) { } catch (RE e) { }
try { f(); } catch (E1 | E2 e) { E ee = e; }
try { g(); } catch (IE1 | IE2 e) { E ee = e; I ii = e; }
try { g(); } catch (IE1 | IE2 e) { E ee = e; I ii = e; e.i(); }
try { g(); } catch (IE1 | IE2 e) { F<?> f = new F<>(e); }
try { g(); } catch (IE1 | IE2 e) { new F<I<? extends Number>>(e); }
@@ -33,12 +47,32 @@ abstract class C {
try { f(); } catch (<error descr="Types in multi-catch must be disjoint: 'C.E' is a subclass of 'C.E'">E</error> | E ignore) { }
try { f(); } catch (E1 | E2 | <error descr="Exception 'C.E3' is never thrown in the corresponding try block">E3</error> e) { }
try { f(); } catch (<error descr="Exception 'C.E3' is never thrown in the corresponding try block">E3</error> | <error descr="Exception 'C.E4' is never thrown in the corresponding try block">E4</error> | RE e) { }
try { f(); } catch (<error descr="Exception 'C.E3' is never thrown in the corresponding try block">E3</error> | <error descr="Exception 'C.E4' is never thrown in the corresponding try block">E4</error> | RE e) { } catch (E e) { }
try { <error descr="Unhandled exceptions: C.E1, C.E2">f();</error> } catch (E3 | E4 | RE e) { }
try { f(); } catch (E e) { } catch (<error descr="Exception 'C.E1' has already been caught">E1</error> | <error descr="Exception 'C.E3' has already been caught">E3</error> e) { }
try { f(); } catch (E1 | E2 e) { } catch (<error descr="Exception 'C.E2' has already been caught">E2</error> e) { }
try { f(); } catch (E1 | E2 e) { } catch (E e) { <error descr="Incompatible types. Found: 'C.E', required: 'C.E1'">E1 ee = e;</error> }
try { f(); } catch (E | RE e) { <error descr="Cannot assign a value to final variable 'e'">e = null</error>; }
try { f(); } catch (E1 | E2 e) { <error descr="Incompatible types. Found: 'C.E1 | C.E2', required: 'C.E2'">E2 ee = e;</error> }
try { f(); } catch (E1 | E2 e) { <error descr="Cannot assign a value to final variable 'e'">e = new E1()</error>; }
try { f(); } catch (E1 | E2 e) { <error descr="Incompatible types. Found: 'C.E', required: 'C.E1 | C.E2'">e = new E()</error>; }
}
}
class D {
static class E extends Exception { }
static interface I { void i(); }
static class E1 extends E implements I { public void i() { } }
static class E2 extends E implements I { public void i() { } }
void m(boolean f) {
try {
if (f)
throw new E1();
else
throw new E2();
} catch (E1|E2 e) {
System.out.println(e);
}
}
}
@@ -116,10 +116,11 @@ public class PsiDisjunctionType extends PsiType {
@Override
public PsiType[] getSuperTypes() {
final PsiType lub = getLeastUpperBound();
final PsiType[] superTypes = lub.getSuperTypes();
final PsiType[] result = new PsiType[superTypes.length + 1];
result[0] = lub;
System.arraycopy(superTypes, 0, result, 1, superTypes.length);
return result;
if (lub instanceof PsiIntersectionType) {
return ((PsiIntersectionType)lub).getConjuncts();
}
else {
return new PsiType[]{lub};
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -672,7 +672,10 @@ public class TypeConversionUtil {
}
if (left instanceof PsiDisjunctionType) {
return isAssignable(((PsiDisjunctionType)left).getLeastUpperBound(), right, allowUncheckedConversion);
for (PsiType type : ((PsiDisjunctionType)left).getDisjunctions()) {
if (isAssignable(type, right, allowUncheckedConversion)) return true;
}
return false;
}
if (right instanceof PsiDisjunctionType) {
return isAssignable(left, ((PsiDisjunctionType)right).getLeastUpperBound(), allowUncheckedConversion);