Skip to content

Commit

Permalink
Merge pull request #8591 from som-snytt/issue/11824
Browse files Browse the repository at this point in the history
Deprecate old-style constructor syntax
  • Loading branch information
lrytz committed Dec 12, 2019
2 parents 4ae0e92 + 4c094fd commit c4cf6c8
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
Expand Up @@ -2749,12 +2749,13 @@ self =>
def funDefOrDcl(start : Int, mods: Modifiers): Tree = {
in.nextToken()
if (in.token == THIS) {
def missingEquals() = deprecationWarning(in.lastOffset, "procedure syntax is deprecated for constructors: add `=`, as in method definition", "2.13.2")
atPos(start, in.skipToken()) {
val vparamss = paramClauses(nme.CONSTRUCTOR, classContextBounds map (_.duplicate), ofCaseClass = false)
newLineOptWhenFollowedBy(LBRACE)
val rhs = in.token match {
case LBRACE => atPos(in.offset) { constrBlock(vparamss) }
case _ => accept(EQUALS) ; atPos(in.offset) { constrExpr(vparamss) }
case LBRACE if !currentRun.isScala214 => missingEquals(); atPos(in.offset) { constrBlock(vparamss) }
case _ => accept(EQUALS) ; atPos(in.offset) { constrExpr(vparamss) }
}
DefDef(mods, nme.CONSTRUCTOR, List(), vparamss, TypeTree(), rhs)
}
Expand Down
2 changes: 1 addition & 1 deletion test/files/jvm/javaReflection/Classes_1.scala
Expand Up @@ -57,7 +57,7 @@ class A {
(() => "5")
}

def this(x: Int) {
def this(x: Int) = {
this()
class Q
trait R
Expand Down
5 changes: 4 additions & 1 deletion test/files/neg/procedure-deprecation.check
Expand Up @@ -10,6 +10,9 @@ procedure-deprecation.scala:6: warning: procedure syntax is deprecated: instead,
procedure-deprecation.scala:7: warning: procedure syntax is deprecated: instead, add `: Unit =` to explicitly declare `boz`'s return type
def boz(i: Int, l: Long) {}
^
procedure-deprecation.scala:8: warning: procedure syntax is deprecated for constructors: add `=`, as in method definition
def this(i: Int) { this() } // Don't complain here! or maybe do complain
^
error: No warnings can be incurred under -Werror.
4 warnings
5 warnings
1 error
4 changes: 2 additions & 2 deletions test/files/neg/procedure-deprecation.scala
@@ -1,10 +1,10 @@
// scalac: -deprecation -Xfatal-warnings
// scalac: -Werror -Xlint:deprecation
//
abstract class Foo {
def bar {}
def baz
def boo(i: Int, l: Long)
def boz(i: Int, l: Long) {}
def this(i: Int) { this() } // Don't complain here!
def this(i: Int) { this() } // Don't complain here! or maybe do complain
def foz: Unit // Don't complain here!
}
8 changes: 7 additions & 1 deletion test/files/neg/procedure-removal.check
Expand Up @@ -10,4 +10,10 @@ procedure-removal.scala:6: error: procedure syntax is unsupported: instead, add
procedure-removal.scala:7: error: procedure syntax is unsupported: instead, add `: Unit =` to explicitly declare `boz`'s return type
def boz(i: Int, l: Long) {}
^
4 errors
procedure-removal.scala:8: error: '=' expected but '{' found.
def this(i: Int) { this() } // Don't complain here! Just slap them with an error.
^
procedure-removal.scala:9: error: 'this' expected.
def foz: Unit // Don't complain here!
^
6 errors
2 changes: 1 addition & 1 deletion test/files/neg/procedure-removal.scala
Expand Up @@ -5,6 +5,6 @@ abstract class Foo {
def baz
def boo(i: Int, l: Long)
def boz(i: Int, l: Long) {}
def this(i: Int) { this() } // Don't complain here!
def this(i: Int) { this() } // Don't complain here! Just slap them with an error.
def foz: Unit // Don't complain here!
}
2 changes: 1 addition & 1 deletion test/files/run/analyzerPlugins.scala
Expand Up @@ -36,7 +36,7 @@ object Test extends DirectTest {
nested()
}
def this(str: String) {
def this(str: String) = {
this(str.toDouble)
math.random
count += 1
Expand Down
4 changes: 2 additions & 2 deletions test/files/run/names-defaults.scala
Expand Up @@ -480,13 +480,13 @@ class A2 {
// using names / defaults in self constructor call.
// overloading resolution: calling A3("string") picks the second, method with default is always less specific.
class A3(x: String, y: Int = 10) {
def this(a: Object) {
def this(a: Object) = {
this(y = 10, x = a.toString())
println(x)
}
}
class A4(x: String, y: Int = 11) {
def this(b: Double, sep: String) {
def this(b: Double, sep: String) = {
this(sep + b + sep)
println(y)
}
Expand Down
2 changes: 1 addition & 1 deletion test/files/run/productElementName.scala
Expand Up @@ -14,7 +14,7 @@ case class Symbols(:: : String, || : Int)
case class MultipleParamLists(a: String, b: Int)(c: Boolean)

case class AuxiliaryConstructor(a: String, b: Int) {
def this(x: String) {
def this(x: String) = {
this(x, 123)
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/files/run/t5543.scala
Expand Up @@ -40,6 +40,6 @@ object T {
class D(val x: Any) {
override def toString() = "D"
// `this` refers again to T
def this(a: Int, b: Int, c: Any = {println(this); this}) { this(c); println(this) } // prints T, then prints D
def this(a: Int, b: Int, c: Any = {println(this); this}) = { this(c); println(this) } // prints T, then prints D
}
}
4 changes: 1 addition & 3 deletions test/files/run/t8197.scala
Expand Up @@ -3,9 +3,7 @@ class A
class B

class Foo(val x: A = null) {
def this(bla: B*) {
this(new A)
}
def this(bla: B*) = this(new A)
}

object Test extends App {
Expand Down

0 comments on commit c4cf6c8

Please sign in to comment.