KT-12152 : constructor consistency: distinguish properties with and w/o backing fields, with default / custom accessors

This commit is contained in:
Mikhail Glukhikh
2015-08-12 14:58:06 +03:00
parent 422ea4c6cb
commit be40cf8138
10 changed files with 148 additions and 18 deletions
@@ -0,0 +1,24 @@
class My {
var x = 1
set(value) {
field = value
}
var y: Int = 1
set(value) {
field = value + if (w == "") 0 else 1
}
var z: Int = 2
set(value) {
field = value + if (w == "") 1 else 0
}
init {
// Writing properties using setters is dangerous
<!DEBUG_INFO_LEAKING_THIS!>y<!> = 4
this.<!DEBUG_INFO_LEAKING_THIS!>z<!> = 5
}
val w = "6"
}
@@ -0,0 +1,12 @@
package
public final class My {
public constructor My()
public final val w: kotlin.String = "6"
public final var x: kotlin.Int
public final var y: kotlin.Int
public final var z: kotlin.Int
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
}
@@ -0,0 +1,17 @@
class My(var x: String) {
var y: String
get() = if (x != "") x else z
set(arg) {
if (arg != "") x = arg
}
val z: String
init {
// Dangerous: setter!
<!DEBUG_INFO_LEAKING_THIS!>y<!> = "x"
// Dangerous: getter!
if (<!DEBUG_INFO_LEAKING_THIS!>y<!> != "") z = this.<!DEBUG_INFO_LEAKING_THIS!>y<!> else z = <!DEBUG_INFO_LEAKING_THIS!>y<!>
}
}
@@ -0,0 +1,11 @@
package
public final class My {
public constructor My(/*0*/ x: kotlin.String)
public final var x: kotlin.String
public final var y: kotlin.String
public final val z: 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
}
@@ -0,0 +1,19 @@
class My {
val x: Int
get() = field + if (z != "") 1 else 0
val y: Int
get() = field - if (z == "") 0 else 1
val w: Int
init {
// Safe, val never has a setter
x = 0
this.y = 0
// Unsafe
w = this.<!DEBUG_INFO_LEAKING_THIS!>x<!> + <!DEBUG_INFO_LEAKING_THIS!>y<!>
}
val z = "1"
}
@@ -0,0 +1,12 @@
package
public final class My {
public constructor My()
public final val w: kotlin.Int
public final val x: kotlin.Int
public final val y: kotlin.Int
public final val z: kotlin.String = "1"
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
}
@@ -9,16 +9,16 @@ class CustomDelegate {
class Kaboom() {
// Here and below we should have errors for simple AND delegated
init {
<!UNINITIALIZED_VARIABLE!>delegated<!>.hashCode()
<!UNINITIALIZED_VARIABLE, DEBUG_INFO_LEAKING_THIS!>delegated<!>.hashCode()
<!UNINITIALIZED_VARIABLE!>simple<!>.hashCode()
withGetter.hashCode()
<!DEBUG_INFO_LEAKING_THIS!>withGetter<!>.hashCode()
}
val other = <!UNINITIALIZED_VARIABLE!>delegated<!>
val other = <!UNINITIALIZED_VARIABLE, DEBUG_INFO_LEAKING_THIS!>delegated<!>
val another = <!UNINITIALIZED_VARIABLE!>simple<!>
val something = withGetter
val something = <!DEBUG_INFO_LEAKING_THIS!>withGetter<!>
val delegated: String by CustomDelegate()
@@ -28,5 +28,5 @@ class Kaboom() {
get() = "abc"
// No error should be here
val after = delegated
val after = <!DEBUG_INFO_LEAKING_THIS!>delegated<!>
}
@@ -5,7 +5,7 @@ class My {
private set
// Error: Variable 'delegate' must be initialized
val another: String = delegate
val another: String = <!DEBUG_INFO_LEAKING_THIS!>delegate<!>
var delegateWithBackingField: String by kotlin.properties.Delegates.notNull()
<!ACCESSOR_FOR_DELEGATED_PROPERTY!>private set(arg) { field = arg }<!>