diff options
Diffstat (limited to 'tests')
24 files changed, 295 insertions, 5 deletions
diff --git a/tests/neg/i2142.scala b/tests/neg/i2142.scala new file mode 100644 index 000000000..7aeef95f0 --- /dev/null +++ b/tests/neg/i2142.scala @@ -0,0 +1,28 @@ +object Foo { + +class A +val a1 = new A() +val a2 = new A() + +def f(x: A, y: x.type) = () +f(a1, a1) // ok +f(a1, a2) // error +f(new A(), new A()) // error +f(new A(), a1) // error + +def g(x: A)(y: x.type) = () +g(a1)(a1) // ok +g(a1)(a2) // error +g(new A())(new A()) // error +g(new A())(a1) // error + +val x0 = g(new A()) _ +x0 (new A()) // error + +class C[T] + +def h(x: A): C[x.type] = ??? +val x = h(a1) +val y = h(new A()) + +} diff --git a/tests/patmat/enum-HList.scala b/tests/patmat/enum-HList.scala new file mode 100644 index 000000000..c019cb6cc --- /dev/null +++ b/tests/patmat/enum-HList.scala @@ -0,0 +1,22 @@ +enum HLst { + case HCons[+Hd, +Tl <: HLst](hd: Hd, tl: Tl) + case HNil +} + +object Test { + import HLst._ + def length(hl: HLst): Int = hl match { + case HCons(_, tl) => 1 + length(tl) + case HNil => 0 + } + def sumInts(hl: HLst): Int = hl match { + case HCons(x: Int, tl) => x + sumInts(tl) + case HCons(_, tl) => sumInts(tl) + case HNil => 0 + } + def main(args: Array[String]) = { + val hl = HCons(1, HCons("A", HNil)) + assert(length(hl) == 2, length(hl)) + assert(sumInts(hl) == 1) + } +} diff --git a/tests/patmat/enum-Tree.scala b/tests/patmat/enum-Tree.scala new file mode 100644 index 000000000..ef5bd7a57 --- /dev/null +++ b/tests/patmat/enum-Tree.scala @@ -0,0 +1,29 @@ +enum Tree[T] { + case True extends Tree[Boolean] + case False extends Tree[Boolean] + case Zero extends Tree[Int] + case Succ(n: Tree[Int]) extends Tree[Int] + case Pred(n: Tree[Int]) extends Tree[Int] + case IsZero(n: Tree[Int]) extends Tree[Boolean] + case If(cond: Tree[Boolean], thenp: Tree[T], elsep: Tree[T]) +} + +object Test { + import Tree._ + + def eval[T](e: Tree[T]): T = e match { + case True => true + case False => false + case Zero => 0 + case Succ(f) => eval(f) + 1 + case Pred(f) => eval(f) - 1 + case IsZero(f) => eval(f) == 0 + case If(cond, thenp, elsep) => if (eval(cond)) eval(thenp) else eval(elsep) + } + + val data = If(IsZero(Pred(Succ(Zero))), Succ(Succ(Zero)), Pred(Pred(Zero))) + + def main(args: Array[String]) = { + println(s"$data --> ${eval(data)}") + } +} diff --git a/tests/patmat/enumColor.scala b/tests/patmat/enumColor.scala new file mode 100644 index 000000000..60d610d0d --- /dev/null +++ b/tests/patmat/enumColor.scala @@ -0,0 +1,12 @@ + enum Color { + case Red, Green, Blue + } + + object Test { + def f(color: Color) = { + import Color._ + color match { + case Red | Green | Blue => + } + } +} diff --git a/tests/patmat/patmat-indent.check b/tests/patmat/patmat-indent.check index 79845ebcf..4f0ec4dd9 100644 --- a/tests/patmat/patmat-indent.check +++ b/tests/patmat/patmat-indent.check @@ -1,3 +1,3 @@ 9: Pattern Match Exhaustivity: Nil -23: Pattern Match Exhaustivity: _: Boolean +23: Pattern Match Exhaustivity: true, false 27: Pattern Match Exhaustivity: _: Int diff --git a/tests/patmat/patmat-indent.scala b/tests/patmat/patmat-indent.scala index ef25bb2c7..a2b18e7fb 100644 --- a/tests/patmat/patmat-indent.scala +++ b/tests/patmat/patmat-indent.scala @@ -1,5 +1,5 @@ object Test { - val Nil = scala.Nil + val Nil: scala.collection.immutable.Nil.type = scala.collection.immutable.Nil val X = 5 object Inner { diff --git a/tests/patmat/planets.scala b/tests/patmat/planets.scala new file mode 100644 index 000000000..bcbfd7eeb --- /dev/null +++ b/tests/patmat/planets.scala @@ -0,0 +1,26 @@ +enum class Planet(mass: Double, radius: Double) { + private final val G = 6.67300E-11 + def surfaceGravity = G * mass / (radius * radius) + def surfaceWeight(otherMass: Double) = otherMass * surfaceGravity +} +object Planet { + case MERCURY extends Planet(3.303e+23, 2.4397e6) + case VENUS extends Planet(4.869e+24, 6.0518e6) + case EARTH extends Planet(5.976e+24, 6.37814e6) + case MARS extends Planet(6.421e+23, 3.3972e6) + case JUPITER extends Planet(1.9e+27, 7.1492e7) + case SATURN extends Planet(5.688e+26, 6.0268e7) + case URANUS extends Planet(8.686e+25, 2.5559e7) + case NEPTUNE extends Planet(1.024e+26, 2.4746e7) +} +object Test { + def main(args: Array[String]) = { + import Planet._ + assert(enumValueNamed("SATURN") == SATURN) + assert(enumValue(2) == EARTH) + val earthWeight = 100 + val mass = earthWeight/EARTH.surfaceGravity + for (p <- enumValues) + println(s"Your weight on $p is ${p.surfaceWeight(mass)}") + } +} diff --git a/tests/patmat/t6420.check b/tests/patmat/t6420.check index 73acf1454..c15701594 100644 --- a/tests/patmat/t6420.check +++ b/tests/patmat/t6420.check @@ -1 +1 @@ -5: Pattern Match Exhaustivity: (Nil, _), (List(_, _), _), (Nil, Nil), (Nil, List(_, _)), (List(_, _), Nil), (List(_, _), List(_, _)), (_, Nil), (_, List(_, _)) +5: Pattern Match Exhaustivity: (Nil, _), (List(true, _), _), (List(false, _), _), (_, Nil), (_, List(true, _)), (_, List(false, _)) diff --git a/tests/patmat/t7285.check b/tests/patmat/t7285.check index 1c2841920..d40b77e4b 100644 --- a/tests/patmat/t7285.check +++ b/tests/patmat/t7285.check @@ -1,3 +1,3 @@ 15: Pattern Match Exhaustivity: (Up, Down) 33: Pattern Match Exhaustivity: Down -51: Pattern Match Exhaustivity: (Base.Up, Base.Down) +51: Pattern Match Exhaustivity: (Up, Down) diff --git a/tests/patmat/t7466.check b/tests/patmat/t7466.check index 35227484e..af596399b 100644 --- a/tests/patmat/t7466.check +++ b/tests/patmat/t7466.check @@ -1 +1 @@ -8: Pattern Match Exhaustivity: (_, _) +8: Pattern Match Exhaustivity: (true, _), (false, _), (_, true), (_, false) diff --git a/tests/pos/i2188.scala b/tests/pos/i2188.scala new file mode 100644 index 000000000..4129977ac --- /dev/null +++ b/tests/pos/i2188.scala @@ -0,0 +1,5 @@ +class Fill(elem: => Int) { + class Iter { + def next(): Int = elem + } +} diff --git a/tests/pos/i2198.scala b/tests/pos/i2198.scala new file mode 100644 index 000000000..62ae7e8b5 --- /dev/null +++ b/tests/pos/i2198.scala @@ -0,0 +1,6 @@ +object Test { + val nil = scala.collection.immutable.Nil + def f(x: nil.type): Int = 3 + + f(scala.collection.immutable.Nil) +} diff --git a/tests/pos/i2200/Hello.scala b/tests/pos/i2200/Hello.scala new file mode 100644 index 000000000..47e8b2024 --- /dev/null +++ b/tests/pos/i2200/Hello.scala @@ -0,0 +1,6 @@ +package bar +import scala.language.higherKinds +class Fix[F[_]](unfix: F[Fix[F]]) +object DocTree { + def docTree(s: StreamTree[DocTree]): DocTree = new Fix(s: StreamTree[DocTree]) +} diff --git a/tests/pos/i2200/package.scala b/tests/pos/i2200/package.scala new file mode 100644 index 000000000..3bc519b72 --- /dev/null +++ b/tests/pos/i2200/package.scala @@ -0,0 +1,4 @@ +package object bar { + type StreamTree[T] = Stream[Int] + type DocTree = Fix[StreamTree] +} diff --git a/tests/pos/i2218.scala b/tests/pos/i2218.scala new file mode 100644 index 000000000..fbede8b9b --- /dev/null +++ b/tests/pos/i2218.scala @@ -0,0 +1,9 @@ +trait Rule[In] + +class C { + def ruleWithName[In](f: In => Int): Rule[In] = { + new DefaultRule(f) {} + } + + class DefaultRule[In](f: In => Int) extends Rule[In] +} diff --git a/tests/pos/i2219.scala b/tests/pos/i2219.scala new file mode 100644 index 000000000..7f786eb3e --- /dev/null +++ b/tests/pos/i2219.scala @@ -0,0 +1,7 @@ +object Test { + type Inv[T[_]] = T[_] + + class Hi[T[_]](x: Inv[T]) { + def foo[T[_]](value: Inv[T] = x) = {} + } +} diff --git a/tests/pending/pos/t7296.scala b/tests/pos/t7296.scala index fcba17c08..fcba17c08 100644 --- a/tests/pending/pos/t7296.scala +++ b/tests/pos/t7296.scala diff --git a/tests/run/1938.scala b/tests/run/1938.scala new file mode 100644 index 000000000..95e94678d --- /dev/null +++ b/tests/run/1938.scala @@ -0,0 +1,45 @@ +case class Large( + e1: Int, + e2: Int, + e3: Int, + e4: Int, + e5: Int, + e6: Int, + e7: Int, + e8: Int, + e9: Int, + e10: Int, + e11: Int, + e12: Int, + e13: Int, + e14: Int, + e15: Int, + e16: Int, + e17: Int, + e18: Int, + e19: Int, + e20: Int, + e21: Int, + e22: Int, + e23: Int +) + +object Test { + def main(args: Array[String]): Unit = { + val l = Large(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23) + + assert(l.productArity == 23) + + assert(l.productElement(0) == 1) + assert(l.productElement(1) == 2) + assert(l.productElement(21) == 22) + assert(l.productElement(22) == 23) + + try { + l.productElement(23) + ??? + } catch { + case e: IndexOutOfBoundsException => assert(e.getMessage == "23") + } + } +} diff --git a/tests/pending/run/case-class-23.check b/tests/run/case-class-23.check index 888ed2c9e..888ed2c9e 100644 --- a/tests/pending/run/case-class-23.check +++ b/tests/run/case-class-23.check diff --git a/tests/pending/run/case-class-23.scala b/tests/run/case-class-23.scala index a6d78763c..a6d78763c 100644 --- a/tests/pending/run/case-class-23.scala +++ b/tests/run/case-class-23.scala diff --git a/tests/run/double-pattern-type.scala b/tests/run/double-pattern-type.scala new file mode 100644 index 000000000..8045d173b --- /dev/null +++ b/tests/run/double-pattern-type.scala @@ -0,0 +1,40 @@ +case class C1(i: String, s: Int) { def isEmpty = false; def get = ("EMPTY", -1) } +case class C2(i: String, s: String) { def isEmpty = false; def get = (-1, -2, -3) } + +object Test { + def main(args: Array[String]): Unit = { + // When both Product and name based patterns with same arity are available, + // we follow scalac and silently use the Product one: + + val c1 = C1("s", 0) + c1 match { + case C1(a, b) => + assert(a == "s") + assert(b == 0) + } + + // When the size differ, both are patterns become usable: + + val c2 = C2("a", "b") + c2 match { + case C2(a, b) => + assert(a == "a") + assert(b == "b") + } + + c2 match { + case C2(a, b, c) => + assert(a == -1) + assert(b == -2) + assert(c == -3) + } + + // Interestingly things also compile with a single pattern, in which case + // the tuple returned by get is binded to `a`: + + c2 match { + case C2(a) => + assert(a == (-1, -2, -3)) + } + } +} diff --git a/tests/run/enum-Color.scala b/tests/run/enum-Color.scala index 683d18d9e..f4f6aaef8 100644 --- a/tests/run/enum-Color.scala +++ b/tests/run/enum-Color.scala @@ -7,5 +7,9 @@ object Test { for (color <- Color.enumValues) { println(s"$color: ${color.enumTag}") assert(Color.enumValue(color.enumTag) eq color) + import Color._ + color match { + case Red | Green | Blue => + } } } diff --git a/tests/run/i2163.scala b/tests/run/i2163.scala new file mode 100644 index 000000000..952f651e3 --- /dev/null +++ b/tests/run/i2163.scala @@ -0,0 +1,21 @@ +class Base(f: Int => Int) { + def result = f(3) +} + +class Child(x: Int) extends Base(y => x + y) + +class Outer(z: Int) { + class Base(f: Int => Int) { + def result = f(3) + } + + class Child(x: Int) extends Base(y => x + y + z) +} + +object Test { + def main(args: Array[String]): Unit = { + assert(new Child(4).result == 7) + val o = new Outer(2) + assert(new o.Child(2).result == 7) + } +} diff --git a/tests/run/zero-arity-case-class.scala b/tests/run/zero-arity-case-class.scala new file mode 100644 index 000000000..de0ba4fe1 --- /dev/null +++ b/tests/run/zero-arity-case-class.scala @@ -0,0 +1,26 @@ +case class Foo() + +object Test { + def main(args: Array[String]): Unit = { + assert(Foo.unapply(Foo()) == true) + + // unapply generate by scalac are `_ != null`, + // dotty returns true in all cases + assert(Foo.unapply(null) == true) + + Foo() match { + case Foo() => () + case _ => ??? + } + + Foo() match { + case _: Foo => () + case _ => ??? + } + + (Foo(): Any) match { + case Foo() => () + case _ => ??? + } + } +} |