Prohibit local objects and enum classes
#KT-5402 Fixed
#KT-4838 Fixed
Resolve type of object inside local object as special, not supertype('Any').
Changed visibility of constructor of anonymous object to 'internal' to be able to resolve the following:
fun box(): String {
var foo = object {
val bar = object {
val baz = "ok"
}
}
return foo.bar.baz
}
The containing declaration of property initializers is constructor, so 'baz' was invisible inside private constructor.
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
// KT-2948 Assertion fails on a local enum
|
||||
|
||||
fun foo(): String {
|
||||
enum class E {
|
||||
OK
|
||||
}
|
||||
|
||||
return E.OK.toString()
|
||||
}
|
||||
|
||||
fun box(): String = foo()
|
||||
@@ -1,8 +0,0 @@
|
||||
fun box(): String {
|
||||
enum class K {
|
||||
O
|
||||
K
|
||||
}
|
||||
|
||||
return K.O.toString() + K.K.toString()
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
fun box(): String {
|
||||
enum class K {
|
||||
O
|
||||
K
|
||||
}
|
||||
|
||||
return K.O.toString() + K.K.toString()
|
||||
}
|
||||
@@ -6,11 +6,11 @@ class A {
|
||||
open fun s() : String = "O"
|
||||
}
|
||||
|
||||
object O: B() {
|
||||
val o = object : B() {
|
||||
override fun s(): String = "K"
|
||||
}
|
||||
|
||||
a = B().s() + O.s()
|
||||
a = B().s() + o.s()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,11 +4,11 @@ class A(
|
||||
open fun s() : String = "O"
|
||||
}
|
||||
|
||||
object O: B() {
|
||||
val o = object : B() {
|
||||
override fun s(): String = "K"
|
||||
}
|
||||
|
||||
B().s() + O.s()
|
||||
B().s() + o.s()
|
||||
}()
|
||||
)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
fun box(): String {
|
||||
object K {
|
||||
val k = object {
|
||||
val ok = "OK"
|
||||
}
|
||||
|
||||
return K.ok
|
||||
return k.ok
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
fun box(): String {
|
||||
var boo = "OK"
|
||||
var foo = object {
|
||||
val bar = object {
|
||||
val baz = boo
|
||||
}
|
||||
}
|
||||
|
||||
return foo.bar.baz
|
||||
}
|
||||
+8
-8
@@ -1,11 +1,11 @@
|
||||
fun box(): String {
|
||||
trait Named {
|
||||
fun name(): String
|
||||
}
|
||||
|
||||
enum class E : Named {
|
||||
OK
|
||||
}
|
||||
trait Named {
|
||||
fun name(): String
|
||||
}
|
||||
|
||||
enum class E : Named {
|
||||
OK
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return E.OK.(Named::name)()
|
||||
}
|
||||
|
||||
@@ -193,7 +193,7 @@ fun testFunctionLiterals() {
|
||||
val <!UNUSED_VARIABLE!>endsWithObjectDeclaration<!> : () -> Int = {
|
||||
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>x<!> = 1
|
||||
x = <!UNUSED_VALUE!>333<!>
|
||||
<!EXPECTED_TYPE_MISMATCH!>object A {}<!>
|
||||
<!EXPECTED_TYPE_MISMATCH!><!LOCAL_OBJECT_NOT_ALLOWED!>object A<!> {}<!>
|
||||
}
|
||||
|
||||
val <!UNUSED_VARIABLE!>expectedUnitReturnType1<!> = { () : Unit ->
|
||||
@@ -202,7 +202,7 @@ fun testFunctionLiterals() {
|
||||
|
||||
val <!UNUSED_VARIABLE!>expectedUnitReturnType2<!> = { () : Unit ->
|
||||
fun meow() : Unit {}
|
||||
object A {}
|
||||
<!LOCAL_OBJECT_NOT_ALLOWED!>object A<!> {}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// !DIAGNOSTICS: -UNUSED_VARIABLE
|
||||
|
||||
fun foo() {
|
||||
<!LOCAL_ENUM_NOT_ALLOWED!>enum class A<!> {
|
||||
FOO
|
||||
BAR
|
||||
}
|
||||
val foo = A.FOO
|
||||
val b = object {
|
||||
<!LOCAL_ENUM_NOT_ALLOWED!>enum class B<!> {}
|
||||
}
|
||||
class C {
|
||||
<!LOCAL_ENUM_NOT_ALLOWED!>enum class D<!> {}
|
||||
}
|
||||
val f = {
|
||||
<!LOCAL_ENUM_NOT_ALLOWED!>enum class E<!> {}
|
||||
}
|
||||
}
|
||||
@@ -8,9 +8,9 @@ inline fun unsupported() {
|
||||
}
|
||||
}<!>
|
||||
|
||||
<!NOT_YET_SUPPORTED_IN_INLINE!>object B{
|
||||
object BInner {}
|
||||
}<!>
|
||||
<!LOCAL_OBJECT_NOT_ALLOWED!>object B<!>{
|
||||
<!LOCAL_OBJECT_NOT_ALLOWED!>object BInner<!> {}
|
||||
}
|
||||
|
||||
<!NOT_YET_SUPPORTED_IN_INLINE!>fun local() {
|
||||
fun localInner() {}
|
||||
|
||||
@@ -11,7 +11,7 @@ fun f() {
|
||||
}
|
||||
}
|
||||
|
||||
object MyObject {
|
||||
<!LOCAL_OBJECT_NOT_ALLOWED!>object MyObject<!> {
|
||||
{
|
||||
val <!UNUSED_VARIABLE!>obj<!>: MyObject = MyObject
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ fun test() {
|
||||
}
|
||||
b.foo()
|
||||
|
||||
object B {
|
||||
<!LOCAL_OBJECT_NOT_ALLOWED!>object B<!> {
|
||||
fun foo() {}
|
||||
}
|
||||
B.foo()
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// !DIAGNOSTICS: -UNUSED_VARIABLE
|
||||
|
||||
fun foo() {
|
||||
<!LOCAL_OBJECT_NOT_ALLOWED!>object a<!> {}
|
||||
val b = object {
|
||||
<!LOCAL_OBJECT_NOT_ALLOWED!>object c<!> {}
|
||||
}
|
||||
b.<!NESTED_CLASS_ACCESSED_VIA_INSTANCE_REFERENCE!>c<!>
|
||||
class A {
|
||||
<!LOCAL_OBJECT_NOT_ALLOWED!>object d<!> {}
|
||||
}
|
||||
val f = {
|
||||
<!LOCAL_OBJECT_NOT_ALLOWED!>object e<!> {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
>>> fun foo() {
|
||||
... object Bar {
|
||||
... }
|
||||
... }
|
||||
ERROR: /line4.kts: (2, 1) Named object 'Bar' is a singleton and cannot be local. Try to use anonymous object instead
|
||||
Reference in New Issue
Block a user