KT-1531 Frontend should prohibit secondary constructors

# KT-1531 Fixed
This commit is contained in:
Svetlana Isakova
2012-03-27 17:48:04 +04:00
parent 406933557b
commit a91a53f7f1
22 changed files with 53 additions and 52 deletions
@@ -302,6 +302,7 @@ public interface Errors {
DiagnosticFactory<JetTypeReference> SUPERTYPE_NOT_A_CLASS_OR_TRAIT = DiagnosticFactory.create(ERROR, "Only classes and traits may serve as supertypes");
DiagnosticFactory<PsiElement> SUPERTYPE_INITIALIZED_IN_TRAIT = DiagnosticFactory.create(ERROR, "Traits cannot initialize supertypes");
DiagnosticFactory<PsiElement> CONSTRUCTOR_IN_TRAIT = DiagnosticFactory.create(ERROR, "A trait may not have a constructor");
DiagnosticFactory<JetSecondaryConstructor> SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED = DiagnosticFactory.create(WARNING, "Secondary constructors are not supported");
DiagnosticFactory<JetTypeReference> SUPERTYPE_APPEARS_TWICE = DiagnosticFactory.create(ERROR, "A supertype appears twice");
DiagnosticFactory<JetTypeReference> FINAL_SUPERTYPE = DiagnosticFactory.create(ERROR, "This type is final, so it cannot be inherited from");
@@ -27,6 +27,7 @@ import java.util.List;
import java.util.Map;
import static org.jetbrains.jet.lang.diagnostics.Errors.CONSTRUCTOR_IN_TRAIT;
import static org.jetbrains.jet.lang.diagnostics.Errors.SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED;
/**
* @author abreslav
@@ -219,6 +220,7 @@ public class DeclarationResolver {
}
private void processSecondaryConstructor(MutableClassDescriptor classDescriptor, JetSecondaryConstructor constructor) {
trace.report(SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED.on(constructor));
if (classDescriptor.getKind() == ClassKind.TRAIT) {
trace.report(CONSTRUCTOR_IN_TRAIT.on(constructor.getNameNode().getPsi()));
}
@@ -53,10 +53,10 @@ abstract class B1(
class B2() : B1(1, "r") {}
abstract class B3(i: Int) {
this(): this(1)
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED!>this(): this(1)<!>
}
fun foo(<!UNUSED_PARAMETER!>c<!>: B3) {
val <!UNUSED_VARIABLE!>a<!> = <!CREATING_AN_INSTANCE_OF_ABSTRACT_CLASS!>B3()<!>
val <!UNUSED_VARIABLE!>b<!> = <!CREATING_AN_INSTANCE_OF_ABSTRACT_CLASS!>B1(2, "s")<!>
}
}
@@ -27,8 +27,8 @@ class WithC() {
val zzz = <!NO_BACKING_FIELD_CUSTOM_ACCESSORS!>$a<!>
}
this(a : Int) : this() {
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED!>this(a : Int) : this() {
val <!UNUSED_VARIABLE!>b<!> = x
}
}<!>
}
}
@@ -8,23 +8,23 @@ class NoC3 : WithC1()
class WithC2() : <!SUPERTYPE_NOT_INITIALIZED_DEFAULT!>WithC1<!>
class <!CONFLICTING_OVERLOADS!>NoPC<!> {
<!CONFLICTING_OVERLOADS!><!SECONDARY_CONSTRUCTOR_NO_INITIALIZER_LIST!>this<!>() {}<!>
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED, CONFLICTING_OVERLOADS!><!SECONDARY_CONSTRUCTOR_NO_INITIALIZER_LIST!>this<!>() {}<!>
}
class WithPC0() {
this(a : Int) : this() {}
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED!>this(a : Int) : this() {}<!>
}
class WithPC1(a : Int) {
<!SECONDARY_CONSTRUCTOR_NO_INITIALIZER_LIST!>this<!>() {}
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED!><!SECONDARY_CONSTRUCTOR_NO_INITIALIZER_LIST!>this<!>() {}<!>
this(b : Long) : this("") {}
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED!>this(b : Long) : this("") {}<!>
this(s : String) : this(1) {}
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED!>this(s : String) : this(1) {}<!>
this(b : Char) : <!NONE_APPLICABLE!>this<!>("", 2) {}
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED!>this(b : Char) : <!NONE_APPLICABLE!>this<!>("", 2) {}<!>
this(b : Byte) : this(""), <!MANY_CALLS_TO_THIS!>this(1)<!> {}
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED!>this(b : Byte) : this(""), <!MANY_CALLS_TO_THIS!>this(1)<!> {}<!>
}
@@ -47,4 +47,4 @@ class NoCPI {
var ab = <!PROPERTY_INITIALIZER_NO_BACKING_FIELD!>1<!>
get() = 1
set(v) {}
}
}
@@ -7,9 +7,9 @@ fun bar(x : Int = <!TYPE_MISMATCH!>""<!>, y : Int = x, <!UNUSED_PARAMETER!>z<!>
// KT-371 Resolve default parameters for constructors
class A(x : Int = <!UNINITIALIZED_PARAMETER!>y<!>, y : Int = x) { // None of the references is resolved, no types checked
this(bool: Boolean, a: Int = <!TYPE_MISMATCH, UNINITIALIZED_PARAMETER!>b<!>, b: String = <!TYPE_MISMATCH!>a<!>) : this(1) {}
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED!>this(bool: Boolean, a: Int = <!TYPE_MISMATCH, UNINITIALIZED_PARAMETER!>b<!>, b: String = <!TYPE_MISMATCH!>a<!>) : this(1) {}<!>
}
val z = 3
fun foo(x: Int = <!UNINITIALIZED_PARAMETER!>y<!>, y: Int = x, <!UNUSED_PARAMETER!>i<!> : Int = z): Int = x + y
fun foo(x: Int = <!UNINITIALIZED_PARAMETER!>y<!>, y: Int = x, <!UNUSED_PARAMETER!>i<!> : Int = z): Int = x + y
@@ -1,4 +1,3 @@
fun none() {}
fun unitEmptyInfer() {}
@@ -138,11 +137,11 @@ fun nonBlockNoReturnIfUnitInOneBranch(): Int = if (1 < 2) <!TYPE_MISMATCH!>{}<!>
val a = <!RETURN_NOT_ALLOWED!>return 1<!>
class A() {
this(a : Int) : this() {
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED!>this(a : Int) : this() {
if (a == 1)
return
return <!TYPE_MISMATCH!>1<!>
}
}<!>
}
fun illegalConstantBody(): Int = <!TYPE_MISMATCH!>"s"<!>
@@ -210,4 +209,4 @@ fun testFunctionLiterals() {
object A {}
}
}
}
@@ -16,14 +16,14 @@ class Test() {
var a : Int = 111
var b : Int get() = $a; set(x) {a = x; $a = x}
this(i : Int) : this() {
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED!>this(i : Int) : this() {
<!NO_BACKING_FIELD_CUSTOM_ACCESSORS!>$b<!> = $a
$a = <!NO_BACKING_FIELD_CUSTOM_ACCESSORS!>$b<!>
a = <!NO_BACKING_FIELD_CUSTOM_ACCESSORS!>$b<!>
}
}<!>
fun f() {
<!NO_BACKING_FIELD_CUSTOM_ACCESSORS!>$b<!> = $a
a = <!NO_BACKING_FIELD_CUSTOM_ACCESSORS!>$b<!>
}
public val <!PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE!>i<!> = 1
}
}
@@ -22,7 +22,7 @@ trait T1 {}
trait T2<T> {}
trait Test<!CONSTRUCTOR_IN_TRAIT!>()<!> {
<!CONSTRUCTOR_IN_TRAIT, SECONDARY_CONSTRUCTOR_NO_INITIALIZER_LIST!>this<!>(x : Int) {}
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED!><!CONSTRUCTOR_IN_TRAIT, SECONDARY_CONSTRUCTOR_NO_INITIALIZER_LIST!>this<!>(x : Int) {}<!>
}
trait Test1 : C2<!SUPERTYPE_INITIALIZED_IN_TRAIT!>()<!> {}
@@ -47,4 +47,4 @@ class CTest4 : T1 {}
class CTest5 : T1, <!SUPERTYPE_APPEARS_TWICE!>T1<!> {}
class CTest6 : <!SUPERTYPE_NOT_INITIALIZED_DEFAULT, FINAL_SUPERTYPE!>C1<!> {}
class CTest6 : <!SUPERTYPE_NOT_INITIALIZED_DEFAULT, FINAL_SUPERTYPE!>C1<!> {}
@@ -1,11 +1,11 @@
open class bar()
trait Foo<!CONSTRUCTOR_IN_TRAIT!>()<!> : bar<!SUPERTYPE_INITIALIZED_IN_TRAIT!>()<!>, <!MANY_CLASSES_IN_SUPERTYPE_LIST, SUPERTYPE_APPEARS_TWICE!>bar<!>, <!MANY_CLASSES_IN_SUPERTYPE_LIST, SUPERTYPE_APPEARS_TWICE!>bar<!> {
<!CONSTRUCTOR_IN_TRAIT, SECONDARY_CONSTRUCTOR_NO_INITIALIZER_LIST!>this<!>(x : Int) {}
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED!><!CONSTRUCTOR_IN_TRAIT, SECONDARY_CONSTRUCTOR_NO_INITIALIZER_LIST!>this<!>(x : Int) {}<!>
}
trait Foo2 : bar, Foo {
}
open class Foo1() : bar(), <!SUPERTYPE_NOT_INITIALIZED_DEFAULT, MANY_CLASSES_IN_SUPERTYPE_LIST, SUPERTYPE_APPEARS_TWICE!>bar<!>, Foo, <!SUPERTYPE_APPEARS_TWICE!>Foo<!><!CONSTRUCTOR_IN_TRAIT!>()<!> {}
open class Foo12 : bar(), <!SUPERTYPE_NOT_INITIALIZED_DEFAULT, MANY_CLASSES_IN_SUPERTYPE_LIST, SUPERTYPE_APPEARS_TWICE!>bar<!> {}
open class Foo12 : bar(), <!SUPERTYPE_NOT_INITIALIZED_DEFAULT, MANY_CLASSES_IN_SUPERTYPE_LIST, SUPERTYPE_APPEARS_TWICE!>bar<!> {}
@@ -7,11 +7,11 @@ class Foo(var bar : Int, var barr : Int, var barrr : Int) {
this : Foo
}
this(var bar : Int) : this(1, 1, 1) {
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED!>this(var bar : Int) : this(1, 1, 1) {
bar = <!UNUSED_VALUE!>1<!>
this.bar
1 : Int
val <!UNUSED_VARIABLE!>a<!> : Int =1
this : Foo
}
}
}<!>
}
@@ -1,3 +1,3 @@
class Z() {
this(x : Int) : this() {}
}
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED!>this(x : Int) : this() {}<!>
}
@@ -1,4 +1,3 @@
package kt_250_617_10
import java.util.ArrayList
@@ -43,9 +42,9 @@ open class X(p: Int, r: Int) {
class Y(i: Int) : X(i, <!UNRESOLVED_REFERENCE!>rrr<!>) {
val rrr = 3
this(s: Int, r: Int) : this(s, <!UNRESOLVED_REFERENCE!>rrr<!>)
<!SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED!>this(s: Int, r: Int) : this(s, <!UNRESOLVED_REFERENCE!>rrr<!>)<!>
}
class Z(val i: Int) : X(<!UNRESOLVED_REFERENCE!>s<!>, <!UNRESOLVED_REFERENCE!>x<!>) {
val x = 2
}
}
+1 -1
View File
@@ -157,7 +157,7 @@ abstract class B1(
class B2() : B1(1, "r") {}
abstract class B3(i: Int) {
this(): this(1)
<warning>this(): this(1)</warning>
}
fun foo(<warning>a</warning>: B3) {
@@ -27,8 +27,8 @@ class WithC() {
val zzz = <error>$a</error>
}
this(a : Int) : this() {
<warning>this(a : Int) : this() {
val <warning>b</warning> = x
}
}</warning>
}
+6 -6
View File
@@ -13,19 +13,19 @@ class <error>NoPC</error> {
}
class WithPC0() {
this(a : Int) : this() {}
<warning>this(a : Int) : this() {}</warning>
}
class WithPC1(a : Int) {
<error>this</error>() {}
<warning><error>this</error>() {}</warning>
this(b : Long) : this("") {}
<warning>this(b : Long) : this("") {}</warning>
this(s : String) : this(1) {}
<warning>this(s : String) : this(1) {}</warning>
this(b : Char) : <error>this</error>("", 2) {}
<warning>this(b : Char) : <error>this</error>("", 2) {}</warning>
this(b : Byte) : this(""), <error>this(1)</error> {}
<warning>this(b : Byte) : this(""), <error>this(1)</error> {}</warning>
}
@@ -134,11 +134,11 @@ fun nonBlockNoReturnIfUnitInOneBranch(): Int = if (1 < 2) <error>{}</error> else
val a = <error>return 1</error>
class A() {
this(a : Int) : this() {
<warning>this(a : Int) : this() {
if (a == 1)
return
return <error>1</error>
}
}</warning>
}
fun illegalConstantBody(): Int = <error>"s"</error>
+2 -2
View File
@@ -16,11 +16,11 @@ class Test() {
var a : Int = 111
var b : Int get() = $a; set(x) {a = x; $a = x}
this(i : Int) : this() {
<warning>this(i : Int) : this() {
<error>$b</error> = $a
$a = <error>$b</error>
a = <error>$b</error>
}
}</warning>
fun f() {
<error>$b</error> = $a
a = <error>$b</error>
@@ -22,7 +22,7 @@ trait T1 {}
trait T2<T> {}
trait Test<error>()</error> {
<error>this</error>(x : Int) {}
<warning><error>this</error>(x : Int) {}</warning>
}
trait Test1 : C2<error>()</error> {}
+1 -1
View File
@@ -1,7 +1,7 @@
open class bar()
trait Foo<error>()</error> : bar<error>()</error>, <error>bar</error>, <error>bar</error> {
<error>this</error>(x : Int) {}
<warning><error>this</error>(x : Int) {}</warning>
}
trait Foo2 : bar, Foo {
@@ -7,12 +7,12 @@
this : Foo
}
this(var bar : Int) : this(1, 1, 1) {
<warning>this(var bar : Int) : this(1, 1, 1) {
bar = <warning>1</warning>
this.bar
1 : Int
val <warning>a</warning> : Int =1
this : Foo
}
}</warning>
}
@@ -1,3 +1,3 @@
class Z() {
this(x : Int) : this() {}
<warning>this(x : Int) : this() {}</warning>
}