diff options
author | Eugene Burmako <xeno.by@gmail.com> | 2013-10-02 17:21:55 +0200 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2013-11-12 18:40:01 -0800 |
commit | 6038bac3513a834e67ab4074c2c7b03aac11b1b3 (patch) | |
tree | 61a5fb1019fd24e69c8fca420b710f1235b27b50 /test/files/neg | |
parent | a2b523a39b4e56eb9ab5d9a5639f5b59d425e354 (diff) | |
download | scala-6038bac3513a834e67ab4074c2c7b03aac11b1b3.tar.gz scala-6038bac3513a834e67ab4074c2c7b03aac11b1b3.tar.bz2 scala-6038bac3513a834e67ab4074c2c7b03aac11b1b3.zip |
blackbox restriction #2: can't guide type inference
When an application of a blackbox macro still has undetermined type
parameters after Scala’s type inference algorithm has finished working,
these type parameters are inferred forcedly, in exactly the same manner
as type inference happens for normal methods.
This makes it impossible for blackbox macros to influence type inference,
prohibiting fundep materialization.
Diffstat (limited to 'test/files/neg')
4 files changed, 64 insertions, 0 deletions
diff --git a/test/files/neg/macro-blackbox-fundep-materialization.check b/test/files/neg/macro-blackbox-fundep-materialization.check new file mode 100644 index 0000000000..a5a9b9a206 --- /dev/null +++ b/test/files/neg/macro-blackbox-fundep-materialization.check @@ -0,0 +1,12 @@ +Test_2.scala:7: Iso.materializeIso is not a valid implicit value for Iso[Test.Foo,L] because: +hasMatchingSymbol reported error: type mismatch; + found : Iso[Test.Foo,(Int, String, Boolean)] + required: Iso[Test.Foo,Nothing] +Note: (Int, String, Boolean) >: Nothing, but trait Iso is invariant in type U. +You may wish to define U as -U instead. (SLS 4.5) + val equiv = foo(Foo(23, "foo", true)) + ^ +Test_2.scala:7: error: could not find implicit value for parameter iso: Iso[Test.Foo,L] + val equiv = foo(Foo(23, "foo", true)) + ^ +one error found diff --git a/test/files/neg/macro-blackbox-fundep-materialization.flags b/test/files/neg/macro-blackbox-fundep-materialization.flags new file mode 100644 index 0000000000..4c6cdb71e2 --- /dev/null +++ b/test/files/neg/macro-blackbox-fundep-materialization.flags @@ -0,0 +1 @@ +-Xlog-implicits
\ No newline at end of file diff --git a/test/files/neg/macro-blackbox-fundep-materialization/Macros_1.scala b/test/files/neg/macro-blackbox-fundep-materialization/Macros_1.scala new file mode 100644 index 0000000000..6bddef4b9d --- /dev/null +++ b/test/files/neg/macro-blackbox-fundep-materialization/Macros_1.scala @@ -0,0 +1,39 @@ +import scala.language.experimental.macros +import scala.reflect.macros.BlackboxContext + +trait Iso[T, U] { + def to(t : T) : U + // def from(u : U) : T +} + +object Iso { + implicit def materializeIso[T, U]: Iso[T, U] = macro impl[T, U] + def impl[T: c.WeakTypeTag, U: c.WeakTypeTag](c: BlackboxContext): c.Expr[Iso[T, U]] = { + import c.universe._ + import definitions._ + import Flag._ + + val sym = c.weakTypeOf[T].typeSymbol + if (!sym.isClass || !sym.asClass.isCaseClass) c.abort(c.enclosingPosition, s"$sym is not a case class") + val fields = sym.typeSignature.declarations.toList.collect{ case x: TermSymbol if x.isVal && x.isCaseAccessor => x } + + def mkTpt() = { + val core = Ident(TupleClass(fields.length) orElse UnitClass) + if (fields.length == 0) core + else AppliedTypeTree(core, fields map (f => TypeTree(f.typeSignature))) + } + + def mkFrom() = { + if (fields.length == 0) Literal(Constant(Unit)) + else Apply(Ident(newTermName("Tuple" + fields.length)), fields map (f => Select(Ident(newTermName("f")), newTermName(f.name.toString.trim)))) + } + + val evidenceClass = ClassDef(Modifiers(FINAL), newTypeName("$anon"), List(), Template( + List(AppliedTypeTree(Ident(newTypeName("Iso")), List(Ident(sym), mkTpt()))), + emptyValDef, + List( + DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))), + DefDef(Modifiers(), newTermName("to"), List(), List(List(ValDef(Modifiers(PARAM), newTermName("f"), Ident(sym), EmptyTree))), TypeTree(), mkFrom())))) + c.Expr[Iso[T, U]](Block(List(evidenceClass), Apply(Select(New(Ident(newTypeName("$anon"))), nme.CONSTRUCTOR), List()))) + } +} diff --git a/test/files/neg/macro-blackbox-fundep-materialization/Test_2.scala b/test/files/neg/macro-blackbox-fundep-materialization/Test_2.scala new file mode 100644 index 0000000000..a00f4ed7db --- /dev/null +++ b/test/files/neg/macro-blackbox-fundep-materialization/Test_2.scala @@ -0,0 +1,12 @@ +// see the comments for macroExpandApply.onDelayed for an explanation of what's tested here +object Test extends App { + case class Foo(i: Int, s: String, b: Boolean) + def foo[C, L](c: C)(implicit iso: Iso[C, L]): L = iso.to(c) + + { + val equiv = foo(Foo(23, "foo", true)) + def typed[T](t: => T) {} + typed[(Int, String, Boolean)](equiv) + println(equiv) + } +}
\ No newline at end of file |