Support single-underscore variable names partially

Currently only parameters of lambdas/function expressions
and destructuring entries are allowed

 #KT-3824 In Progress
 #KT-2783 In Progress
This commit is contained in:
Denis Zharkov
2016-10-13 11:55:37 +03:00
parent 82364ad3e5
commit dbca310d8c
19 changed files with 319 additions and 21 deletions
+12 -1
View File
@@ -8,13 +8,24 @@ class <!UNDERSCORE_IS_RESERVED!>_<!><<!UNDERSCORE_IS_RESERVED!>________<!>>
val <!UNDERSCORE_IS_RESERVED!>______<!> = _<Int>()
fun <!UNDERSCORE_IS_RESERVED!>__<!>(<!UNDERSCORE_IS_RESERVED!>___<!>: Int, y: _<Int>?): Int {
val (_, <!UNUSED_VARIABLE!>z<!>) = Pair(___ - 1, 42)
val (x, <!UNDERSCORE_IS_RESERVED!>__________<!>) = Pair(___ - 1, 42)
val <!UNDERSCORE_IS_RESERVED!>____<!> = x
// in backquotes: allowed
val `_` = __________
val q = fun(_: Int, <!UNDERSCORE_IS_RESERVED, UNUSED_PARAMETER!>__<!>: Int) {}
q(1, 2)
val <!UNDERSCORE_IS_RESERVED!>_<!> = 56
<!UNDERSCORE_IS_RESERVED!>__<!>@ return if (y != null) __(____, y) else __(`_`, ______)
}
// one underscore parameters for named function are still prohibited
fun oneUnderscore(<!UNUSED_PARAMETER, UNDERSCORE_IS_RESERVED!>_<!>: Int) {}
fun doIt(f: (Any?) -> Any?) = f(null)
val something = doIt { <!UNDERSCORE_IS_RESERVED!>__<!> -> __ }
val something = doIt { <!UNDERSCORE_IS_RESERVED!>__<!> -> __ }
val something2 = doIt { _ -> 1 }
+2
View File
@@ -2,8 +2,10 @@ package
public val ______: _<kotlin.Int>
public val something: kotlin.Any?
public val something2: kotlin.Any?
public fun __(/*0*/ ___: kotlin.Int, /*1*/ y: _<kotlin.Int>?): kotlin.Int
public fun doIt(/*0*/ f: (kotlin.Any?) -> kotlin.Any?): kotlin.Any?
public fun oneUnderscore(/*0*/ <anonymous parameter 0>: kotlin.Int): kotlin.Unit
@kotlin.Deprecated(message = "") public final data class Pair {
public constructor Pair(/*0*/ x: kotlin.Int, /*1*/ y: kotlin.Int)
@@ -6,9 +6,9 @@ fun useDeclaredVariables() {
}
fun checkersShouldRun() {
for ((@A a, <!UNDERSCORE_IS_RESERVED, UNUSED_VARIABLE!>_<!>)<!SYNTAX!><!>) {
for ((@A a, _)<!SYNTAX!><!>) {
}
}
annotation class A
annotation class A
@@ -5,7 +5,7 @@ fun useDeclaredVariables() {
}
fun checkersShouldRun() {
val (@A a, <!UNDERSCORE_IS_RESERVED, UNUSED_VARIABLE!>_<!>) = <!UNRESOLVED_REFERENCE!>unresolved<!>
val (@A a, _) = <!UNRESOLVED_REFERENCE!>unresolved<!>
}
annotation class A
annotation class A
@@ -5,7 +5,7 @@ fun useDeclaredVariables() {
}
fun checkersShouldRun() {
<!INITIALIZER_REQUIRED_FOR_DESTRUCTURING_DECLARATION!>val (@A a, <!UNDERSCORE_IS_RESERVED, UNUSED_VARIABLE!>_<!>)<!>
<!INITIALIZER_REQUIRED_FOR_DESTRUCTURING_DECLARATION!>val (@A a, _)<!>
}
annotation class A
annotation class A
@@ -0,0 +1,48 @@
class A {
operator fun component1() = 1
operator fun component2() = ""
}
class C {
operator fun iterator(): Iterator<A> = null!!
}
fun test() {
for ((x, _) in C()) {
foo(x, <!UNRESOLVED_REFERENCE!>_<!>)
}
for ((_, y) in C()) {
foo(<!UNRESOLVED_REFERENCE!>_<!>, y)
}
for ((_, _) in C()) {
foo(<!UNRESOLVED_REFERENCE!>_<!>, <!UNRESOLVED_REFERENCE!>_<!>)
}
for ((_ : Int, _ : String) in C()) {
foo(<!UNRESOLVED_REFERENCE!>_<!>, <!UNRESOLVED_REFERENCE!>_<!>)
}
for ((_ : String, _ : Int) in <!COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH, COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH!>C()<!>) {
foo(<!UNRESOLVED_REFERENCE!>_<!>, <!UNRESOLVED_REFERENCE!>_<!>)
}
val (x, _) = A()
val (_, y) = A()
foo(x, y)
foo(x, <!UNRESOLVED_REFERENCE!>_<!>)
foo(<!UNRESOLVED_REFERENCE!>_<!>, y)
val (<!REDECLARATION!>`_`<!>, z) = A()
foo(_, z)
val (_, <!NAME_SHADOWING, REDECLARATION!>`_`<!>) = A()
foo(<!TYPE_MISMATCH!>_<!>, y)
}
fun foo(<!UNUSED_PARAMETER!>x<!>: Int, <!UNUSED_PARAMETER!>y<!>: String) {}
@@ -0,0 +1,21 @@
package
public fun foo(/*0*/ x: kotlin.Int, /*1*/ y: kotlin.String): kotlin.Unit
public fun test(): kotlin.Unit
public final class A {
public constructor A()
public final operator fun component1(): kotlin.Int
public final operator fun component2(): kotlin.String
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
}
public final class C {
public constructor C()
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 final operator fun iterator(): kotlin.collections.Iterator<A>
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,62 @@
// !CHECK_TYPE
// !DIAGNOSTICS: -UNUSED_PARAMETER
data class A(val x: Int, val y: String)
data class B(val u: Double, val w: Short)
fun foo(block: (A) -> Unit) { }
fun bar() {
foo { (_, b) ->
<!UNRESOLVED_REFERENCE!>_<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>hashCode<!>()
b checkType { _<String>() }
}
foo { (a, _) ->
a checkType { _<Int>() }
<!UNRESOLVED_REFERENCE!>_<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>hashCode<!>()
}
foo { (_, _) ->
<!UNRESOLVED_REFERENCE!>_<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>hashCode<!>()
}
foo { (_: Int, b: String) ->
<!UNRESOLVED_REFERENCE!>_<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>hashCode<!>()
b checkType { _<String>() }
}
foo { (a: Int, _: String) ->
a checkType { _<Int>() }
<!UNRESOLVED_REFERENCE!>_<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>hashCode<!>()
}
foo { (_: Int, _: String) ->
<!UNRESOLVED_REFERENCE!>_<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>hashCode<!>()
}
foo { (_, _): A ->
<!UNRESOLVED_REFERENCE!>_<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>hashCode<!>()
}
foo { (`_`, _) ->
_ checkType { _<Int>() }
}
foo { (_, `_`) ->
_ checkType { _<String>() }
}
foo { (<!REDECLARATION!>`_`<!>, <!REDECLARATION!>`_`<!>) ->
_ checkType { _<String>() }
}
foo { (<!COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH!>_: String<!>, b) ->
<!UNRESOLVED_REFERENCE!>_<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>hashCode<!>()
b checkType { _<String>() }
}
foo { <!EXPECTED_PARAMETER_TYPE_MISMATCH!>(_, b): B<!> ->
<!UNRESOLVED_REFERENCE!>_<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>hashCode<!>()
b checkType { _<Short>() }
}
}
@@ -0,0 +1,28 @@
package
public fun bar(): kotlin.Unit
public fun foo(/*0*/ block: (A) -> kotlin.Unit): kotlin.Unit
public final data class A {
public constructor A(/*0*/ x: kotlin.Int, /*1*/ y: kotlin.String)
public final val x: kotlin.Int
public final val y: kotlin.String
public final operator /*synthesized*/ fun component1(): kotlin.Int
public final operator /*synthesized*/ fun component2(): kotlin.String
public final /*synthesized*/ fun copy(/*0*/ x: kotlin.Int = ..., /*1*/ y: kotlin.String = ...): A
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
public final data class B {
public constructor B(/*0*/ u: kotlin.Double, /*1*/ w: kotlin.Short)
public final val u: kotlin.Double
public final val w: kotlin.Short
public final operator /*synthesized*/ fun component1(): kotlin.Double
public final operator /*synthesized*/ fun component2(): kotlin.Short
public final /*synthesized*/ fun copy(/*0*/ u: kotlin.Double = ..., /*1*/ w: kotlin.Short = ...): B
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
@@ -0,0 +1,49 @@
// !CHECK_TYPE
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun foo(block: (Int, String) -> Unit) { }
fun foobar(block: (Double) -> Unit) { }
fun bar() {
foo { _, b ->
<!UNRESOLVED_REFERENCE!>_<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>hashCode<!>()
b checkType { _<String>() }
}
foo { a, _ ->
a checkType { _<Int>() }
<!UNRESOLVED_REFERENCE!>_<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>hashCode<!>()
}
foo { _, _ ->
<!UNRESOLVED_REFERENCE!>_<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>hashCode<!>()
}
foo { _: Int, b: String ->
<!UNRESOLVED_REFERENCE!>_<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>hashCode<!>()
b checkType { _<String>() }
}
foo { a: Int, _: String ->
a checkType { _<Int>() }
<!UNRESOLVED_REFERENCE!>_<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>hashCode<!>()
}
foo { _: Int, _: String ->
<!UNRESOLVED_REFERENCE!>_<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>hashCode<!>()
}
foo { `_`, _ ->
_ checkType { _<Int>() }
}
foo { _, `_` ->
_ checkType { _<String>() }
}
foo { <!REDECLARATION, REDECLARATION!>`_`<!>, <!REDECLARATION, REDECLARATION!>`_`<!> ->
_ checkType { _<String>() }
}
foo(fun(x: Int, _: String) {})
}
@@ -0,0 +1,5 @@
package
public fun bar(): kotlin.Unit
public fun foo(/*0*/ block: (kotlin.Int, kotlin.String) -> kotlin.Unit): kotlin.Unit
public fun foobar(/*0*/ block: (kotlin.Double) -> kotlin.Unit): kotlin.Unit