From 2933e3f3cce9221a1fbe76b434957f8affb54548 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 21 Jun 2006 10:36:06 +0000 Subject: Fixed test files after syntax change for closures. --- src/compiler/scala/tools/nsc/symtab/Types.scala | 31 ++++++++++++++++++---- .../scala/tools/nsc/typechecker/Contexts.scala | 1 + .../scala/tools/nsc/typechecker/Typers.scala | 14 +++++++--- src/library/scala/Predef.scala | 8 ++++++ test/files/jvm/serialization.scala | 2 +- test/files/pos/bug115.scala | 4 +-- test/files/pos/bug628.scala | 17 ++++++++++++ test/files/pos/lambda.scala | 9 ++++--- test/files/run/Course-2002-05.scala | 6 ++--- test/files/run/Course-2002-10.scala | 6 ++--- test/files/run/bug216.scala | 2 +- 11 files changed, 77 insertions(+), 23 deletions(-) create mode 100755 test/files/pos/bug628.scala diff --git a/src/compiler/scala/tools/nsc/symtab/Types.scala b/src/compiler/scala/tools/nsc/symtab/Types.scala index e6a94f4aca..e2f8e23b3f 100644 --- a/src/compiler/scala/tools/nsc/symtab/Types.scala +++ b/src/compiler/scala/tools/nsc/symtab/Types.scala @@ -504,6 +504,10 @@ trait Types requires SymbolTable { // override def isNullable: boolean = true } + case class BoundedWildcardType(override val bounds: TypeBounds) extends Type { + override def toString(): String = "?" + bounds + } + /** An object representing a non-existing type */ case object NoType extends Type { override def isTrivial: boolean = true @@ -1147,9 +1151,10 @@ trait Types requires SymbolTable { /** A class expressing upper and lower bounds constraints * for type variables, as well as their instantiations */ - class TypeConstraint { - var lobounds: List[Type] = List() - var hibounds: List[Type] = List() + class TypeConstraint(lo: List[Type], hi: List[Type]) { + def this() = this(List(), List()) + var lobounds: List[Type] = lo + var hibounds: List[Type] = hi var inst: Type = NoType def instantiate(tp: Type): boolean = @@ -1208,6 +1213,10 @@ trait Types requires SymbolTable { val hi1 = this(hi) if ((lo1 eq lo) && (hi1 eq hi)) tp else TypeBounds(lo1, hi1) + case BoundedWildcardType(bounds) => + val bounds1 = this(bounds) + if (bounds1 eq bounds) tp + else BoundedWildcardType(bounds1.asInstanceOf[TypeBounds]) case RefinedType(parents, decls) => val parents1 = List.mapConserve(parents)(this); val decls1 = mapOver(decls); @@ -1379,8 +1388,12 @@ trait Types requires SymbolTable { * type variable */ object wildcardToTypeVarMap extends TypeMap { def apply(tp: Type): Type = tp match { - case WildcardType => TypeVar(tp, new TypeConstraint) - case _ => mapOver(tp) + case WildcardType => + TypeVar(tp, new TypeConstraint) + case BoundedWildcardType(bounds) => + TypeVar(tp, new TypeConstraint(List(bounds.lo), List(bounds.hi))) + case _ => + mapOver(tp) } } @@ -1575,6 +1588,10 @@ trait Types requires SymbolTable { res1 =:= res2.substSym(tparams2, tparams1)) case Pair(TypeBounds(lo1, hi1), TypeBounds(lo2, hi2)) => lo1 =:= lo2 && hi1 =:= hi2 + case Pair(BoundedWildcardType(bounds), _) => + bounds containsType tp2 + case Pair(_, BoundedWildcardType(bounds)) => + bounds containsType tp1 case Pair(TypeVar(_, constr1), _) => if (constr1.inst != NoType) constr1.inst =:= tp2 else constr1 instantiate (wildcardToTypeVarMap(tp2)) @@ -1664,6 +1681,10 @@ trait Types requires SymbolTable { res1 <:< res2.substSym(tparams2, tparams1)) case Pair(TypeBounds(lo1, hi1), TypeBounds(lo2, hi2)) => lo2 <:< lo1 && hi1 <:< hi2 + case Pair(BoundedWildcardType(bounds), _) => + bounds.lo <:< tp2 + case Pair(_, BoundedWildcardType(bounds)) => + tp1 <:< bounds.hi case Pair(_, TypeVar(_, constr2)) => if (constr2.inst != NoType) tp1 <:< constr2.inst else { constr2.lobounds = tp1 :: constr2.lobounds; true } diff --git a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala index 64a0c72416..f85c5040e4 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala @@ -362,6 +362,7 @@ trait Contexts requires Analyzer { if (tpeCache == null) tpeCache = pre.memberType(sym) tpeCache } + override def toString = "ImplicitInfo("+name+","+pre+","+sym+")" } val NoImplicitInfo = new ImplicitInfo(null, null, null) diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 82af6dbaad..3991798aa1 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -1163,8 +1163,10 @@ trait Typers requires Analyzer { context.undetparams = undetparams val args1 = tryTypedArgs(args map (arg => UnTyper.apply(arg))) context.reportGeneralErrors = reportGeneralErrors + def templateArgType(arg: Tree) = + new BoundedWildcardType(TypeBounds(arg.tpe, AnyClass.tpe)) val qual1 = if (args1 == null || pt.isError) qual - else adaptToMember(qual, name, MethodType(args1 map (.tpe), pt)) + else adaptToMember(qual, name, MethodType(args1 map templateArgType, pt)) val tree1 = Apply(Select(qual1, name) setPos fun.pos, args map (arg => UnTyper.apply(arg))) setPos tree.pos typed1(tree1, mode | SNDTRYmode, pt) } finally { @@ -1572,7 +1574,9 @@ trait Typers requires Analyzer { fun1.isInstanceOf[Select] && !fun1.tpe.isInstanceOf[ImplicitMethodType] && (mode & (EXPRmode | SNDTRYmode)) == EXPRmode) tryTypedApply(fun1, args) - else typedApply(fun1, args) + else { + typedApply(fun1, args) + } } case Super(qual, mix) => @@ -1785,7 +1789,7 @@ trait Typers requires Analyzer { * @returns A typed tree if the implicit info can be made to conform to `pt', EmptyTree otherwise. * @pre info.tpe does not contain an error */ - private def typedImplicit(pos: int, info: ImplicitInfo, pt: Type, isLocal: boolean): Tree = + private def typedImplicit(pos: int, info: ImplicitInfo, pt: Type, isLocal: boolean): Tree = { if (isCompatible(depoly(info.tpe), pt)) { val tree = Ident(info.name) setPos pos def fail(reason: String, sym1: Symbol, sym2: Symbol): Tree = { @@ -1799,12 +1803,14 @@ trait Typers requires Analyzer { if (settings.debug.value) log("typed implicit "+tree1+":"+tree1.tpe+", pt = "+pt); val tree2 = adapt(tree1, EXPRmode, pt) if (settings.debug.value) log("adapted implicit "+tree1.symbol+":"+tree2.tpe+" to "+pt); - if (!tree2.tpe.isError && info.sym == tree1.symbol) tree2 + if (tree2.tpe.isError) EmptyTree + else if (info.sym == tree1.symbol) tree2 else fail("syms differ: ", tree1.symbol, info.sym) } catch { case ex: TypeError => fail(ex.getMessage(), NoSymbol, NoSymbol) } } else EmptyTree + } /** Infer implicit argument or view * @param pos position for error reporting diff --git a/src/library/scala/Predef.scala b/src/library/scala/Predef.scala index 8d0a7d62ca..3b493a4e6b 100644 --- a/src/library/scala/Predef.scala +++ b/src/library/scala/Predef.scala @@ -42,6 +42,14 @@ object Predef { type NullPointerException = java.lang.NullPointerException type Throwable = java.lang.Throwable +/* + type ~[a, b] = Tuple2[a, b] + class FirstOfPair[a](x: a) { + def ~[b](y: b): Tuple2[a, b] = Tuple2(x, y) + } + implicit def any2firstOfPair[a](x: a): FirstOfPair[a] = new FirstOfPair(x) +*/ + type Pair[+a, +b] = Tuple2[a, b] def Pair[a, b](x: a, y: b) = Tuple2(x, y) diff --git a/test/files/jvm/serialization.scala b/test/files/jvm/serialization.scala index f33458fac7..ec1bbbab13 100644 --- a/test/files/jvm/serialization.scala +++ b/test/files/jvm/serialization.scala @@ -43,7 +43,7 @@ object Test1_scala { val x1 = Nil; val x2 = None; val x3 = Array(1, 2, 3); - val x4 = x: Int => 2 * x; + val x4 = { x: Int => 2 * x } try { val y1: List[All] = Serialize.read(Serialize.write(x1)); diff --git a/test/files/pos/bug115.scala b/test/files/pos/bug115.scala index a250e3c090..ad6cb44497 100644 --- a/test/files/pos/bug115.scala +++ b/test/files/pos/bug115.scala @@ -1,9 +1,9 @@ class S[A](f: A => A, x: A) { System.out.println(f(x)); } -class T[B](f: B => B, y: B) extends S(x: B => f(x), y) { +class T[B](f: B => B, y: B) extends S((x: B) => f(x), y) { } object Test extends Application { new T[Int](x => x * 2, 1); - val f = new S(x: Int => x, 1); + val f = new S((x: Int) => x, 1); } diff --git a/test/files/pos/bug628.scala b/test/files/pos/bug628.scala new file mode 100755 index 0000000000..f32c1cad0f --- /dev/null +++ b/test/files/pos/bug628.scala @@ -0,0 +1,17 @@ +object Test { + abstract class Unit + object NoUnit extends Unit + object Hour extends Unit + + case class Measure(scalar: Double, unit: Unit) { + def *(newUnit: Unit): Measure = Measure(scalar, newUnit) + } + + implicit def double2Measure(scalar: Double) = + Measure(scalar, NoUnit) + + + def main(args: Array[String]): scala.Unit = { + Console.println("3.0 * Hour = " + (3.0 * (Hour: Unit))) + } +} diff --git a/test/files/pos/lambda.scala b/test/files/pos/lambda.scala index 187b3f9783..c9094992e8 100644 --- a/test/files/pos/lambda.scala +++ b/test/files/pos/lambda.scala @@ -1,8 +1,9 @@ object test { - def apply[a,b](f: a => b): a => b = x: a => f(x); + def apply[a,b](f: a => b): a => b = { x: a => f(x) } - def twice[a](f: a => a): a => a = x: a => f(f(x)); + def twice[a](f: a => a): a => a = { x: a => f(f(x)) } + + def main = apply[Int,Int](twice[Int]{x: Int => x})(1); +} - def main = apply[Int,Int](twice[Int](x: Int => x))(1); -} \ No newline at end of file diff --git a/test/files/run/Course-2002-05.scala b/test/files/run/Course-2002-05.scala index c761f88f5d..abf9978ac6 100644 --- a/test/files/run/Course-2002-05.scala +++ b/test/files/run/Course-2002-05.scala @@ -21,7 +21,7 @@ object M0 { xs else { val pivot = xs.head; - val sub = partition(xs.tail, (elem : a => less(elem, pivot))); + val sub = partition(xs.tail, { elem : a => less(elem, pivot) }); quicksort(less)(sub._1) ::: List(pivot) ::: quicksort(less)(sub._2) } } @@ -61,7 +61,7 @@ object M1 { xs else { val pivot = xs.head; - val sub = partition(xs.tail, (elem : a => less(elem, pivot))); + val sub = partition(xs.tail, (elem : a) => less(elem, pivot)); quicksort(less)(sub._1) ::: List(pivot) ::: quicksort(less)(sub._2) } } @@ -97,7 +97,7 @@ object M2 { else { val x = s.head; val withoutX = powerset(s.tail); - withoutX ::: withoutX.map(s1 : List[a] => x::s1) + withoutX ::: withoutX.map { s1 : List[a] => x::s1 } } } diff --git a/test/files/run/Course-2002-10.scala b/test/files/run/Course-2002-10.scala index e634309018..d92893772e 100644 --- a/test/files/run/Course-2002-10.scala +++ b/test/files/run/Course-2002-10.scala @@ -25,7 +25,7 @@ object M0 { object M1 { def scale(x: double, s: Stream[double]): Stream[double] = - s map (e: double => e*x); + s map { e: double => e*x } def partialSums(s: Stream[double]): Stream[double] = Stream.cons(s.head, partialSums(s.tail) map (x => x + s.head)); @@ -46,14 +46,14 @@ object M1 { better(s, transform) map (x => x.head); def lnSummands(n: double): Stream[double] = - Stream.cons(1.0 / n, lnSummands(n + 1.0) map (x: double => -x)); + Stream.cons(1.0 / n, lnSummands(n + 1.0) map { x: double => -x }) var ln0 = partialSums(lnSummands(1.0)); var ln1 = euler(ln0); var ln2 = veryGood(ln0, euler); def piSummands(n: double): Stream[double] = - Stream.cons(1.0 / n, piSummands(n + 2.0) map (x: double => -x)); + Stream.cons(1.0 / n, piSummands(n + 2.0) map { x: double => -x }) var pi0 = scale(4.0, partialSums(piSummands(1.0))); var pi1 = euler(pi0); diff --git a/test/files/run/bug216.scala b/test/files/run/bug216.scala index 41b2af7b50..8f67c7b024 100644 --- a/test/files/run/bug216.scala +++ b/test/files/run/bug216.scala @@ -1,6 +1,6 @@ object Test extends Application { object m { - val f = x: unit => (); + val f = { x: unit => () } Console.println("OK") } m; -- cgit v1.2.3