summaryrefslogtreecommitdiff
path: root/test/files/run/sammy_erasure_cce.scala
diff options
context:
space:
mode:
authorAdriaan Moors <adriaan.moors@typesafe.com>2016-03-21 21:54:48 -0700
committerAdriaan Moors <adriaan.moors@typesafe.com>2016-03-26 22:55:02 -0700
commit878e20a5243383300d3b4990146d260409bf5dfd (patch)
treed7ed82ebf6d317ae901cc5e2317ac6029970a5da /test/files/run/sammy_erasure_cce.scala
parent391e2843f420bb4686b974b18ac361c9bb49465c (diff)
downloadscala-878e20a5243383300d3b4990146d260409bf5dfd.tar.gz
scala-878e20a5243383300d3b4990146d260409bf5dfd.tar.bz2
scala-878e20a5243383300d3b4990146d260409bf5dfd.zip
Track Function's SAM symbol & target type using an attachment
We cannot use the expected type to track whether a Function node targets a SAM type, as the expected type may be erased (see test for an example). Thus, the type checker attaches a SAMFunction attachment to a Function node when SAM conversion is performed in adapt. Ideally, we'd move to Dotty's Closure AST, but that will need a deprecation cycle. Thanks to Jason for catching my mistake, suggesting the fix and providing the test. Both the sam method symbol and sam target type must be tracked, as their relationship can be complicated (due to inheritance). For example, the sam method could be defined in a superclass (T) of the Function's target type (U). ``` trait T { def foo(a: Any): Any } trait U extends T { def apply = ??? } (((x: Any) => x) : U).foo("") ``` This removes some of the duplication in deriving the sam method from the expected type, but some grossness (see TODO) remains.
Diffstat (limited to 'test/files/run/sammy_erasure_cce.scala')
-rw-r--r--test/files/run/sammy_erasure_cce.scala22
1 files changed, 22 insertions, 0 deletions
diff --git a/test/files/run/sammy_erasure_cce.scala b/test/files/run/sammy_erasure_cce.scala
new file mode 100644
index 0000000000..fb973befe4
--- /dev/null
+++ b/test/files/run/sammy_erasure_cce.scala
@@ -0,0 +1,22 @@
+trait F1 {
+ def apply(a: List[String]): String
+ def f1 = "f1"
+}
+
+object Test extends App {
+ // Wrap the sam-targeting function in a context where the expected type is erased (identity's argument type erases to Object),
+ // so that Erasure can't tell that the types actually conform by looking only
+ // at an un-adorned Function tree and the expected type
+ // (because a function type needs no cast it the expected type is a SAM type),
+ //
+ // A correct implementation of Typers/Erasure tracks a Function's SAM target type directly
+ // (currently using an attachment for backwards compat),
+ // and not in the expected type (which was the case in my first attempt),
+ // as the expected type may lose its SAM status due to erasure.
+ // (In a sense, this need not be so, but erasure drops type parameters,
+ // so that identity's F1 type argument cannot be propagated to its argument type.)
+ def foo = identity[F1]((as: List[String]) => as.head)
+
+ // check that this doesn't CCE's
+ foo.f1
+}