aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorodersky <odersky@gmail.com>2017-02-01 18:21:58 +1100
committerGitHub <noreply@github.com>2017-02-01 18:21:58 +1100
commitaf7fdb32df34b352bf39f01a26653b169e0d55cf (patch)
tree4307381bb799db513dd07a0f40aec968ae99e877 /tests
parentbb2e99cdfa9876561df912d26e9870526de3dd5d (diff)
parent678e8e47b630786df7548c1be5bee744342f826c (diff)
downloaddotty-af7fdb32df34b352bf39f01a26653b169e0d55cf.tar.gz
dotty-af7fdb32df34b352bf39f01a26653b169e0d55cf.tar.bz2
dotty-af7fdb32df34b352bf39f01a26653b169e0d55cf.zip
Merge pull request #1881 from dotty-staging/add-structural-select
Implement structural type member access
Diffstat (limited to 'tests')
-rw-r--r--tests/neg/structural.scala11
-rw-r--r--tests/neg/zoo.scala14
-rw-r--r--tests/pos/i1866.scala5
-rw-r--r--tests/pos/zoo2.scala45
-rw-r--r--tests/run/structural.scala25
-rw-r--r--tests/run/structuralNoSuchMethod.check1
-rw-r--r--tests/run/structuralNoSuchMethod.scala23
7 files changed, 117 insertions, 7 deletions
diff --git a/tests/neg/structural.scala b/tests/neg/structural.scala
new file mode 100644
index 000000000..aab52b2cb
--- /dev/null
+++ b/tests/neg/structural.scala
@@ -0,0 +1,11 @@
+object Test3 {
+ import scala.reflect.Selectable.reflectiveSelectable
+ def g(x: { type T ; def t: T ; def f(a: T): Boolean }) = x.f(x.t) // error: no ClassTag for x.T
+ g(new { type T = Int; def t = 4; def f(a:T) = true })
+ g(new { type T = Any; def t = 4; def f(a:T) = true })
+ val y: { type T = Int; def t = 4; def f(a:T) = true }
+ = new { type T = Int; def t = 4; def f(a:T) = true }
+
+ def h(x: { def f[T](a: T): Int }) = x.f[Int](4) // error: polymorphic refinement method ... no longer allowed
+
+}
diff --git a/tests/neg/zoo.scala b/tests/neg/zoo.scala
index 19efcc1d7..1674548e8 100644
--- a/tests/neg/zoo.scala
+++ b/tests/neg/zoo.scala
@@ -7,19 +7,19 @@ type Grass = {
}
type Animal = {
type Food
- def eats(food: Food): Unit // error
- def gets: Food // error
+ def eats(food: Food): Unit
+ def gets: Food
}
type Cow = {
type IsMeat = Any
type Food <: Grass
- def eats(food: Grass): Unit // error
- def gets: Grass // error
+ def eats(food: Grass): Unit
+ def gets: Grass
}
type Lion = {
type Food = Meat
- def eats(food: Meat): Unit // error
- def gets: Meat // error
+ def eats(food: Meat): Unit
+ def gets: Meat
}
def newMeat: Meat = new {
type IsMeat = Any
@@ -40,5 +40,5 @@ def newLion: Lion = new {
}
val milka = newCow
val leo = newLion
-leo.eats(milka) // structural select not supported
+leo.eats(milka) // error: no projector found
}
diff --git a/tests/pos/i1866.scala b/tests/pos/i1866.scala
new file mode 100644
index 000000000..918d2e182
--- /dev/null
+++ b/tests/pos/i1866.scala
@@ -0,0 +1,5 @@
+import scala.reflect.Selectable.reflectiveSelectable
+object Test {
+ def f(g: { val update: Unit }) = g.update
+ def main(update: Array[String]) = {}
+}
diff --git a/tests/pos/zoo2.scala b/tests/pos/zoo2.scala
new file mode 100644
index 000000000..06210fe67
--- /dev/null
+++ b/tests/pos/zoo2.scala
@@ -0,0 +1,45 @@
+import scala.reflect.Selectable.reflectiveSelectable
+object Test {
+type Meat = {
+ type IsMeat = Any
+}
+type Grass = {
+ type IsGrass = Any
+}
+type Animal = {
+ type Food
+ def eats(food: Food): Unit
+ def gets: Food
+}
+type Cow = {
+ type IsMeat = Any
+ type Food <: Grass
+ def eats(food: Grass): Unit
+ def gets: Grass
+}
+type Lion = {
+ type Food = Meat
+ def eats(food: Meat): Unit
+ def gets: Meat
+}
+def newMeat: Meat = new {
+ type IsMeat = Any
+}
+def newGrass: Grass = new {
+ type IsGrass = Any
+}
+def newCow: Cow = new {
+ type IsMeat = Any
+ type Food = Grass
+ def eats(food: Grass) = ()
+ def gets = newGrass
+}
+def newLion: Lion = new {
+ type Food = Meat
+ def eats(food: Meat) = ()
+ def gets = newMeat
+}
+val milka = newCow
+val leo = newLion
+leo.eats(milka)
+}
diff --git a/tests/run/structural.scala b/tests/run/structural.scala
new file mode 100644
index 000000000..0f18f4579
--- /dev/null
+++ b/tests/run/structural.scala
@@ -0,0 +1,25 @@
+case class Record(elems: (String, Any)*) extends Selectable {
+ def selectDynamic(name: String): Any = elems.find(_._1 == name).get._2
+}
+
+object Test {
+ import scala.reflect.Selectable.reflectiveSelectable
+
+ def f(closeable: { def close(): Unit }) =
+ closeable.close()
+
+ type RN = Record { val name: String; val age: Int }
+
+ def g(r: RN) = r.name
+
+ val rr: RN = Record("name" -> "Bob", "age" -> 42).asInstanceOf[RN]
+
+ def main(args: Array[String]): Unit = {
+ f(new java.io.PrintStream("foo"))
+ assert(g(rr) == "Bob")
+
+ val s: { def concat(s: String): String } = "abc"
+ assert(s.concat("def") == "abcdef")
+ }
+}
+
diff --git a/tests/run/structuralNoSuchMethod.check b/tests/run/structuralNoSuchMethod.check
new file mode 100644
index 000000000..20576fc9c
--- /dev/null
+++ b/tests/run/structuralNoSuchMethod.check
@@ -0,0 +1 @@
+no such method
diff --git a/tests/run/structuralNoSuchMethod.scala b/tests/run/structuralNoSuchMethod.scala
new file mode 100644
index 000000000..476d7ed82
--- /dev/null
+++ b/tests/run/structuralNoSuchMethod.scala
@@ -0,0 +1,23 @@
+import scala.reflect.Selectable.reflectiveSelectable
+
+/** Demonstrates limitation of structural method dispatch (in Scala 2.x and dotty).
+ * The method must be defined at exactly the argument types given in the structural type;
+ * Generic instantiation is not possible.
+ */
+object Test {
+ type T = { def f(x: String, y: String): String }
+
+ class C[X] {
+ def f(x: X, y: String): String = "f1"
+ }
+
+ val x: T = new C[String]
+
+ def main(args: Array[String]) =
+ try println(x.f("", "")) // throws NoSuchMethodException
+ catch {
+ case ex: NoSuchMethodException =>
+ println("no such method")
+ }
+
+}