summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Rytz <lukas.rytz@epfl.ch>2010-07-13 07:14:37 +0000
committerLukas Rytz <lukas.rytz@epfl.ch>2010-07-13 07:14:37 +0000
commit78d96afa56e51c7ef2ed317ca96b4c4e345ba105 (patch)
treebcc7f0ff554f5db3cb78cd5e154551594e367947
parentada6cccb3587d6d51703e385faba85d8694c9ccd (diff)
downloadscala-78d96afa56e51c7ef2ed317ca96b4c4e345ba105.tar.gz
scala-78d96afa56e51c7ef2ed317ca96b4c4e345ba105.tar.bz2
scala-78d96afa56e51c7ef2ed317ca96b4c4e345ba105.zip
close #3648 (again).
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Namers.scala4
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Typers.scala16
-rw-r--r--test/files/neg/names-defaults-neg.check13
-rw-r--r--test/files/neg/names-defaults-neg.scala5
-rw-r--r--test/files/run/t3648.check1
-rw-r--r--test/files/run/t3648.scala10
6 files changed, 29 insertions, 20 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Namers.scala b/src/compiler/scala/tools/nsc/typechecker/Namers.scala
index ca188099c7..756863f8f9 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Namers.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Namers.scala
@@ -1035,11 +1035,9 @@ trait Namers { self: Analyzer =>
}))
val defRhs = copyUntyped(vparam.rhs)
- val staticFlag = if (isConstr) STATIC else 0
-
val defaultTree = atPos(vparam.pos.focus) {
DefDef(
- Modifiers(meth.flags & (PRIVATE | PROTECTED | FINAL)) | SYNTHETIC | DEFAULTPARAM | oflag | staticFlag,
+ Modifiers(meth.flags & (PRIVATE | PROTECTED | FINAL)) | SYNTHETIC | DEFAULTPARAM | oflag,
name, deftParams, defvParamss, defTpt, defRhs)
}
if (!isConstr)
diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala
index 7d6139c52b..043c41fc10 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala
@@ -2183,6 +2183,17 @@ trait Typers { self: Analyzer =>
def isNamedApplyBlock(tree: Tree) =
context.namedApplyBlockInfo exists (_._1 == tree)
+ def callToCompanionConstr(context: Context, calledFun: Symbol) = {
+ if (calledFun.isConstructor) {
+ val methCtx = context.enclMethod
+ if (methCtx != NoContext) {
+ val contextFun = methCtx.tree.symbol
+ contextFun.isPrimaryConstructor && contextFun.owner.isModuleClass &&
+ companionModuleOf(calledFun.owner, context).moduleClass == contextFun.owner
+ } else false
+ } else false
+ }
+
def doTypedApply(tree: Tree, fun0: Tree, args: List[Tree], mode: Int, pt: Type): Tree = {
var fun = fun0
if (fun.hasSymbol && (fun.symbol hasFlag OVERLOADED)) {
@@ -2320,7 +2331,10 @@ trait Typers { self: Analyzer =>
case _ => false
}
val (allArgs, missing) = addDefaults(args, qual, targs, previousArgss, params, fun.pos.focus, context)
- if (allArgs.length == formals.length) {
+ val funSym = fun1 match { case Block(_, expr) => expr.symbol }
+ if (allArgs.length != args.length && callToCompanionConstr(context, funSym)) {
+ errorTree(tree, "module extending its companion class cannot use default constructor arguments")
+ } else if (allArgs.length == formals.length) {
// useful when a default doesn't match parameter type, e.g. def f[T](x:T="a"); f[Int]()
val note = "Error occurred in an application involving default arguments."
if (!(context.diagnostic contains note)) context.diagnostic = note :: context.diagnostic
diff --git a/test/files/neg/names-defaults-neg.check b/test/files/neg/names-defaults-neg.check
index 38bacc0888..b2b00b7050 100644
--- a/test/files/neg/names-defaults-neg.check
+++ b/test/files/neg/names-defaults-neg.check
@@ -104,17 +104,20 @@ names-defaults-neg.scala:82: error: type mismatch;
Error occurred in an application involving default arguments.
new A2[String]()
^
-names-defaults-neg.scala:115: error: reference to var2 is ambiguous; it is both, a parameter
+names-defaults-neg.scala:86: error: module extending its companion class cannot use default constructor arguments
+ object C extends C()
+ ^
+names-defaults-neg.scala:120: error: reference to var2 is ambiguous; it is both, a parameter
name of the method and the name of a variable currently in scope.
delay(var2 = 40)
^
-names-defaults-neg.scala:118: error: missing parameter type for expanded function ((x$1) => a = x$1)
+names-defaults-neg.scala:123: error: missing parameter type for expanded function ((x$1) => a = x$1)
val taf2: Int => Unit = testAnnFun(a = _, b = get("+"))
^
-names-defaults-neg.scala:119: error: parameter specified twice: a
+names-defaults-neg.scala:124: error: parameter specified twice: a
val taf3 = testAnnFun(b = _: String, a = get(8))
^
-names-defaults-neg.scala:120: error: wrong number of parameters; expected = 2
+names-defaults-neg.scala:125: error: wrong number of parameters; expected = 2
val taf4: (Int, String) => Unit = testAnnFun(_, b = _)
^
-28 errors found
+29 errors found
diff --git a/test/files/neg/names-defaults-neg.scala b/test/files/neg/names-defaults-neg.scala
index e73dc71c9b..43883540a0 100644
--- a/test/files/neg/names-defaults-neg.scala
+++ b/test/files/neg/names-defaults-neg.scala
@@ -81,6 +81,11 @@ object Test extends Application {
// correct error message
new A2[String]()
+ object t3648 {
+ class C(val s: String = "")
+ object C extends C()
+ }
+
// DEFINITIONS
def test1(a: Int, b: String) = a +": "+ b
def test2(x: Unit) = println("test2")
diff --git a/test/files/run/t3648.check b/test/files/run/t3648.check
deleted file mode 100644
index 5f7559f647..0000000000
--- a/test/files/run/t3648.check
+++ /dev/null
@@ -1 +0,0 @@
-kult
diff --git a/test/files/run/t3648.scala b/test/files/run/t3648.scala
deleted file mode 100644
index 91927d6d6d..0000000000
--- a/test/files/run/t3648.scala
+++ /dev/null
@@ -1,10 +0,0 @@
-object Test {
- class C(val s: String = "")
- object C extends C() {
- override def toString() = "kult"
- }
-
- def main(args: Array[String]) {
- println(C)
- }
-}