From a91a53f7f19c43c004ddd5924373e5819c1e8601 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Tue, 27 Mar 2012 17:48:04 +0400 Subject: [PATCH] KT-1531 Frontend should prohibit secondary constructors # KT-1531 Fixed --- .../jetbrains/jet/lang/diagnostics/Errors.java | 1 + .../jet/lang/resolve/DeclarationResolver.java | 2 ++ compiler/testData/diagnostics/tests/Abstract.jet | 4 ++-- .../diagnostics/tests/AnonymousInitializers.jet | 6 +++--- .../testData/diagnostics/tests/Constructors.jet | 16 ++++++++-------- .../tests/DefaultValuesTypechecking.jet | 4 ++-- .../diagnostics/tests/FunctionReturnTypes.jet | 7 +++---- .../testData/diagnostics/tests/Properties.jet | 6 +++--- .../diagnostics/tests/SupertypeListChecks.jet | 4 ++-- .../diagnostics/tests/TraitSupertypeList.jet | 4 ++-- .../ScopeForSecondaryConstructors.jet | 6 +++--- .../ThisConstructorInGenericClass.jet | 4 ++-- .../diagnostics/tests/scopes/kt250.617.10.jet | 5 ++--- idea/testData/checker/Abstract.jet | 2 +- idea/testData/checker/AnonymousInitializers.jet | 4 ++-- idea/testData/checker/Constructors.jet | 12 ++++++------ idea/testData/checker/FunctionReturnTypes.jet | 4 ++-- idea/testData/checker/Properties.jet | 4 ++-- idea/testData/checker/SupertypeListChecks.jet | 2 +- idea/testData/checker/TraitSupertypeList.jet | 2 +- .../regression/ScopeForSecondaryConstructors.jet | 4 ++-- .../regression/ThisConstructorInGenericClass.jet | 2 +- 22 files changed, 53 insertions(+), 52 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java index 932764ea5b4..52ebcbd8be8 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java @@ -302,6 +302,7 @@ public interface Errors { DiagnosticFactory SUPERTYPE_NOT_A_CLASS_OR_TRAIT = DiagnosticFactory.create(ERROR, "Only classes and traits may serve as supertypes"); DiagnosticFactory SUPERTYPE_INITIALIZED_IN_TRAIT = DiagnosticFactory.create(ERROR, "Traits cannot initialize supertypes"); DiagnosticFactory CONSTRUCTOR_IN_TRAIT = DiagnosticFactory.create(ERROR, "A trait may not have a constructor"); + DiagnosticFactory SECONDARY_CONSTRUCTORS_ARE_NOT_SUPPORTED = DiagnosticFactory.create(WARNING, "Secondary constructors are not supported"); DiagnosticFactory SUPERTYPE_APPEARS_TWICE = DiagnosticFactory.create(ERROR, "A supertype appears twice"); DiagnosticFactory FINAL_SUPERTYPE = DiagnosticFactory.create(ERROR, "This type is final, so it cannot be inherited from"); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DeclarationResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DeclarationResolver.java index 5fc3ca3008e..04de9ed92b0 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DeclarationResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DeclarationResolver.java @@ -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())); } diff --git a/compiler/testData/diagnostics/tests/Abstract.jet b/compiler/testData/diagnostics/tests/Abstract.jet index 6ada45c4784..35a73b65f5a 100644 --- a/compiler/testData/diagnostics/tests/Abstract.jet +++ b/compiler/testData/diagnostics/tests/Abstract.jet @@ -53,10 +53,10 @@ abstract class B1( class B2() : B1(1, "r") {} abstract class B3(i: Int) { - this(): this(1) + this(): this(1) } fun foo(c: B3) { val a = B3() val b = B1(2, "s") -} +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/AnonymousInitializers.jet b/compiler/testData/diagnostics/tests/AnonymousInitializers.jet index 51ffa96a42c..47251bef500 100644 --- a/compiler/testData/diagnostics/tests/AnonymousInitializers.jet +++ b/compiler/testData/diagnostics/tests/AnonymousInitializers.jet @@ -27,8 +27,8 @@ class WithC() { val zzz = $a } - this(a : Int) : this() { + this(a : Int) : this() { val b = x - } + } -} +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/Constructors.jet b/compiler/testData/diagnostics/tests/Constructors.jet index 6aed9765f29..fbce71c5942 100644 --- a/compiler/testData/diagnostics/tests/Constructors.jet +++ b/compiler/testData/diagnostics/tests/Constructors.jet @@ -8,23 +8,23 @@ class NoC3 : WithC1() class WithC2() : WithC1 class NoPC { - this() {} + this() {} } class WithPC0() { - this(a : Int) : this() {} + this(a : Int) : this() {} } class WithPC1(a : Int) { - this() {} + this() {} - this(b : Long) : this("") {} + this(b : Long) : this("") {} - this(s : String) : this(1) {} + this(s : String) : this(1) {} - this(b : Char) : this("", 2) {} + this(b : Char) : this("", 2) {} - this(b : Byte) : this(""), this(1) {} + this(b : Byte) : this(""), this(1) {} } @@ -47,4 +47,4 @@ class NoCPI { var ab = 1 get() = 1 set(v) {} -} +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/DefaultValuesTypechecking.jet b/compiler/testData/diagnostics/tests/DefaultValuesTypechecking.jet index 0f9cc687872..f794c55a8a9 100644 --- a/compiler/testData/diagnostics/tests/DefaultValuesTypechecking.jet +++ b/compiler/testData/diagnostics/tests/DefaultValuesTypechecking.jet @@ -7,9 +7,9 @@ fun bar(x : Int = "", y : Int = x, z // KT-371 Resolve default parameters for constructors class A(x : Int = y, y : Int = x) { // None of the references is resolved, no types checked - this(bool: Boolean, a: Int = b, b: String = a) : this(1) {} + this(bool: Boolean, a: Int = b, b: String = a) : this(1) {} } val z = 3 -fun foo(x: Int = y, y: Int = x, i : Int = z): Int = x + y +fun foo(x: Int = y, y: Int = x, i : Int = z): Int = x + y \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/FunctionReturnTypes.jet b/compiler/testData/diagnostics/tests/FunctionReturnTypes.jet index 974e9347675..1ee14186849 100644 --- a/compiler/testData/diagnostics/tests/FunctionReturnTypes.jet +++ b/compiler/testData/diagnostics/tests/FunctionReturnTypes.jet @@ -1,4 +1,3 @@ - fun none() {} fun unitEmptyInfer() {} @@ -138,11 +137,11 @@ fun nonBlockNoReturnIfUnitInOneBranch(): Int = if (1 < 2) {} val a = return 1 class A() { - this(a : Int) : this() { + this(a : Int) : this() { if (a == 1) return return 1 - } + } } fun illegalConstantBody(): Int = "s" @@ -210,4 +209,4 @@ fun testFunctionLiterals() { object A {} } -} +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/Properties.jet b/compiler/testData/diagnostics/tests/Properties.jet index d59c29a5908..437fb593054 100644 --- a/compiler/testData/diagnostics/tests/Properties.jet +++ b/compiler/testData/diagnostics/tests/Properties.jet @@ -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() { + this(i : Int) : this() { $b = $a $a = $b a = $b - } + } fun f() { $b = $a a = $b } public val i = 1 -} +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/SupertypeListChecks.jet b/compiler/testData/diagnostics/tests/SupertypeListChecks.jet index e3f8185de46..8c6fff106ee 100644 --- a/compiler/testData/diagnostics/tests/SupertypeListChecks.jet +++ b/compiler/testData/diagnostics/tests/SupertypeListChecks.jet @@ -22,7 +22,7 @@ trait T1 {} trait T2 {} trait Test() { - this(x : Int) {} + this(x : Int) {} } trait Test1 : C2() {} @@ -47,4 +47,4 @@ class CTest4 : T1 {} class CTest5 : T1, T1 {} -class CTest6 : C1 {} +class CTest6 : C1 {} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/TraitSupertypeList.jet b/compiler/testData/diagnostics/tests/TraitSupertypeList.jet index cb73f1e1baa..320e4bcf052 100644 --- a/compiler/testData/diagnostics/tests/TraitSupertypeList.jet +++ b/compiler/testData/diagnostics/tests/TraitSupertypeList.jet @@ -1,11 +1,11 @@ open class bar() trait Foo() : bar(), bar, bar { - this(x : Int) {} + this(x : Int) {} } trait Foo2 : bar, Foo { } open class Foo1() : bar(), bar, Foo, Foo() {} -open class Foo12 : bar(), bar {} +open class Foo12 : bar(), bar {} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/regressions/ScopeForSecondaryConstructors.jet b/compiler/testData/diagnostics/tests/regressions/ScopeForSecondaryConstructors.jet index 352d736cd78..375141603f7 100644 --- a/compiler/testData/diagnostics/tests/regressions/ScopeForSecondaryConstructors.jet +++ b/compiler/testData/diagnostics/tests/regressions/ScopeForSecondaryConstructors.jet @@ -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) { + this(var bar : Int) : this(1, 1, 1) { bar = 1 this.bar 1 : Int val a : Int =1 this : Foo - } - } + } + } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/regressions/ThisConstructorInGenericClass.jet b/compiler/testData/diagnostics/tests/regressions/ThisConstructorInGenericClass.jet index 13ac5a7d8de..a2b408ae64d 100644 --- a/compiler/testData/diagnostics/tests/regressions/ThisConstructorInGenericClass.jet +++ b/compiler/testData/diagnostics/tests/regressions/ThisConstructorInGenericClass.jet @@ -1,3 +1,3 @@ class Z() { - this(x : Int) : this() {} -} + this(x : Int) : this() {} +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/scopes/kt250.617.10.jet b/compiler/testData/diagnostics/tests/scopes/kt250.617.10.jet index 542b12e2fa3..869a85957ac 100644 --- a/compiler/testData/diagnostics/tests/scopes/kt250.617.10.jet +++ b/compiler/testData/diagnostics/tests/scopes/kt250.617.10.jet @@ -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, rrr) { val rrr = 3 - this(s: Int, r: Int) : this(s, rrr) + this(s: Int, r: Int) : this(s, rrr) } class Z(val i: Int) : X(s, x) { val x = 2 -} +} \ No newline at end of file diff --git a/idea/testData/checker/Abstract.jet b/idea/testData/checker/Abstract.jet index 95e0f01792f..07f39c65a4b 100644 --- a/idea/testData/checker/Abstract.jet +++ b/idea/testData/checker/Abstract.jet @@ -157,7 +157,7 @@ abstract class B1( class B2() : B1(1, "r") {} abstract class B3(i: Int) { - this(): this(1) + this(): this(1) } fun foo(a: B3) { diff --git a/idea/testData/checker/AnonymousInitializers.jet b/idea/testData/checker/AnonymousInitializers.jet index 5fdfd561edb..921bf4cfb82 100644 --- a/idea/testData/checker/AnonymousInitializers.jet +++ b/idea/testData/checker/AnonymousInitializers.jet @@ -27,8 +27,8 @@ class WithC() { val zzz = $a } - this(a : Int) : this() { + this(a : Int) : this() { val b = x - } + } } \ No newline at end of file diff --git a/idea/testData/checker/Constructors.jet b/idea/testData/checker/Constructors.jet index b2fabb321de..cbd1033db99 100644 --- a/idea/testData/checker/Constructors.jet +++ b/idea/testData/checker/Constructors.jet @@ -13,19 +13,19 @@ class NoPC { } class WithPC0() { - this(a : Int) : this() {} + this(a : Int) : this() {} } class WithPC1(a : Int) { - this() {} + this() {} - this(b : Long) : this("") {} + this(b : Long) : this("") {} - this(s : String) : this(1) {} + this(s : String) : this(1) {} - this(b : Char) : this("", 2) {} + this(b : Char) : this("", 2) {} - this(b : Byte) : this(""), this(1) {} + this(b : Byte) : this(""), this(1) {} } diff --git a/idea/testData/checker/FunctionReturnTypes.jet b/idea/testData/checker/FunctionReturnTypes.jet index c12170c87d0..16b238478e5 100644 --- a/idea/testData/checker/FunctionReturnTypes.jet +++ b/idea/testData/checker/FunctionReturnTypes.jet @@ -134,11 +134,11 @@ fun nonBlockNoReturnIfUnitInOneBranch(): Int = if (1 < 2) {} else val a = return 1 class A() { - this(a : Int) : this() { + this(a : Int) : this() { if (a == 1) return return 1 - } + } } fun illegalConstantBody(): Int = "s" diff --git a/idea/testData/checker/Properties.jet b/idea/testData/checker/Properties.jet index 6c089bd09a8..b116ee187d4 100644 --- a/idea/testData/checker/Properties.jet +++ b/idea/testData/checker/Properties.jet @@ -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() { + this(i : Int) : this() { $b = $a $a = $b a = $b - } + } fun f() { $b = $a a = $b diff --git a/idea/testData/checker/SupertypeListChecks.jet b/idea/testData/checker/SupertypeListChecks.jet index 4a0cde7049c..cbe946c5eaa 100644 --- a/idea/testData/checker/SupertypeListChecks.jet +++ b/idea/testData/checker/SupertypeListChecks.jet @@ -22,7 +22,7 @@ trait T1 {} trait T2 {} trait Test() { - this(x : Int) {} + this(x : Int) {} } trait Test1 : C2() {} diff --git a/idea/testData/checker/TraitSupertypeList.jet b/idea/testData/checker/TraitSupertypeList.jet index 696e5d5424e..cf3b2d0e236 100644 --- a/idea/testData/checker/TraitSupertypeList.jet +++ b/idea/testData/checker/TraitSupertypeList.jet @@ -1,7 +1,7 @@ open class bar() trait Foo() : bar(), bar, bar { - this(x : Int) {} + this(x : Int) {} } trait Foo2 : bar, Foo { diff --git a/idea/testData/checker/regression/ScopeForSecondaryConstructors.jet b/idea/testData/checker/regression/ScopeForSecondaryConstructors.jet index 84c59da5a8a..2f5c5fb9554 100644 --- a/idea/testData/checker/regression/ScopeForSecondaryConstructors.jet +++ b/idea/testData/checker/regression/ScopeForSecondaryConstructors.jet @@ -7,12 +7,12 @@ this : Foo } - this(var bar : Int) : this(1, 1, 1) { + this(var bar : Int) : this(1, 1, 1) { bar = 1 this.bar 1 : Int val a : Int =1 this : Foo - } + } } diff --git a/idea/testData/checker/regression/ThisConstructorInGenericClass.jet b/idea/testData/checker/regression/ThisConstructorInGenericClass.jet index cb4408bd600..074bec82a22 100644 --- a/idea/testData/checker/regression/ThisConstructorInGenericClass.jet +++ b/idea/testData/checker/regression/ThisConstructorInGenericClass.jet @@ -1,3 +1,3 @@ class Z() { - this(x : Int) : this() {} + this(x : Int) : this() {} } \ No newline at end of file