From fc6ea96f1946712a889b7db50a520be49ea2e208 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Sun, 10 Jun 2012 21:59:35 +0200 Subject: SI-5696 Detect excess constructor argument lists. An apply method fooled the usual mechanism. --- .../tools/nsc/typechecker/ContextErrors.scala | 5 +++ .../scala/tools/nsc/typechecker/Typers.scala | 2 + test/files/neg/t5696.check | 19 +++++++++ test/files/neg/t5696.scala | 47 ++++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 test/files/neg/t5696.check create mode 100644 test/files/neg/t5696.scala diff --git a/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala b/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala index a2f0e053a8..bcf2ba6b71 100644 --- a/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala +++ b/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala @@ -224,6 +224,11 @@ trait ContextErrors { def SuperConstrArgsThisReferenceError(tree: Tree) = NormalTypeError(tree, "super constructor arguments cannot reference unconstructed `this`") + def TooManyArgumentListsForConstructor(tree: Tree) = { + issueNormalTypeError(tree, "too many argument lists for constructor invocation") + setError(tree) + } + // typedValDef def VolatileValueError(vdef: Tree) = issueNormalTypeError(vdef, "values cannot be volatile") diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index fec5063bd0..2bdae4164a 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -4950,6 +4950,8 @@ trait Typers extends Modes with Adaptations with Tags { else new ApplyToImplicitArgs(Select(tag, nme.newArray), args) } typed(newArrayApp, mode, pt) + case Apply(Select(fun, nme.apply), _) if treeInfo.isSuperConstrCall(fun) => //SI-5696 + TooManyArgumentListsForConstructor(tree) case tree1 => tree1 } diff --git a/test/files/neg/t5696.check b/test/files/neg/t5696.check new file mode 100644 index 0000000000..72b7781fc4 --- /dev/null +++ b/test/files/neg/t5696.check @@ -0,0 +1,19 @@ +t5696.scala:6: error: too many argument lists for constructor invocation + new G(1)(2) {} + ^ +t5696.scala:14: error: too many argument lists for constructor invocation + new G()(2) {} + ^ +t5696.scala:22: error: too many argument lists for constructor invocation + new G[Int]()(2) {} + ^ +t5696.scala:30: error: too many argument lists for constructor invocation + new G[Int]()(2)(3) {} + ^ +t5696.scala:38: error: too many argument lists for constructor invocation + new G[Int]()()(2) {} + ^ +t5696.scala:46: error: too many argument lists for constructor invocation + object x extends G(1)(2) {} + ^ +6 errors found diff --git a/test/files/neg/t5696.scala b/test/files/neg/t5696.scala new file mode 100644 index 0000000000..051e3a07f9 --- /dev/null +++ b/test/files/neg/t5696.scala @@ -0,0 +1,47 @@ +class TestApply1 { + class G(y: Int) { + def apply(x: Int) = 1 + } + + new G(1)(2) {} +} + +class TestApply2 { + class G { + def apply(x: Int) = 1 + } + + new G()(2) {} +} + +class TestApply3 { + class G[X] { + def apply(x: Int) = 1 + } + + new G[Int]()(2) {} +} + +class TestApply4 { + class G[X] { + def apply(x: Int)(y: Int) = 1 + } + + new G[Int]()(2)(3) {} +} + +class TestApply5 { + class G[X]()() { + def apply(x: Int) = 1 + } + + new G[Int]()()(2) {} +} + +class TestApply6 { + class G(y: Int) { + def apply(x: Int) = 1 + } + + object x extends G(1)(2) {} +} -- cgit v1.2.3