From d079c0291289ad9f6517b0b929c4f03ef6b9f082 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 3 Apr 2014 17:55:20 +0200 Subject: Flag self names that conflict with parameters or members A self name may no longer have the same name as a parameterless class member (or param accessor). The restriction makes sense because otherwise scoping is confusing. It's needed because otherwise we get TermRefs that have the same name and prefix but denote different things. Moved some code which exercises this from pos/typers to neg/typers --- tests/neg/typers.scala | 62 +++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 23 deletions(-) (limited to 'tests/neg/typers.scala') diff --git a/tests/neg/typers.scala b/tests/neg/typers.scala index d25a8911e..2f1cf40c4 100644 --- a/tests/neg/typers.scala +++ b/tests/neg/typers.scala @@ -1,40 +1,56 @@ object typers { - + + class A(x: Int) { + val x: String = "a" // error: double def + + { val y: String = "" + val y: Int = 0 // error: double def + y + } + } + + class B { self => // error: double def + def self: Int = 0 + def self(x: Int): Int = x + } + class C { val x: Int - val x: String + val x: String // error: double def val y: Int - def y: String + def y: String // error: double def val z: Int - def z(): String - - def f(x: Any) = () + def z(): String // error: double def + + def f(x: Any) = () // error: double def def f(x: AnyRef): AnyRef - + def g(x: Object): Unit - def g[T](x: T): T = x + def g[T](x: T): T = x // error: double def } - - + + + + object returns { - - def foo(x: Int) = { + + def foo(x: Int) = { // error: has return; needs result type return 3 } - - return 4 + + return 4 // error: return outside method definition } - + object cyclic { - def factorial(acc: Int, n: Int) = + def factorial(acc: Int, n: Int) = if (n == 0) acc - else factorial(acc * n, n - 1) - - def foo(x: Int) = x + else factorial(acc * n, n - 1) // error: cyclic reference + + def foo(x: Int) = x // error: cyclic reference def foo() = foo(1) - + } - + object tries { val x = try { @@ -46,6 +62,6 @@ object typers { } class Refinements { - val y: C { val x: T; type T } + val y: C { val x: T; type T } // error: illegal forward reference in refinement } -} \ No newline at end of file +} -- cgit v1.2.3