Added tests on labels

#KT-1703 Fixed
 #KT-361 Fixed
 #KT-3920 Fixed
 #KT-3988 Fixed
 #KT-4247 Fixed
 #KT-4586 Fixed
 #KT-4603 Fixed
 #KT-591 Fixed
This commit is contained in:
Svetlana Isakova
2014-05-14 13:57:14 +04:00
parent a02af7344e
commit 69e5444ddf
10 changed files with 190 additions and 1 deletions
@@ -0,0 +1,15 @@
//KT-1703 Reference to label is unresolved
fun test() {
val ints = Array<Int?>(2, { null })
ints.forEach @lit {
if (it == null) <!RETURN_NOT_ALLOWED_EXPLICIT_RETURN_TYPE_REQUIRED!>return @lit<!>
use(<!DEBUG_INFO_AUTOCAST!>it<!> + 5)
}
}
fun <T> Array<out T>.forEach(operation: (T) -> Unit) {
for (element in this) operation(element)
}
fun use(a: Any?) = a
@@ -0,0 +1,12 @@
fun nonlocals(b : Boolean) {
@a{(): Int ->
fun foo() {
if (b) {
<!RETURN_NOT_ALLOWED!>return@a 1<!> // The label must be resolved, but an error should be reported for a non-local return
}
}
return@a 5
}
}
@@ -0,0 +1,11 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE
//KT-3920 Labeling information is lost when passing through some expressions
fun test() {
run @f{ (): Int ->
val x = if (1 > 2) return@f 1 else 2
2
}
}
fun <T> run(f: () -> T): T = f()
@@ -0,0 +1,21 @@
//KT-3988 This@label for outer function not resolved
class Comment() {
var article = ""
}
class Comment2() {
var article2 = ""
}
fun new(body: Comment.() -> Unit) = body
fun new2(body: Comment2.() -> Unit) = body
fun main(args: Array<String>) {
new {
new2 {
this@new //UNRESOLVED REFERENCE
}
}
}
@@ -0,0 +1,11 @@
//KT-4247 LABEL_NAME_CLASH
fun foo(bar1: (String.() -> Int) -> Int) {
bar1 {
this.length
}
bar1 {
this@bar1.length
}
}
@@ -0,0 +1,17 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE
//KT-4586 this@ does not work for builders
fun string(init: StringBuilder.() -> Unit): String{
val answer = StringBuilder()
answer.init()
return answer.toString()
}
val str = string @l{
append("hello, ")
val sub = string {
append("world!")
this@l.append(this)
}
}
@@ -0,0 +1,12 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE
//KT-4603 Labeling information is lost when passing through local classes or objects
fun foo() {
val s = @l{ Int.() ->
class Local(val y: Int = this@l) {
fun bar() {
val x: Int = this@l //unresolved
}
}
}
}
@@ -0,0 +1,11 @@
//KT-591 Unresolved label in valid code
fun test() {
val <!UNUSED_VARIABLE!>a<!> = @a{(Int?).() ->
if (this != null) {
val <!UNUSED_VARIABLE!>b<!> = {String.() ->
<!DEBUG_INFO_AUTOCAST!>this@a<!>.times(5) // @a Unresolved
}
}
}
}
@@ -0,0 +1,25 @@
trait A {
fun foo()
}
trait B {
fun bar()
}
fun B.b() {
object : A {
override fun foo() {
this@b.bar()
}
}
}
fun test() {
@b { B.() ->
object : A {
override fun foo() {
this@b.bar()
}
}
}
}