Mark only unreachable parts of element if it has reachable parts

like for 'return todo()' mark only 'return'
This commit is contained in:
Svetlana Isakova
2014-06-11 19:35:00 +04:00
parent 88ecc5cc59
commit 79cec6411d
38 changed files with 597 additions and 84 deletions
@@ -124,9 +124,9 @@ public class JetFlowInformationProvider {
}
public void checkFunction(@Nullable JetType expectedReturnType) {
Set<JetElement> unreachableElements = collectUnreachableCode();
checkDefiniteReturn(expectedReturnType != null ? expectedReturnType : NO_EXPECTED_TYPE, unreachableElements);
reportUnreachableCode(unreachableElements);
UnreachableCode unreachableCode = collectUnreachableCode();
checkDefiniteReturn(expectedReturnType != null ? expectedReturnType : NO_EXPECTED_TYPE, unreachableCode);
reportUnreachableCode(unreachableCode);
markTailCalls();
}
@@ -209,7 +209,7 @@ public class JetFlowInformationProvider {
}
}
public void checkDefiniteReturn(final @NotNull JetType expectedReturnType, @NotNull final Set<JetElement> unreachableElements) {
public void checkDefiniteReturn(final @NotNull JetType expectedReturnType, @NotNull final UnreachableCode unreachableCode) {
assert subroutine instanceof JetDeclarationWithBody;
JetDeclarationWithBody function = (JetDeclarationWithBody) subroutine;
@@ -236,7 +236,7 @@ public class JetFlowInformationProvider {
if (blockBody && !noExpectedType(expectedReturnType)
&& !KotlinBuiltIns.getInstance().isUnit(expectedReturnType)
&& !unreachableElements.contains(element)) {
&& !unreachableCode.getElements().contains(element)) {
noReturnError[0] = true;
}
}
@@ -247,19 +247,17 @@ public class JetFlowInformationProvider {
}
}
private void reportUnreachableCode(@NotNull Set<JetElement> unreachableElements) {
// This is needed in order to highlight only '1 < 2' and not '1', '<' and '2' as well
Set<JetElement> rootUnreachableElements = JetPsiUtil.findRootExpressions(unreachableElements);
for (JetElement element : rootUnreachableElements) {
trace.report(UNREACHABLE_CODE.on(element));
private void reportUnreachableCode(@NotNull UnreachableCode unreachableCode) {
for (JetElement element : unreachableCode.getElements()) {
trace.report(UNREACHABLE_CODE.on(element, unreachableCode.getUnreachableTextRanges(element)));
}
}
private Set<JetElement> collectUnreachableCode() {
@NotNull
private UnreachableCode collectUnreachableCode() {
Set<JetElement> reachableElements = Sets.newHashSet();
Set<JetElement> unreachableElements = Sets.newHashSet();
for (Instruction instruction : pseudocode.getDeadInstructions()) {
if (!PseudocodeUtil.isDeadInAllCopies(instruction)) continue;
for (Instruction instruction : pseudocode.getInstructionsIncludingDeadCode()) {
if (!(instruction instanceof JetElementInstruction)
|| instruction instanceof LoadUnitValueInstruction
|| instruction instanceof MergeInstruction
@@ -275,9 +273,14 @@ public class JetFlowInformationProvider {
if (!isJumpElement) continue;
}
unreachableElements.add(element);
if (PseudocodeUtil.isDeadInAllCopies(instruction)) {
unreachableElements.add(element);
}
else {
reachableElements.add(element);
}
}
return unreachableElements;
return new UnreachableCodeImpl(reachableElements, unreachableElements);
}
////////////////////////////////////////////////////////////////////////////////
@@ -0,0 +1,119 @@
/*
* Copyright 2010-2014 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.
*/
package org.jetbrains.jet.lang.cfg
import org.jetbrains.jet.lang.psi.JetElement
import com.intellij.openapi.util.TextRange
import java.util.HashSet
import org.jetbrains.jet.lang.psi.JetPsiUtil
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiWhiteSpace
import java.util.ArrayList
import org.jetbrains.jet.lexer.JetTokens
import com.intellij.psi.util.PsiTreeUtil
trait UnreachableCode {
val elements: Set<JetElement>
fun getUnreachableTextRanges(element: JetElement): List<TextRange>
}
class UnreachableCodeImpl(
private val reachableElements: Set<JetElement>,
private val unreachableElements: Set<JetElement>
) : UnreachableCode {
// This is needed in order to highlight only '1 < 2' and not '1', '<' and '2' as well
override val elements = JetPsiUtil.findRootExpressions(unreachableElements)
override fun getUnreachableTextRanges(element: JetElement): List<TextRange> {
return if (element.hasChildrenInSet(reachableElements)) {
element.getLeavesOrReachableChildren().removeReachableElementsWithMeaninglessSiblings().mergeAdjacentTextRanges()
}
else {
listOf(element.getTextRange()!!)
}
}
private fun JetElement.hasChildrenInSet(set: Set<JetElement>): Boolean {
return PsiTreeUtil.collectElements(this) { it != this }.any { it in set }
}
private fun JetElement.getLeavesOrReachableChildren(): List<PsiElement> {
val children = ArrayList<PsiElement>()
acceptChildren(object : PsiElementVisitor() {
override fun visitElement(element: PsiElement) {
val isReachable = element is JetElement && reachableElements.contains(element) && !element.hasChildrenInSet(unreachableElements)
if (isReachable || element.getChildren().size == 0) {
children.add(element)
}
else {
element.acceptChildren(this)
}
}
})
return children
}
fun List<PsiElement>.removeReachableElementsWithMeaninglessSiblings(): List<PsiElement> {
fun PsiElement.isMeaningless() = this is PsiWhiteSpace || this.getNode()?.getElementType() == JetTokens.COMMA
val childrenToRemove = HashSet<PsiElement>()
fun collectSiblingsIfMeaningless(elementIndex: Int, direction: Int) {
val index = elementIndex + direction
if (index !in 0..(size() - 1)) return
val element = this[index]
if (element.isMeaningless()) {
childrenToRemove.add(element)
collectSiblingsIfMeaningless(index, direction)
}
}
for ((index, element) in this.withIndices()) {
if (reachableElements.contains(element)) {
childrenToRemove.add(element)
collectSiblingsIfMeaningless(index, -1)
collectSiblingsIfMeaningless(index, 1)
}
}
return this.filter { it !in childrenToRemove }
}
private fun List<PsiElement>.mergeAdjacentTextRanges(): List<TextRange> {
val result = ArrayList<TextRange>()
val lastRange = fold(null: TextRange?) {
currentTextRange, element ->
val elementRange = element.getTextRange()!!
if (currentTextRange == null) {
elementRange
}
else if (currentTextRange.getEndOffset() == elementRange.getStartOffset()) {
currentTextRange.union(elementRange)
}
else {
result.add(currentTextRange)
elementRange
}
}
if (lastRange != null) {
result.add(lastRange)
}
return result
}
}
@@ -27,6 +27,7 @@ public abstract class AbstractDiagnostic<E extends PsiElement> implements Parame
private final E psiElement;
private final DiagnosticFactoryWithPsiElement<E, ?> factory;
private final Severity severity;
private List<TextRange> textRanges;
public AbstractDiagnostic(@NotNull E psiElement,
@NotNull DiagnosticFactoryWithPsiElement<E, ?> factory,
@@ -63,6 +64,9 @@ public abstract class AbstractDiagnostic<E extends PsiElement> implements Parame
@Override
@NotNull
public List<TextRange> getTextRanges() {
if (textRanges != null) {
return textRanges;
}
return getFactory().getTextRanges(this);
}
@@ -71,4 +75,10 @@ public abstract class AbstractDiagnostic<E extends PsiElement> implements Parame
if (!getFactory().isValid(this)) return false;
return true;
}
@NotNull
public Diagnostic setTextRanges(@NotNull List<TextRange> textRanges) {
this.textRanges = textRanges;
return this;
}
}
@@ -30,7 +30,7 @@ public abstract class DiagnosticFactoryWithPsiElement<E extends PsiElement, D ex
}
protected List<TextRange> getTextRanges(ParametrizedDiagnostic<E> diagnostic) {
return positioningStrategy.mark(diagnostic.getPsiElement());
return positioningStrategy.markDiagnostic(diagnostic);
}
protected boolean isValid(ParametrizedDiagnostic<E> diagnostic) {
@@ -17,9 +17,11 @@
package org.jetbrains.jet.lang.diagnostics;
import com.google.common.collect.ImmutableSet;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
import kotlin.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*;
@@ -446,7 +448,13 @@ public interface Errors {
// Control flow / Data flow
DiagnosticFactory0<JetElement> UNREACHABLE_CODE = DiagnosticFactory0.create(WARNING);
DiagnosticFactory1<JetElement, List<TextRange>> UNREACHABLE_CODE = DiagnosticFactory1.create(
WARNING, PositioningStrategies.markTextRangesFromDiagnostic(new Function1<Diagnostic, List<TextRange>>() {
@Override
public List<TextRange> invoke(Diagnostic diagnostic) {
return UNREACHABLE_CODE.cast(diagnostic).getA();
}
}));
DiagnosticFactory0<JetVariableDeclaration> VARIABLE_WITH_NO_TYPE_NO_INITIALIZER = DiagnosticFactory0.create(ERROR, NAME_IDENTIFIER);
@@ -23,6 +23,7 @@ import com.intellij.lang.ASTNode;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNameIdentifierOwner;
import kotlin.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetNodeTypes;
import org.jetbrains.jet.lang.psi.*;
@@ -527,6 +528,18 @@ public class PositioningStrategies {
}
};
public static PositioningStrategy<PsiElement> markTextRangesFromDiagnostic(
@NotNull final Function1<Diagnostic, List<TextRange>> getTextRanges
) {
return new PositioningStrategy<PsiElement>() {
@NotNull
@Override
public List<TextRange> markDiagnostic(@NotNull ParametrizedDiagnostic<? extends PsiElement> diagnostic) {
return getTextRanges.invoke(diagnostic);
}
};
}
private PositioningStrategies() {
}
}
@@ -27,7 +27,12 @@ import java.util.List;
public class PositioningStrategy<E extends PsiElement> {
@NotNull
public List<TextRange> mark(@NotNull E element) {
public List<TextRange> markDiagnostic(@NotNull ParametrizedDiagnostic<? extends E> diagnostic) {
return mark(diagnostic.getPsiElement());
}
@NotNull
protected List<TextRange> mark(@NotNull E element) {
return markElement(element);
}
@@ -212,7 +212,7 @@ public class DefaultErrorMessages {
MAP.put(INITIALIZATION_USING_BACKING_FIELD_OPEN_SETTER,
"Setter of this property can be overridden, so initialization using backing field required", NAME);
MAP.put(UNREACHABLE_CODE, "Unreachable code");
MAP.put(UNREACHABLE_CODE, "Unreachable code", TO_STRING);
MAP.put(MANY_CLASS_OBJECTS, "Only one class object is allowed per class");
MAP.put(CLASS_OBJECT_NOT_ALLOWED, "A class object is not allowed here");
@@ -661,7 +661,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
JetType result;
if (operationType == JetTokens.PLUSPLUS || operationType == JetTokens.MINUSMINUS) {
assert returnType != null : "returnType is null for " + resolutionResults.getResultingDescriptor();
if (JetTypeChecker.INSTANCE.isSubtypeOf(returnType, KotlinBuiltIns.getInstance().getUnitType())) {
if (KotlinBuiltIns.getInstance().isUnit(returnType)) {
result = ErrorUtils.createErrorType(KotlinBuiltIns.getInstance().getUnit().getName().asString());
context.trace.report(INC_DEC_SHOULD_NOT_RETURN_UNIT.on(operationSign));
}
@@ -1,3 +1,5 @@
// !DIAGNOSTICS: -UNREACHABLE_CODE
fun none() {}
fun unitEmptyInfer() {}
@@ -51,7 +53,7 @@ fun blockAndAndMismatch1() : Int {
return <!TYPE_MISMATCH!>true && false<!>
}
fun blockAndAndMismatch2() : Int {
<!UNREACHABLE_CODE!>(return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>true<!>) && (return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>false<!>)<!>
(return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>true<!>) && (return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>false<!>)
}
fun blockAndAndMismatch3() : Int {
@@ -61,7 +63,7 @@ fun blockAndAndMismatch4() : Int {
return <!TYPE_MISMATCH!>true || false<!>
}
fun blockAndAndMismatch5() : Int {
<!UNREACHABLE_CODE!>(return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>true<!>) || (return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>false<!>)<!>
(return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>true<!>) || (return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>false<!>)
}
fun blockReturnValueTypeMatch1() : Int {
return if (1 > 2) <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1.0<!> else <!CONSTANT_EXPECTED_TYPE_MISMATCH!>2.0<!>
@@ -0,0 +1,12 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testCommasAndWhitespaces() {
fun bar(i: Int, s: String, x: Any) {}
<!UNREACHABLE_CODE!>bar(<!> 1 , todo() , <!UNREACHABLE_CODE!>"" )<!>
}
fun todo() = throw Exception()
@@ -0,0 +1,11 @@
fun testInvoke() {
fun Nothing.invoke() = this
todo()<!UNREACHABLE_CODE!>()<!>
}
fun testInvokeWithLambda() {
fun Nothing.invoke(<!UNUSED_PARAMETER!>i<!>: Int, f: () -> Int) = f
todo()<!UNREACHABLE_CODE!>(1){ 42 }<!>
}
fun todo() = throw Exception()
@@ -0,0 +1,11 @@
fun test11() {
fun Any.bar(<!UNUSED_PARAMETER!>i<!>: Int) {}
todo().<!UNREACHABLE_CODE!>bar(1)<!>
}
fun test12() {
fun Any.bar(<!UNUSED_PARAMETER!>i<!>: Int) {}
todo()<!UNNECESSARY_SAFE_CALL!>?.<!><!UNREACHABLE_CODE!>bar(1)<!>
}
fun todo() = throw Exception()
@@ -1,3 +1,4 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
fun t1() : Int{
return 0
@@ -46,7 +47,7 @@ fun t3() : Any {
<!UNREACHABLE_CODE!>1<!>
}
fun t4(<!UNUSED_PARAMETER!>a<!> : Boolean) : Int {
fun t4(a : Boolean) : Int {
do {
return 1
}
@@ -54,7 +55,7 @@ fun t4(<!UNUSED_PARAMETER!>a<!> : Boolean) : Int {
<!UNREACHABLE_CODE!>1<!>
}
fun t4break(<!UNUSED_PARAMETER!>a<!> : Boolean) : Int {
fun t4break(a : Boolean) : Int {
do {
break
}
@@ -109,7 +110,7 @@ fun t7() : Int {
<!UNREACHABLE_CODE!>2<!>
}
catch (<!TYPE_MISMATCH!>e : Any<!>) {
<!UNUSED_EXPRESSION!>2<!>
2
}
return 1 // this is OK, like in Java
}
@@ -127,16 +128,16 @@ fun t8() : Int {
}
fun blockAndAndMismatch() : Boolean {
<!UNREACHABLE_CODE!>(return true) || (return false)<!>
(return true) <!UNREACHABLE_CODE!>|| (return false)<!>
<!UNREACHABLE_CODE!>return true<!>
}
fun tf() : Int {
try {<!UNREACHABLE_CODE!>return 1<!>} finally{return 1}
try {<!UNREACHABLE_CODE!>return<!> 1} finally{return 1}
<!UNREACHABLE_CODE!>return 1<!>
}
fun failtest(<!UNUSED_PARAMETER!>a<!> : Int) : Int {
fun failtest(a : Int) : Int {
if (fail() || <!UNREACHABLE_CODE!>true<!>) <!UNREACHABLE_CODE!>{
}<!>
@@ -144,8 +145,8 @@ fun failtest(<!UNUSED_PARAMETER!>a<!> : Int) : Int {
}
fun foo(a : Nothing) : Unit {
<!UNUSED_EXPRESSION!>1<!>
<!UNUSED_EXPRESSION!>a<!>
1
a
<!UNREACHABLE_CODE!>2<!>
}
@@ -161,7 +162,7 @@ fun nullIsNotNothing() : Unit {
fail()
}
fun returnInWhile(<!UNUSED_PARAMETER!>a<!>: Int) {
fun returnInWhile(a: Int) {
do {return}
while (<!UNREACHABLE_CODE!>1 > a<!>)
}
@@ -1,7 +1,7 @@
package c
fun test1() {
<!UNREACHABLE_CODE!>val <!UNUSED_VARIABLE!>r<!>: Nothing = null!!<!>
<!UNREACHABLE_CODE!>val <!UNUSED_VARIABLE!>r<!>: Nothing =<!> null!!
}
fun test2(a: A) {
@@ -0,0 +1,40 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testArrayAccess1(array: Array<Any>) {
array<!UNREACHABLE_CODE!>[<!>todo()<!UNREACHABLE_CODE!>]<!>
}
fun testArrayAccess2() {
fun Nothing.get(i: Int, s: String) {}
todo()<!UNREACHABLE_CODE!>[1, ""]<!>
}
fun testAraryAccess3() {
fun Nothing.get(n: Nothing) {}
todo()<!UNREACHABLE_CODE!>[todo()]<!>
}
fun testArrayAssignment1(array: Array<Any>) {
array[todo()] <!UNREACHABLE_CODE!>= 11<!>
}
fun testArrayAssignment2(array: Array<Any>) {
array[1] <!UNREACHABLE_CODE!>=<!> todo()
}
fun testArrayAssignment3(n: Nothing) {
fun Nothing.set(i: Int, j: Int) {}
n<!UNREACHABLE_CODE!>[1] = 2<!>
}
fun testArrayAssignment4(n: Nothing) {
fun Nothing.set(i: Int, a: Any) {}
n<!UNREACHABLE_CODE!>[1] = todo()<!>
}
fun testArrayPlusAssign(array: Array<Any>) {
fun Any.plusAssign(a: Any) {}
array[1] <!UNREACHABLE_CODE!>+=<!> todo()
}
fun todo() = throw Exception()
@@ -0,0 +1,18 @@
fun testAssignment() {
var <!UNUSED_VARIABLE!>a<!> = 1
<!UNREACHABLE_CODE!>a =<!> todo()
}
fun testVariableDeclaration() {
<!UNREACHABLE_CODE!>val <!UNUSED_VARIABLE!>a<!> =<!> todo()
}
fun testPlusAssign() {
fun Int.plusAssign(<!UNUSED_PARAMETER!>i<!>: Int) {}
var a = 1
a <!UNREACHABLE_CODE!>+=<!> todo()
}
fun todo() = throw Exception()
@@ -0,0 +1,39 @@
fun testBinary1() {
fun Int.times(<!UNUSED_PARAMETER!>s<!>: String) {}
todo() <!UNREACHABLE_CODE!>* ""<!>
}
fun testBinary2() {
"1" <!UNREACHABLE_CODE!>+<!> todo()
}
fun testElvis1() {
<!USELESS_ELVIS!>todo()<!> <!UNREACHABLE_CODE!>?: ""<!>
}
fun testElvis2(s: String?) {
s ?: todo()
bar()
}
fun testAnd1(b: Boolean) {
b && todo()
bar()
}
fun testAnd2(<!UNUSED_PARAMETER!>b<!>: Boolean) {
todo() <!UNREACHABLE_CODE!>&& b<!>
}
fun returnInBinary1(): Boolean {
(return true) <!UNREACHABLE_CODE!>&& (return false)<!>
}
fun returnInBinary2(): Boolean {
(return true) <!UNREACHABLE_CODE!>|| (return false)<!>
}
fun todo() = throw Exception()
fun bar() {}
@@ -0,0 +1,13 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testArgumentInCall() {
fun bar(i: Int, s: String, x: Any) {}
<!UNREACHABLE_CODE!>bar(<!>1, todo(), <!UNREACHABLE_CODE!>"")<!>
}
fun testArgumentInVariableAsFunctionCall(f: (Any) -> Unit) {
f<!UNREACHABLE_CODE!>(<!>todo()<!UNREACHABLE_CODE!>)<!>
}
fun todo() = throw Exception()
@@ -0,0 +1,24 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun unreachable0() {
return
<!UNREACHABLE_CODE!>return todo()<!>
}
fun unreachable2() {
return
<!UNREACHABLE_CODE!>val a = todo()<!>
}
fun unreachable3() {
return
<!UNREACHABLE_CODE!>bar(todo())<!>
}
fun unreachable4(array: Array<Any>) {
return
<!UNREACHABLE_CODE!>array[todo()]<!>
}
fun bar(a: Any) {}
fun todo() = throw Exception()
@@ -0,0 +1,12 @@
fun testIf() {
if (todo()) <!UNREACHABLE_CODE!>1<!> else <!UNREACHABLE_CODE!>2<!>
}
fun testIf1(b: Boolean) {
if (b) todo() else 1
bar()
}
fun todo() = throw Exception()
fun bar() {}
@@ -0,0 +1,13 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testCompound() {
fun Nothing.get(i: Int) {}
todo()<!UNREACHABLE_CODE!><!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>[12]<!>
}
fun testCompound1() {
fun Int.times(s: String): Array<String> = throw Exception()
<!UNREACHABLE_CODE!>(<!>todo() <!UNREACHABLE_CODE!>* "")[1]<!>
}
fun todo() = throw Exception()
@@ -0,0 +1,36 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testObject() {
<!UNREACHABLE_CODE!>object : Foo(<!>todo()<!UNREACHABLE_CODE!>) {
fun foo() = 1
}<!>
}
fun testObjectExpression() {
<!UNREACHABLE_CODE!>val <!UNUSED_VARIABLE!>a<!> = object : Foo(<!>todo()<!UNREACHABLE_CODE!>) {
fun foo() = 1
}<!>
}
fun testObjectExpression1() {
fun bar(i: Int, x: Any) {}
<!UNREACHABLE_CODE!>bar(<!>1, <!UNREACHABLE_CODE!>object : Foo(<!>todo()<!UNREACHABLE_CODE!>) {
fun foo() = 1
})<!>
}
fun testClassDeclaration() {
class C : Foo(todo()) {}
<!UNREACHABLE_CODE!>bar()<!>
}
fun testFunctionDefaultArgument() {
fun foo(x: Int = todo()) { bar() }
}
open class Foo(i: Int) {}
fun todo() = throw Exception()
fun bar() {}
@@ -0,0 +1,21 @@
fun testFor() {
fun Nothing.iterator() = (0..1).iterator()
<!UNREACHABLE_CODE!>for (i in<!> todo()<!UNREACHABLE_CODE!>) {}<!>
}
fun testWhile() {
<!UNREACHABLE_CODE!>while (<!>todo()<!UNREACHABLE_CODE!>) {
}<!>
}
fun testDoWhile() {
do {
} while(todo())
<!UNREACHABLE_CODE!>bar()<!>
}
fun todo() = throw Exception()
fun bar() {}
@@ -0,0 +1,5 @@
fun testReturn() {
<!UNREACHABLE_CODE!>return<!> todo()
}
fun todo() = throw Exception()
@@ -0,0 +1,15 @@
fun testPrefix() {
fun Any.not() {}
<!UNREACHABLE_CODE!>!<!>todo()
}
fun testPostfixWithCall(n: Nothing) {
fun Nothing.inc(): Nothing = this
n<!UNREACHABLE_CODE!>++<!>
}
fun testPostfixSpecial() {
todo()<!UNNECESSARY_NOT_NULL_ASSERTION, UNREACHABLE_CODE!>!!<!>
}
fun todo() = throw Exception()
@@ -3,20 +3,20 @@ fun bar(<!UNUSED_PARAMETER!>a<!>: Any, <!UNUSED_PARAMETER!>b<!>: Any) {}
fun test(arr: Array<Int>) {
while (true) {
<!UNREACHABLE_CODE!>foo<!>(break)
<!UNREACHABLE_CODE!>foo(<!>break<!UNREACHABLE_CODE!>)<!>
}
while (true) {
<!UNREACHABLE_CODE!>bar<!>(arr, break)
<!UNREACHABLE_CODE!>bar(<!>arr, break<!UNREACHABLE_CODE!>)<!>
}
while (true) {
<!UNREACHABLE_CODE!>arr[break]<!>
arr<!UNREACHABLE_CODE!>[<!>break<!UNREACHABLE_CODE!>]<!>
}
while (true) {
<!UNREACHABLE_CODE!>arr[1] = break<!>
arr[1] <!UNREACHABLE_CODE!>=<!> break
}
while (true) {
@@ -32,7 +32,7 @@ fun test(arr: Array<Int>) {
while (true) {
var <!UNUSED_VARIABLE!>x<!> = 1
<!UNREACHABLE_CODE!>x = break<!>
<!UNREACHABLE_CODE!>x =<!> break
}
// TODO: bug, should be fixed in CFA
@@ -50,6 +50,6 @@ fun test(arr: Array<Int>) {
}
while (true) {
<!USELESS_ELVIS!>break<!> ?: <!UNREACHABLE_CODE!>null<!>
<!USELESS_ELVIS!>break<!> <!UNREACHABLE_CODE!>?: null<!>
}
}
@@ -2,7 +2,7 @@
fun foo(x: String): String {
try {
<!UNREACHABLE_CODE!>throw RuntimeException()<!>
<!UNREACHABLE_CODE!>throw<!> RuntimeException()
} finally {
try {
} catch (e: Exception) {
@@ -2,7 +2,7 @@
fun foo() {
try {
<!UNREACHABLE_CODE!>throw RuntimeException()<!>
<!UNREACHABLE_CODE!>throw<!> RuntimeException()
} catch (e: Exception) {
<!UNREACHABLE_CODE!>return<!> // <- Wrong UNREACHABLE_CODE
} finally {
@@ -2,7 +2,7 @@
fun foo(<!UNUSED_PARAMETER!>x<!>: String): String {
try {
<!UNREACHABLE_CODE!>throw RuntimeException()<!> //should be marked as unreachable, but is not
<!UNREACHABLE_CODE!>throw<!> RuntimeException() //should be marked as unreachable, but is not
} finally {
throw NullPointerException()
}
@@ -1,3 +1,4 @@
// !DIAGNOSTICS: -UNREACHABLE_CODE
package kt770_351_735
@@ -120,9 +121,9 @@ fun testStatementInExpressionContext() {
val <!UNUSED_VARIABLE!>a1<!>: Unit = <!ASSIGNMENT_IN_EXPRESSION_CONTEXT!>z = <!UNUSED_VALUE!>334<!><!>
val <!UNUSED_VARIABLE!>f<!> = <!EXPRESSION_EXPECTED!>for (i in 1..10) {}<!>
if (true) return <!ASSIGNMENT_IN_EXPRESSION_CONTEXT!>z = <!UNUSED_VALUE!>34<!><!>
<!UNREACHABLE_CODE!>return <!EXPRESSION_EXPECTED!>while (true) {}<!><!>
return <!EXPRESSION_EXPECTED!>while (true) {}<!>
}
fun testStatementInExpressionContext2() {
<!UNREACHABLE_CODE!>val <!UNUSED_VARIABLE!>a2<!>: Unit = <!EXPRESSION_EXPECTED!>while(true) {}<!><!>
val <!UNUSED_VARIABLE!>a2<!>: Unit = <!EXPRESSION_EXPECTED!>while(true) {}<!>
}
@@ -5,7 +5,7 @@ fun foo() : Int {
doSmth()
}
catch (e: Exception) {
<!UNREACHABLE_CODE!>return <!TYPE_MISMATCH!>""<!><!>
<!UNREACHABLE_CODE!>return<!> <!TYPE_MISMATCH!>""<!>
}
finally {
return <!TYPE_MISMATCH!>""<!>
@@ -1,3 +1,4 @@
// !DIAGNOSTICS: -UNREACHABLE_CODE
//KT-2445 Calling method with function with generic parameter causes compile-time exception
package a
@@ -7,4 +8,4 @@ fun main(args: Array<String>) {
}
}
fun test<R>(callback: (R) -> Unit):Unit = <!UNREACHABLE_CODE!>callback<!>(null!!)
fun test<R>(callback: (R) -> Unit):Unit = callback(null!!)
@@ -1,3 +1,4 @@
// !DIAGNOSTICS: -UNREACHABLE_CODE
//KT-2838 Type inference failed on passing null as a nullable argument
package a
@@ -9,9 +10,9 @@ fun test(a: Int) {
bar(a, <!NULL_FOR_NONNULL_TYPE!>null<!>)
}
fun test1(a: Int) {
<!UNREACHABLE_CODE!>foo<!>(a, throw Exception())
foo(a, throw Exception())
}
fun test2(a: Int) {
<!UNREACHABLE_CODE!>bar<!>(a, throw Exception())
bar(a, throw Exception())
}
@@ -17,7 +17,7 @@ fun lock<T>(lock : Lock, body : () -> T) : T {
//more tests
fun t1() : Int {
try {
<!UNREACHABLE_CODE!>return 1<!>
<!UNREACHABLE_CODE!>return<!> 1
}
finally {
return 2
@@ -469,11 +469,6 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest("compiler/testData/diagnostics/tests/UnitValue.kt");
}
@TestMetadata("UnreachableCode.kt")
public void testUnreachableCode() throws Exception {
doTest("compiler/testData/diagnostics/tests/UnreachableCode.kt");
}
@TestMetadata("Unresolved.kt")
public void testUnresolved() throws Exception {
doTest("compiler/testData/diagnostics/tests/Unresolved.kt");
@@ -1239,7 +1234,7 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
}
@TestMetadata("compiler/testData/diagnostics/tests/controlFlowAnalysis")
@InnerTestClasses({ControlFlowAnalysis.DefiniteReturn.class})
@InnerTestClasses({ControlFlowAnalysis.DeadCode.class, ControlFlowAnalysis.DefiniteReturn.class})
public static class ControlFlowAnalysis extends AbstractJetDiagnosticsTest {
public void testAllFilesPresentInControlFlowAnalysis() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/diagnostics/tests/controlFlowAnalysis"), Pattern.compile("^(.+)\\.kt$"), true);
@@ -1260,11 +1255,6 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/checkPropertyAccessor.kt");
}
@TestMetadata("DeadCode.kt")
public void testDeadCode() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/DeadCode.kt");
}
@TestMetadata("definiteReturnInWhen.kt")
public void testDefiniteReturnInWhen() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/definiteReturnInWhen.kt");
@@ -1340,21 +1330,6 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/kt2369.kt");
}
@TestMetadata("kt2585_1.kt")
public void testKt2585_1() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/kt2585_1.kt");
}
@TestMetadata("kt2585_2.kt")
public void testKt2585_2() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/kt2585_2.kt");
}
@TestMetadata("kt2585_3.kt")
public void testKt2585_3() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/kt2585_3.kt");
}
@TestMetadata("kt2845.kt")
public void testKt2845() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/kt2845.kt");
@@ -1455,16 +1430,119 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/UninitializedOrReassignedVariables.kt");
}
@TestMetadata("unreachableCode.kt")
public void testUnreachableCode() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/unreachableCode.kt");
}
@TestMetadata("varInitializationInIfInCycle.kt")
public void testVarInitializationInIfInCycle() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/varInitializationInIfInCycle.kt");
}
@TestMetadata("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode")
public static class DeadCode extends AbstractJetDiagnosticsTest {
public void testAllFilesPresentInDeadCode() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("commasAndWhitespaces.kt")
public void testCommasAndWhitespaces() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/commasAndWhitespaces.kt");
}
@TestMetadata("deadCallInInvokeCall.kt")
public void testDeadCallInInvokeCall() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCallInInvokeCall.kt");
}
@TestMetadata("deadCallInReceiver.kt")
public void testDeadCallInReceiver() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCallInReceiver.kt");
}
@TestMetadata("deadCodeDifferentExamples.kt")
public void testDeadCodeDifferentExamples() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeDifferentExamples.kt");
}
@TestMetadata("deadCodeFromDifferentSources.kt")
public void testDeadCodeFromDifferentSources() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeFromDifferentSources.kt");
}
@TestMetadata("deadCodeInArrayAccess.kt")
public void testDeadCodeInArrayAccess() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInArrayAccess.kt");
}
@TestMetadata("deadCodeInAssignment.kt")
public void testDeadCodeInAssignment() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInAssignment.kt");
}
@TestMetadata("deadCodeInBinaryExpressions.kt")
public void testDeadCodeInBinaryExpressions() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInBinaryExpressions.kt");
}
@TestMetadata("deadCodeInCalls.kt")
public void testDeadCodeInCalls() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInCalls.kt");
}
@TestMetadata("deadCodeInDeadCode.kt")
public void testDeadCodeInDeadCode() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInDeadCode.kt");
}
@TestMetadata("deadCodeInIf.kt")
public void testDeadCodeInIf() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInIf.kt");
}
@TestMetadata("deadCodeInInnerExpressions.kt")
public void testDeadCodeInInnerExpressions() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInInnerExpressions.kt");
}
@TestMetadata("deadCodeInLocalDeclarations.kt")
public void testDeadCodeInLocalDeclarations() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInLocalDeclarations.kt");
}
@TestMetadata("deadCodeInLoops.kt")
public void testDeadCodeInLoops() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInLoops.kt");
}
@TestMetadata("deadCodeInReturn.kt")
public void testDeadCodeInReturn() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInReturn.kt");
}
@TestMetadata("deadCodeInUnaryExpr.kt")
public void testDeadCodeInUnaryExpr() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInUnaryExpr.kt");
}
@TestMetadata("deadCodeInWhileFromBreak.kt")
public void testDeadCodeInWhileFromBreak() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInWhileFromBreak.kt");
}
@TestMetadata("kt2585_1.kt")
public void testKt2585_1() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/kt2585_1.kt");
}
@TestMetadata("kt2585_2.kt")
public void testKt2585_2() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/kt2585_2.kt");
}
@TestMetadata("kt2585_3.kt")
public void testKt2585_3() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/kt2585_3.kt");
}
}
@TestMetadata("compiler/testData/diagnostics/tests/controlFlowAnalysis/definiteReturn")
public static class DefiniteReturn extends AbstractJetDiagnosticsTest {
public void testAllFilesPresentInDefiniteReturn() throws Exception {
@@ -1491,6 +1569,7 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
public static Test innerSuite() {
TestSuite suite = new TestSuite("ControlFlowAnalysis");
suite.addTestSuite(ControlFlowAnalysis.class);
suite.addTestSuite(DeadCode.class);
suite.addTestSuite(DefiniteReturn.class);
return suite;
}
+2 -2
View File
@@ -48,7 +48,7 @@ fun blockAndAndMismatch1() : Int {
return <error>true && false</error>
}
fun blockAndAndMismatch2() : Int {
<warning>(return <error>true</error>) && (return <error>false</error>)</warning>
(return <error>true</error>) <warning>&& (return <error>false</error>)</warning>
}
fun blockAndAndMismatch3() : Int {
@@ -58,7 +58,7 @@ fun blockAndAndMismatch4() : Int {
return <error>true || false</error>
}
fun blockAndAndMismatch5() : Int {
<warning>(return <error>true</error>) || (return <error>false</error>)</warning>
(return <error>true</error>) <warning>|| (return <error>false</error>)</warning>
}
fun blockReturnValueTypeMatch1() : Int {
return if (1 > 2) <error>1.0</error> else <error>2.0</error>
+2 -2
View File
@@ -126,12 +126,12 @@ fun t8() : Int {
}
fun blockAndAndMismatch() : Boolean {
<warning>(return true) || (return false)</warning>
(return true) <warning>|| (return false)</warning>
<warning>return true</warning>
}
fun tf() : Int {
try {<warning>return 1</warning>} finally{return 1}
try {<warning>return</warning> 1} finally{return 1}
<warning>return 1</warning>
}