From 181cefa87219f65d9b6d101049d4916db6715092 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 10 Jan 2007 16:26:29 +0000 Subject: fixed bug875 --- .../scala/tools/nsc/transform/UnCurry.scala | 26 ++++++-------------- .../scala/tools/nsc/typechecker/Typers.scala | 23 ++++++++++++++---- test/files/neg/bug875.check | 13 ++++++++++ test/files/neg/bug875.scala | 18 ++++++++++++++ test/files/neg/bug876.check | 4 ++++ test/files/neg/bug876.scala | 28 ++++++++++++++++++++++ test/files/neg/bug877.check | 4 ++++ test/files/neg/bug877.scala | 3 +++ test/files/pos/bug873.scala | 10 ++++++++ test/files/pos/bug880.scala | 6 +++++ test/files/pos/unapply.scala | 8 +++++++ 11 files changed, 119 insertions(+), 24 deletions(-) create mode 100644 test/files/neg/bug875.check create mode 100644 test/files/neg/bug875.scala create mode 100644 test/files/neg/bug876.check create mode 100644 test/files/neg/bug876.scala create mode 100644 test/files/neg/bug877.check create mode 100755 test/files/neg/bug877.scala create mode 100755 test/files/pos/bug873.scala create mode 100755 test/files/pos/bug880.scala create mode 100755 test/files/pos/unapply.scala diff --git a/src/compiler/scala/tools/nsc/transform/UnCurry.scala b/src/compiler/scala/tools/nsc/transform/UnCurry.scala index 9d3180f0f2..352dc3b75c 100644 --- a/src/compiler/scala/tools/nsc/transform/UnCurry.scala +++ b/src/compiler/scala/tools/nsc/transform/UnCurry.scala @@ -277,30 +277,19 @@ abstract class UnCurry extends InfoTransform with TypingTransformers { val args1 = formals.last match { case TypeRef(pre, sym, List(elempt)) if (sym == RepeatedParamClass) => - def mkArrayValue(ts: List[Tree]): Tree = + def mkArrayValue(ts: List[Tree]) = atPos(pos)(ArrayValue(TypeTree(elempt), ts) setType formals.last); - def mkConcat(left: Tree, right: Tree): Tree = - atPos(pos) { - localTyper.typed { - Apply( - TypeApply( - Select(left, nme.PLUSPLUS), - List(TypeTree(elempt))), - List(right)) - } setType formals.last - } + if (args.isEmpty) List(mkArrayValue(args)) else { - val {fixedArgs, varArgs} = args.splitAt(formals.length - 1) - val suffix = args.last match { + val suffix: Tree = args.last match { case Typed(arg, Ident(name)) if name == nme.WILDCARD_STAR.toTypeName => - if (varArgs.length > 1) mkConcat(ArrayValue(TypeTree(elempt), varArgs.init), arg) - else arg setType seqType(arg.tpe) + arg setType seqType(arg.tpe) case _ => - mkArrayValue(varArgs) + mkArrayValue(args.drop(formals.length - 1)) } - fixedArgs ::: List(suffix) + args.take(formals.length - 1) ::: List(suffix) } case _ => args } @@ -487,8 +476,7 @@ abstract class UnCurry extends InfoTransform with TypingTransformers { case Apply(Apply(fn, args), args1) => copy.Apply(tree, fn, args ::: args1) case Ident(name) => - if (name == nme.WILDCARD_STAR.toTypeName) - unit.error(tree.pos, " argument does not correspond to `*'-parameter"); + assert(name != nme.WILDCARD_STAR.toTypeName) applyUnary(tree); case Select(qual, name) => /* Function1.apply to ByNameFunction.apply if qualifier is a ByNameFunction */ diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index be33fb215d..2e98c001c7 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -990,9 +990,11 @@ trait Typers requires Analyzer { var ownAcc = clazz.info.decl(name).suchThat(.hasFlag(PARAMACCESSOR)) if ((ownAcc hasFlag ACCESSOR) && !(ownAcc hasFlag DEFERRED)) ownAcc = ownAcc.accessed - if (settings.debug.value) - log("" + ownAcc + " has alias "+alias + alias.locationString);//debug - ownAcc.asInstanceOf[TermSymbol].setAlias(alias) + if (!ownAcc.isVariable && !alias.accessed.isVariable) { + if (settings.debug.value) + log("" + ownAcc + " has alias "+alias + alias.locationString);//debug + ownAcc.asInstanceOf[TermSymbol].setAlias(alias) + } } } case _ => @@ -1302,8 +1304,18 @@ trait Typers requires Analyzer { def typedArgs(args: List[Tree], mode: int) = List.mapConserve(args)(arg => typedArg(arg, mode, 0, WildcardType)) - def typedArgs(args: List[Tree], mode: int, originalFormals: List[Type], adaptedFormals: List[Type]) = - if ((mode & PATTERNmode) != 0 && isVarArgs(originalFormals)) { + def typedArgs(args: List[Tree], mode: int, originalFormals: List[Type], adaptedFormals: List[Type]) = { + val varargs = isVarArgs(originalFormals) + if (!args.isEmpty) + args.last match { + case Typed(expr, Ident(name)) if (name == nme.WILDCARD_STAR.toTypeName) => + if (!varargs) + error(args.last.pos, "_*-argument does not correspond to *-parameter") + else if (originalFormals.length != adaptedFormals.length) + error(args.last.pos, "_*-argument may not appear after other arguments matching a *-parameter") + case _ => + } + if (varargs && (mode & PATTERNmode) != 0) { val nonVarCount = originalFormals.length - 1 val prefix = List.map2(args take nonVarCount, adaptedFormals take nonVarCount) ((arg, formal) => @@ -1315,6 +1327,7 @@ trait Typers requires Analyzer { } else { List.map2(args, adaptedFormals)((arg, formal) => typedArg(arg, mode, 0, formal)) } + } /** * @param tree ... diff --git a/test/files/neg/bug875.check b/test/files/neg/bug875.check new file mode 100644 index 0000000000..63ad0f7eb0 --- /dev/null +++ b/test/files/neg/bug875.check @@ -0,0 +1,13 @@ +bug875.scala:3: error: _*-argument may not appear after other arguments matching a *-parameter + val ys = List(1, 2, 3, xs: _*) + ^ +bug875.scala:6: error: _*-argument does not correspond to *-parameter + mkList(xs: _*) + ^ +bug875.scala:15: error: _*-argument may not appear after other arguments matching a *-parameter + f(true, 1, xs: _*) + ^ +bug875.scala:16: error: _*-argument may not appear after other arguments matching a *-parameter + g(1, xs:_*) + ^ +four errors found diff --git a/test/files/neg/bug875.scala b/test/files/neg/bug875.scala new file mode 100644 index 0000000000..af0a74d480 --- /dev/null +++ b/test/files/neg/bug875.scala @@ -0,0 +1,18 @@ +object Test extends Application { + val xs = List(4, 5, 6) + val ys = List(1, 2, 3, xs: _*) + def mkList(x: int) = List(x) + def mkList(x: boolean) = List(x) + mkList(xs: _*) + + + def f(x: int*) = List(x: _*) + + def f(x: boolean, y: int*) = List(y: _*) + + def g[a](x: a*) = List(x: _*) + + f(true, 1, xs: _*) + g(1, xs:_*) + +} diff --git a/test/files/neg/bug876.check b/test/files/neg/bug876.check new file mode 100644 index 0000000000..54c27fc96a --- /dev/null +++ b/test/files/neg/bug876.check @@ -0,0 +1,4 @@ +bug876.scala:25: error: wrong number of arguments for method apply: (AssertionError.this.A)manager.B + assert(manager.map(A2) == List(manager.map(A2, A1))) + ^ +one error found diff --git a/test/files/neg/bug876.scala b/test/files/neg/bug876.scala new file mode 100644 index 0000000000..8a94dd9db5 --- /dev/null +++ b/test/files/neg/bug876.scala @@ -0,0 +1,28 @@ +import scala.collection.mutable.HashMap + +object AssertionError extends AnyRef with Application +{ + abstract class A {} + + object A1 extends A {} + + object A2 extends A {} + + class Manager + { + final class B {} + + val map = new HashMap[A, B] + } + + + def test[T](f: => T) { f } + + test { + val manager = new Manager + + // This line is illegal and causes a compiler crash with Scala 2.3.1 + assert(manager.map(A2) == List(manager.map(A2, A1))) + } + +} diff --git a/test/files/neg/bug877.check b/test/files/neg/bug877.check new file mode 100644 index 0000000000..081d5066ae --- /dev/null +++ b/test/files/neg/bug877.check @@ -0,0 +1,4 @@ +bug877.scala:3: error: `{' expected +trait Foo extends A(22A, Bug!) {} + ^ +one error found diff --git a/test/files/neg/bug877.scala b/test/files/neg/bug877.scala new file mode 100755 index 0000000000..5e132a1dd4 --- /dev/null +++ b/test/files/neg/bug877.scala @@ -0,0 +1,3 @@ +class A + +trait Foo extends A(22A, Bug!) {} diff --git a/test/files/pos/bug873.scala b/test/files/pos/bug873.scala new file mode 100755 index 0000000000..b8c50afd35 --- /dev/null +++ b/test/files/pos/bug873.scala @@ -0,0 +1,10 @@ +abstract class Foo { + + val x:Option[List[String]] + val y:List[Int] + + val z = (0:Any) match { + case 1 => x + case 2 => y + } +} diff --git a/test/files/pos/bug880.scala b/test/files/pos/bug880.scala new file mode 100755 index 0000000000..cceb53c398 --- /dev/null +++ b/test/files/pos/bug880.scala @@ -0,0 +1,6 @@ +import scala.xml.Null + +class Test[A >: Null] +{ + val x : A = null +} diff --git a/test/files/pos/unapply.scala b/test/files/pos/unapply.scala new file mode 100755 index 0000000000..17577ef991 --- /dev/null +++ b/test/files/pos/unapply.scala @@ -0,0 +1,8 @@ +object Test { + val xs = List(1) + val f: int = { + xs match { + case List(x) => x + } + } +} -- cgit v1.2.3