KT-22274 report warning on labels that can't be referenced

Labels are meaningful only if they can be referenced by 'break',
'continue', or 'return' expressions.
This commit is contained in:
Dmitry Petrov
2018-07-24 15:58:10 +03:00
parent 6fb913a463
commit df6d4f358a
28 changed files with 127 additions and 42 deletions
@@ -298,6 +298,17 @@ class ControlFlowProcessor(
generateInstructions(baseExpression)
copyValue(baseExpression, expression)
}
val labelNameExpression = expression.getTargetLabel()
if (labelNameExpression != null) {
val deparenthesizedBaseExpression = KtPsiUtil.deparenthesize(expression)
if (deparenthesizedBaseExpression !is KtLambdaExpression &&
deparenthesizedBaseExpression !is KtLoopExpression &&
deparenthesizedBaseExpression !is KtNamedFunction
) {
trace.report(Errors.REDUNDANT_LABEL_WARNING.on(labelNameExpression))
}
}
}
override fun visitBinaryExpression(expression: KtBinaryExpression) {
@@ -822,6 +822,8 @@ public interface Errors {
DiagnosticFactory0<KtExpressionWithLabel> NOT_A_FUNCTION_LABEL = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtExpressionWithLabel> NOT_A_FUNCTION_LABEL_WARNING = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<KtElement> REDUNDANT_LABEL_WARNING = DiagnosticFactory0.create(WARNING);
// Control flow / Data flow
DiagnosticFactory1<KtElement, List<TextRange>> UNREACHABLE_CODE = DiagnosticFactory1.create(
@@ -602,6 +602,8 @@ public class DefaultErrorMessages {
MAP.put(NOT_A_FUNCTION_LABEL, "Target label does not denote a function");
MAP.put(NOT_A_FUNCTION_LABEL_WARNING, "Target label does not denote a function");
MAP.put(REDUNDANT_LABEL_WARNING, "Label is redundant, because it can not be referenced in either ''break'', ''continue'', or ''return'' expression");
MAP.put(ANONYMOUS_INITIALIZER_IN_INTERFACE, "Anonymous initializers are not allowed in interfaces");
MAP.put(NULLABLE_SUPERTYPE, "A supertype cannot be nullable");
MAP.put(DYNAMIC_SUPERTYPE, "A supertype cannot be dynamic");
+2 -2
View File
@@ -2,7 +2,7 @@ class C {
fun f (<!UNUSED_PARAMETER!>a<!> : Boolean, <!UNUSED_PARAMETER!>b<!> : Boolean) {
b@ while (true)
a@ {
<!REDUNDANT_LABEL_WARNING!>a@<!> {
<!NOT_A_LOOP_LABEL!>break@f<!>
break
<!UNREACHABLE_CODE!>break@b<!>
@@ -12,7 +12,7 @@ class C {
<!BREAK_OR_CONTINUE_OUTSIDE_A_LOOP!>continue<!>
b@ while (true)
a@ {
<!REDUNDANT_LABEL_WARNING!>a@<!> {
<!NOT_A_LOOP_LABEL!>continue@f<!>
continue
<!UNREACHABLE_CODE!>continue@b<!>
+1 -1
View File
@@ -17,7 +17,7 @@ fun test() : Unit {
val <!UNUSED_VARIABLE!>s<!> = "" as Any
("" as String?)?.length
(data@("" as String?))?.length
(<!REDUNDANT_LABEL_WARNING!>data@<!>("" as String?))?.length
(<!WRONG_ANNOTATION_TARGET!>@MustBeDocumented()<!>( "" as String?))?.length
Unit
}
@@ -59,7 +59,7 @@ fun main1() {
{1}();
(fun (x : Int) = x)(1)
1.(fun Int.(x : Int) = x)(1);
l@{1}()
<!OI;REDUNDANT_LABEL_WARNING!>l@<!>{1}()
1.((fun Int.() = 1))()
1.(f())()
1.if(true){f()}else{f()}()
+5 -5
View File
@@ -78,17 +78,17 @@ class Test() {
(f@ <!VARIABLE_EXPECTED!>getInt()<!>) += 343
<!VARIABLE_EXPECTED!>1<!>++
(r@ <!VARIABLE_EXPECTED!>1<!>)++
(<!REDUNDANT_LABEL_WARNING!>r@<!> <!VARIABLE_EXPECTED!>1<!>)++
<!VARIABLE_EXPECTED!>getInt()<!>++
(m@ <!VARIABLE_EXPECTED!>getInt()<!>)++
(<!REDUNDANT_LABEL_WARNING!>m@<!> <!VARIABLE_EXPECTED!>getInt()<!>)++
this<!UNRESOLVED_REFERENCE!>++<!>
var s : String = "r"
s += "ss"
s += this
s += (a@ 2)
s += (<!REDUNDANT_LABEL_WARNING!>a@<!> 2)
}
fun testIncompleteSyntax() {
@@ -106,7 +106,7 @@ class Test() {
<!VAL_REASSIGNMENT!>b<!> += 34
a++
(l@ a)++
(<!REDUNDANT_LABEL_WARNING!>l@<!> a)++
<!UNUSED_CHANGED_VALUE!>(a)++<!>
}
@@ -126,7 +126,7 @@ class Test() {
ab.getArray()[54] = 23
ab.getArray()[54]++
(f@ a)[3] = 4
(<!REDUNDANT_LABEL_WARNING!>f@<!> a)[3] = 4
this<!NO_SET_METHOD!><!UNRESOLVED_REFERENCE!>[<!>54<!UNRESOLVED_REFERENCE!>]<!><!> = 34
}
@@ -22,6 +22,6 @@ fun main(args : Array<String>) {
<!NO_COMPANION_OBJECT!>System<!> is Int
<!INVISIBLE_MEMBER!>System<!>()
(<!NO_COMPANION_OBJECT!>System<!>)
foo@ <!NO_COMPANION_OBJECT!>System<!>
<!OI;REDUNDANT_LABEL_WARNING!>foo@<!> <!NO_COMPANION_OBJECT!>System<!>
null in <!NO_COMPANION_OBJECT!>System<!>
}
+2 -2
View File
@@ -5,8 +5,8 @@
annotation class yield
fun bar(p: Int) {
<!YIELD_IS_RESERVED!>yield<!>@ p
`yield`@ p
<!REDUNDANT_LABEL_WARNING!><!YIELD_IS_RESERVED!>yield<!>@<!> p
<!REDUNDANT_LABEL_WARNING!>`yield`@<!> p
@<!YIELD_IS_RESERVED!>yield<!>() p
@`yield`() p
+1 -1
View File
@@ -21,7 +21,7 @@ fun <!UNDERSCORE_IS_RESERVED!>__<!>(<!UNDERSCORE_IS_RESERVED!>___<!>: Int, y: <!
fun localFun(<!UNDERSCORE_IS_RESERVED!>_<!>: String) = 1
<!UNDERSCORE_IS_RESERVED!>__<!>@ return if (y != null) <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>__<!>(<!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>____<!>, y) else <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>__<!>(`_`, <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>______<!>)
<!REDUNDANT_LABEL_WARNING!><!UNDERSCORE_IS_RESERVED!>__<!>@<!> return if (y != null) <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>__<!>(<!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>____<!>, y) else <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>__<!>(`_`, <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>______<!>)
}
@@ -12,7 +12,7 @@ fun test2() {
<!UNREACHABLE_CODE!>bar(<!>11, todo()/*comment1*/, <!UNREACHABLE_CODE!>""/*comment2*/)<!>
}
fun test3() {
<!UNREACHABLE_CODE!>bar(<!>11, l@(todo()/*comment*/), <!UNREACHABLE_CODE!>"")<!>
<!UNREACHABLE_CODE!>bar(<!>11, <!REDUNDANT_LABEL_WARNING!>l@<!>(todo()/*comment*/), <!UNREACHABLE_CODE!>"")<!>
}
fun todo(): Nothing = throw Exception()
@@ -19,7 +19,7 @@ class TestObjectLiteral {
val y = <!UNINITIALIZED_VARIABLE!>obj<!>
}
}
val obj1: A = l@ ( object: A(<!UNINITIALIZED_VARIABLE!>obj1<!>) {
val obj1: A = <!REDUNDANT_LABEL_WARNING!>l@<!> ( object: A(<!UNINITIALIZED_VARIABLE!>obj1<!>) {
init {
val x = <!UNINITIALIZED_VARIABLE!>obj1<!>
}
@@ -51,12 +51,12 @@ fun testLoopLabelInReturn(xs: List<Int>) {
}
fun testValLabelInReturn() {
L@ val fn = { <!NOT_A_FUNCTION_LABEL!>return@L<!> }
<!REDUNDANT_LABEL_WARNING!>L@<!> val fn = { <!NOT_A_FUNCTION_LABEL!>return@L<!> }
fn()
}
fun testHighOrderFunctionCallLabelInReturn() {
L@ run {
<!REDUNDANT_LABEL_WARNING!>L@<!> run {
<!NOT_A_FUNCTION_LABEL!>return@L<!>
}
}
@@ -51,12 +51,12 @@ fun testLoopLabelInReturn(xs: List<Int>) {
}
fun testValLabelInReturn() {
L@ val fn = { <!NOT_A_FUNCTION_LABEL_WARNING!>return@L<!> }
<!REDUNDANT_LABEL_WARNING!>L@<!> val fn = { <!NOT_A_FUNCTION_LABEL_WARNING!>return@L<!> }
fn()
}
fun testHighOrderFunctionCallLabelInReturn() {
L@ run {
<!REDUNDANT_LABEL_WARNING!>L@<!> run {
<!NOT_A_FUNCTION_LABEL_WARNING!>return@L<!>
}
}
@@ -0,0 +1,39 @@
@Target(AnnotationTarget.EXPRESSION)
@Retention(AnnotationRetention.SOURCE)
annotation class Ann
fun testLambdaLabel() = l@ { 42 }
fun testAnonymousFunctionLabel() = l@ fun() {}
fun testAnnotatedLambdaLabel() = lambda@ @Ann {}
fun testParenthesizedLambdaLabel() = lambda@ ( {} )
fun testLabelBoundToInvokeOperatorExpression() = <!REDUNDANT_LABEL_WARNING!>l@<!> { 42 }()
fun testLabelBoundToLambda() = (l@ { 42 })()
fun testWhileLoopLabel() {
L@ while (true) {}
}
fun testDoWhileLoopLabel() {
L@ do {} while (true)
}
fun testForLoopLabel(xs: List<Any>) {
L@ for (x in xs) {}
}
fun testValLabel() {
<!REDUNDANT_LABEL_WARNING!>L@<!> val fn = {}
fn()
}
fun testHighOrderFunctionCallLabel() {
<!REDUNDANT_LABEL_WARNING!>L@<!> run {}
}
fun testAnonymousObjectLabel() =
<!REDUNDANT_LABEL_WARNING!>L@<!> object {}
@@ -0,0 +1,21 @@
package
public fun testAnnotatedLambdaLabel(): () -> kotlin.Unit
public fun testAnonymousFunctionLabel(): () -> kotlin.Unit
public fun testAnonymousObjectLabel(): kotlin.Any
public fun testDoWhileLoopLabel(): kotlin.Unit
public fun testForLoopLabel(/*0*/ xs: kotlin.collections.List<kotlin.Any>): kotlin.Unit
public fun testHighOrderFunctionCallLabel(): kotlin.Unit
public fun testLabelBoundToInvokeOperatorExpression(): kotlin.Int
public fun testLabelBoundToLambda(): kotlin.Int
public fun testLambdaLabel(): () -> kotlin.Int
public fun testParenthesizedLambdaLabel(): () -> kotlin.Unit
public fun testValLabel(): kotlin.Unit
public fun testWhileLoopLabel(): kotlin.Unit
@kotlin.annotation.Target(allowedTargets = {AnnotationTarget.EXPRESSION}) @kotlin.annotation.Retention(value = AnnotationRetention.SOURCE) public final annotation class Ann : kotlin.Annotation {
public constructor Ann()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -1,7 +1,7 @@
import kotlin.reflect.KProperty
class A3 {
val a: String by l@ MyProperty()
val a: String by <!REDUNDANT_LABEL_WARNING!>l@<!> MyProperty()
class MyProperty<T> {}
@@ -1,3 +1,3 @@
fun test() {
(d@ <!DECLARATION_IN_ILLEGAL_CONTEXT!>val <!UNUSED_VARIABLE!>bar<!> = 2<!>)
(<!REDUNDANT_LABEL_WARNING!>d@<!> <!DECLARATION_IN_ILLEGAL_CONTEXT!>val <!UNUSED_VARIABLE!>bar<!> = 2<!>)
}
@@ -5,19 +5,19 @@ package m
fun test(i: Int?) {
if (i != null) {
foo(l1@ <!DEBUG_INFO_SMARTCAST!>i<!>)
foo(<!OI;REDUNDANT_LABEL_WARNING!>l1@<!> <!DEBUG_INFO_SMARTCAST!>i<!>)
foo((<!DEBUG_INFO_SMARTCAST!>i<!>))
foo(l2@ (<!DEBUG_INFO_SMARTCAST!>i<!>))
foo((l3@ <!DEBUG_INFO_SMARTCAST!>i<!>))
foo(<!OI;REDUNDANT_LABEL_WARNING!>l2@<!> (<!DEBUG_INFO_SMARTCAST!>i<!>))
foo((<!OI;REDUNDANT_LABEL_WARNING!>l3@<!> <!DEBUG_INFO_SMARTCAST!>i<!>))
}
val a: Int = l4@ <!TYPE_MISMATCH!>""<!>
val a: Int = <!OI;REDUNDANT_LABEL_WARNING!>l4@<!> <!TYPE_MISMATCH!>""<!>
val b: Int = (<!TYPE_MISMATCH!>""<!>)
val c: Int = checkSubtype<Int>(<!TYPE_MISMATCH!>""<!>)
val d: Int = <!NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, TYPE_MISMATCH!>checkSubtype<Long>(<!TYPE_MISMATCH!>""<!>)<!>
foo(l4@ <!TYPE_MISMATCH!>""<!>)
foo(<!OI;REDUNDANT_LABEL_WARNING!>l4@<!> <!TYPE_MISMATCH!>""<!>)
foo((<!TYPE_MISMATCH!>""<!>))
foo(checkSubtype<Int>(<!NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, TYPE_MISMATCH!>""<!>))
foo(<!NI;TYPE_MISMATCH, TYPE_MISMATCH!>checkSubtype<Long>(<!NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, TYPE_MISMATCH!>""<!>)<!>)
@@ -1,3 +1,3 @@
fun f(s : String?) : Boolean {
return foo@(s?.equals("a"))!!
return <!REDUNDANT_LABEL_WARNING!>foo@<!>(s?.equals("a"))!!
}
@@ -11,7 +11,7 @@ fun <V> id(value: V) = value
val par1 = (foo()) as String
val par2 = ((foo())) as String
val par3 = (dd@ (foo())) as String
val par3 = (<!REDUNDANT_LABEL_WARNING!>dd@<!> (foo())) as String
val par4 = ( @bar() (foo())) as String
+4 -4
View File
@@ -24,17 +24,17 @@ inline fun foo(bar1: (String.() -> Int) -> Int, bar2: (()->Int) -> Int) {
}
inline fun foo2(bar1: (String.() -> Int) -> Int) {
l1@ <!USAGE_IS_NOT_INLINABLE!>bar1<!>
<!REDUNDANT_LABEL_WARNING!>l1@<!> <!USAGE_IS_NOT_INLINABLE!>bar1<!>
l2@ bar1 {
<!REDUNDANT_LABEL_WARNING!>l2@<!> bar1 {
11
}
(l3@ bar1) {
(<!REDUNDANT_LABEL_WARNING!>l3@<!> bar1) {
11
}
(l5@ (l4@ bar1)) {
(<!REDUNDANT_LABEL_WARNING!>l5@<!> (<!REDUNDANT_LABEL_WARNING!>l4@<!> bar1)) {
11
}
}
@@ -22,6 +22,6 @@ inline fun inlineFunWithInvoke2(s: (p: Int) -> Unit) {
}
inline fun propagation(s: (p: Int) -> Unit) {
inlineFunWithInvoke((label@ s))
inlineFunWithInvoke((label2@ label@ s))
inlineFunWithInvoke((<!REDUNDANT_LABEL_WARNING!>label@<!> s))
inlineFunWithInvoke((<!REDUNDANT_LABEL_WARNING!>label2@<!> <!REDUNDANT_LABEL_WARNING!>label@<!> s))
}
@@ -9,8 +9,8 @@ fun main(args : Array<String>) {
val <!UNUSED_VARIABLE!>h<!> : String = <!TYPE_MISMATCH!>v--<!>;
val <!UNUSED_VARIABLE!>h1<!> : String = <!TYPE_MISMATCH!>--v<!>;
val <!UNUSED_VARIABLE!>i<!> : String = <!TYPE_MISMATCH!>!true<!>;
val <!UNUSED_VARIABLE!>j<!> : String = foo@ <!CONSTANT_EXPECTED_TYPE_MISMATCH!>true<!>;
val <!UNUSED_VARIABLE!>k<!> : String = foo@ bar@ <!CONSTANT_EXPECTED_TYPE_MISMATCH!>true<!>;
val <!UNUSED_VARIABLE!>j<!> : String = <!OI;REDUNDANT_LABEL_WARNING!>foo@<!> <!CONSTANT_EXPECTED_TYPE_MISMATCH!>true<!>;
val <!UNUSED_VARIABLE!>k<!> : String = <!OI;REDUNDANT_LABEL_WARNING!>foo@<!> <!OI;REDUNDANT_LABEL_WARNING!>bar@<!> <!CONSTANT_EXPECTED_TYPE_MISMATCH!>true<!>;
val <!UNUSED_VARIABLE!>l<!> : String = <!TYPE_MISMATCH!>-1<!>;
val <!UNUSED_VARIABLE!>m<!> : String = <!TYPE_MISMATCH!>+1<!>;
}
@@ -14,7 +14,7 @@ fun test(bar: Bar, a: A) {
// no elements with error types
fooInt((bar()))
fooInt(if (true) bar() else bar())
fooInt(label@ bar())
fooInt(<!REDUNDANT_LABEL_WARNING!>label@<!> bar())
fooInt(a.bar())
fooInt(((label@ if (true) (a.bar()) else bar())))
fooInt(((<!REDUNDANT_LABEL_WARNING!>label@<!> if (true) (a.bar()) else bar())))
}
@@ -1,23 +1,23 @@
fun testEquals(x: Int) {
if (<!SENSELESS_COMPARISON!>x == null<!>) {}
if (<!SENSELESS_COMPARISON!>x == (null)<!>) {}
if (<!SENSELESS_COMPARISON!>x == foo@ null<!>) {}
if (<!SENSELESS_COMPARISON!>x == <!REDUNDANT_LABEL_WARNING!>foo@<!> null<!>) {}
}
fun testEqualsFlipped(x: Int) {
if (<!SENSELESS_COMPARISON!>null == x<!>) {}
if (<!SENSELESS_COMPARISON!>(null) == x<!>) {}
if (<!SENSELESS_COMPARISON!>foo@ null == x<!>) {}
if (<!SENSELESS_COMPARISON!><!REDUNDANT_LABEL_WARNING!>foo@<!> null == x<!>) {}
}
fun testNotEquals(x: Int) {
if (<!SENSELESS_COMPARISON!>x != null<!>) {}
if (<!SENSELESS_COMPARISON!>x != (null)<!>) {}
if (<!SENSELESS_COMPARISON!>x != foo@ null<!>) {}
if (<!SENSELESS_COMPARISON!>x != <!REDUNDANT_LABEL_WARNING!>foo@<!> null<!>) {}
}
fun testNotEqualsFlipped(x: Int) {
if (<!SENSELESS_COMPARISON!>null != x<!>) {}
if (<!SENSELESS_COMPARISON!>(null) != x<!>) {}
if (<!SENSELESS_COMPARISON!>foo@ null != x<!>) {}
if (<!SENSELESS_COMPARISON!><!REDUNDANT_LABEL_WARNING!>foo@<!> null != x<!>) {}
}
@@ -4140,6 +4140,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
runTest("compiler/testData/diagnostics/tests/controlStructures/notAFunctionLabel_before.kt");
}
@TestMetadata("redundantLabel.kt")
public void testRedundantLabel() throws Exception {
runTest("compiler/testData/diagnostics/tests/controlStructures/redundantLabel.kt");
}
@TestMetadata("specialConstructsAndPlatformTypes.kt")
public void testSpecialConstructsAndPlatformTypes() throws Exception {
runTest("compiler/testData/diagnostics/tests/controlStructures/specialConstructsAndPlatformTypes.kt");
@@ -4140,6 +4140,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/controlStructures/notAFunctionLabel_before.kt");
}
@TestMetadata("redundantLabel.kt")
public void testRedundantLabel() throws Exception {
runTest("compiler/testData/diagnostics/tests/controlStructures/redundantLabel.kt");
}
@TestMetadata("specialConstructsAndPlatformTypes.kt")
public void testSpecialConstructsAndPlatformTypes() throws Exception {
runTest("compiler/testData/diagnostics/tests/controlStructures/specialConstructsAndPlatformTypes.kt");