Skip to content

Commit 8b9e6d6

Browse files
committed
Add interweaving tests
1 parent e0f115a commit 8b9e6d6

15 files changed

+162
-0
lines changed

tests/neg/interweaving-ab.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object Ab:
2+
given String = ""
3+
given Double = 0
4+
5+
def ab[A][B](x: A)(using B): B = summon[B]
6+
7+
def test =
8+
ab[Int](0: Int) // error
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object nameCollision:
2+
def f[T](x: T)[U](y: U) = (x,y)
3+
def f[T](x: T, y: T) = (x,y) // error

tests/neg/interweaving-params.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Params{
2+
def bar[T](x: T)[T]: String = ??? // error
3+
def zoo(x: Int)[T, U](x: U): T = ??? // error
4+
def bbb[T <: U](x: U)[U]: U = ??? // error // error
5+
def f0[T](implicit x: T)[U](y: U) = (x,y) // error
6+
def f1[T](implicit x: T)[U] = (x,y) // error
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
object typeApply:
2+
3+
def f3[T <: Int][U <: String](): T => T = ???
4+
def f5[T <: Int][U <: String]: [X <: Unit] => X => X = ???
5+
def f7[T <: Int][U <: String]()[X <: Unit]: X => X = ???
6+
7+
@main def test = {
8+
f3[String]() // error
9+
f5[Int][Unit] // error
10+
f5[String][Unit] // error // error
11+
f7[String]()[Unit] // error
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object unmatched:
2+
def f1[T (x: T)] = ??? // error
3+
def f2(x: Any[)T] = ??? // error // error
4+
def f3[T(x: T) = ??? // error

tests/pos/interweaving-ba.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
3+
object BA {
4+
given String = ""
5+
given Double = 0
6+
7+
def ba[B][A](x: A)(using B): B = summon[B]
8+
9+
def test = ba[String](0)
10+
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
object chainedParams{
2+
3+
trait Chain{
4+
type Tail <: Chain
5+
}
6+
7+
def f[C1 <: Chain](c1: C1)[C2 <: c1.Tail](c2: C2)[C3 <: c2.Tail](c3: C3): c3.Tail = ???
8+
9+
val self = new Chain{ type Tail = this.type }
10+
val res: self.type = f(self)(self)(self)
11+
12+
type C <: Chain
13+
14+
val c3 = new Chain{ type Tail = C }
15+
val c2 = new Chain{ type Tail = c3.type }
16+
val c1 = new Chain{ type Tail = c2.type }
17+
val u: C = f(c1)(c2)(c3)
18+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def f1[T][U](x: T, y: U): (T, U) = (x, y)
2+
def f2[T](x: T)[U](y: U): (T, U) = (x, y)
3+
def f3[T, U][V](x: T): U = ???
4+
def f4[T](x: T)[U <: x.type](y: U): (T, U) = (x, y)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
object functorCurrying:
2+
//taken from https://dotty.epfl.ch/docs/reference/contextual/type-classes.html
3+
//at version 3.1.1-RC1-bin-20210930-01f040b-NIGHTLY
4+
//modified to have type currying
5+
trait Functor[F[_]]:
6+
def map[A][B](x: F[A], f: A => B): F[B]
7+
8+
9+
given Functor[List] with
10+
def map[A][B](x: List[A], f: A => B): List[B] =
11+
x.map(f)
12+
13+
def assertTransformation[F[_]: Functor][A][B](expected: F[B], original: F[A], mapping: A => B): Unit =
14+
assert(expected == summon[Functor[F]].map(original, mapping))
15+
16+
@main def testCurrying =
17+
assertTransformation(List("a1", "b1"), List("a", "b"), elt => s"${elt}1")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
object functorInterweaving:
2+
//taken from https://dotty.epfl.ch/docs/reference/contextual/type-classes.html
3+
//at version 3.1.1-RC1-bin-20210930-01f040b-NIGHTLY
4+
//modified to have type interveawing
5+
trait Functor[F[_]]:
6+
def map[A](x: F[A])[B](f: A => B): F[B]
7+
8+
9+
given Functor[List] with
10+
def map[A](x: List[A])[B](f: A => B): List[B] =
11+
x.map(f)
12+
13+
def assertTransformation[F[_]: Functor][A](original: F[A])[B](expected: F[B])(mapping: A => B): Unit =
14+
assert(expected == summon[Functor[F]].map(original)(mapping))
15+
16+
@main def testInterweaving =
17+
assertTransformation(List("a", "b"))(List("a1", "b1")){elt => s"${elt}1"}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import scala.annotation.targetName
2+
3+
object nameCollision:
4+
def f[T](x: T)[U](y: U) = (x,y)
5+
@targetName("g") def f[T](x: T, y: T) = (x,y)

tests/pos/interweaving-newline.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object newline {
2+
def multipleLines
3+
[T]
4+
(x: T)
5+
[U]
6+
(using (T,U))
7+
(y: U)
8+
= ???
9+
}

tests/pos/interweaving-overload.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
class A{
3+
4+
def f1[T][U](x: Any) = ???
5+
def f1[T][U](x: Int) = ???
6+
7+
f1(1)
8+
f1("hello")
9+
10+
case class B[U](x: Int)
11+
def b[U](x: Int) = B[U](x)
12+
13+
def f2[T]: [U] => Int => B[U] = [U] => (x: Int) => b[U](x)
14+
15+
f2(1)
16+
f2[Any](1)
17+
f2[Any][Any](1)
18+
19+
}

tests/pos/interweaving-params.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Params{
2+
type U
3+
def foo[T](x: T)[U >: x.type <: T][L <: List[U]](l: L): L = ???
4+
def aaa(x: U): U = ???
5+
def bbb[T <: U](x: U)[U]: U = ???
6+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
object typeApply:
2+
3+
def f0[T]: [U] => T => T = ???
4+
def f1[T][U]: T => T = ???
5+
def f2[T][U](): T => T = ???
6+
def f3[T <: Int][U <: String](): T => T = ???
7+
def f4[T <: Int][U <: String]: T => T = ???
8+
def f5[T <: Int][U <: String]: [X <: Unit] => X => X = ???
9+
def f6[T <: Int][U <: String](): [X <: Unit] => X => X = ???
10+
def f7[T <: Int][U <: String]()[X <: Unit]: X => X = ???
11+
12+
@main def test = {
13+
f0[Int][String]
14+
f1[Int][String]
15+
f2[Int][String]()
16+
f3[Int][String]()
17+
f4[Int][String]
18+
f5[Int][String]
19+
f5[Int][String][Unit]
20+
f6[Int]()[Unit]
21+
f7[Int]()[Unit]
22+
}

0 commit comments

Comments
 (0)