Skip to content

Commit 0c5dd9e

Browse files
committed
[backport] SI-7470 implements fundep materialization
Backports 21a8c6c from the 2.11.x branch under -Xfundep-materialization as per Miles Sabin's request. Thanks Miles!
1 parent 300db2a commit 0c5dd9e

File tree

19 files changed

+214
-4
lines changed

19 files changed

+214
-4
lines changed

src/compiler/scala/tools/nsc/settings/ScalaSettings.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ trait ScalaSettings extends AbsScalaSettings
111111
val XnoPatmatAnalysis = BooleanSetting ("-Xno-patmat-analysis", "Don't perform exhaustivity/unreachability analysis. Also, ignore @switch annotation.")
112112
val XfullLubs = BooleanSetting ("-Xfull-lubs", "Retains pre 2.10 behavior of less aggressive truncation of least upper bounds.")
113113
val Xdivergence211 = BooleanSetting ("-Xdivergence211", "Turn on the 2.11 behavior of implicit divergence not terminating recursive implicit searches (SI-7291).")
114+
val XfundepMaterialization = BooleanSetting("-Xfundep-materialization", "Turn on the 2.11 behavior of macro expansion being able to influence type inference in implicit searches")
114115

115116
/** Compatibility stubs for options whose value name did
116117
* not previously match the option name.

src/compiler/scala/tools/nsc/typechecker/Macros.scala

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,13 @@ trait Macros extends scala.tools.reflect.FastTrack with Traces {
713713

714714
var expectedTpe = expandee.tpe
715715
if (isNullaryInvocation(expandee)) expectedTpe = expectedTpe.finalResultType
716+
if (settings.XfundepMaterialization.value) {
717+
// approximation is necessary for whitebox macros to guide type inference
718+
// read more in the comments for onDelayed below
719+
val undetparams = expectedTpe collect { case tp if tp.typeSymbol.isTypeParameter => tp.typeSymbol }
720+
expectedTpe = deriveTypeWithWildcards(undetparams)(expectedTpe)
721+
}
722+
716723
// also see http://groups.google.com/group/scala-internals/browse_thread/thread/492560d941b315cc
717724
val expanded0 = duplicateAndKeepPositions(expanded)
718725
val expanded1 = typecheck("macro def return type", expanded0, expectedTpe)
@@ -766,9 +773,24 @@ trait Macros extends scala.tools.reflect.FastTrack with Traces {
766773
// (in a sense that a datatype's uniform representation is unambiguously determined by the datatype,
767774
// e.g. for Foo it will be Int :: String :: Boolean :: HNil), there's no way to convey this information
768775
// to the typechecker. Therefore the typechecker will infer Nothing for L, which is hardly what we want.
776+
//
777+
// =========== THE SOLUTION ===========
778+
//
779+
// To give materializers a chance to say their word before vanilla inference kicks in,
780+
// we infer as much as possible (e.g. in the example above even though L is hopeless, C still can be inferred to Foo)
781+
// and then trigger macro expansion with the undetermined type parameters still there.
782+
// Thanks to that the materializer can take a look at what's going on and react accordingly.
783+
//
784+
// NOTE: This functionality is only available under the -Xfundep-materialization flag in Scala 2.10,
785+
// but is enabled by default in Scala 2.11.
769786
val shouldInstantiate = typer.context.undetparams.nonEmpty && !inPolyMode(mode)
770-
if (shouldInstantiate) typer.instantiatePossiblyExpectingUnit(delayed, mode, pt)
771-
else delayed
787+
if (shouldInstantiate) {
788+
if (settings.XfundepMaterialization.value) {
789+
forced += delayed
790+
typer.infer.inferExprInstance(delayed, typer.context.extractUndetparams(), pt, keepNothings = false)
791+
macroExpand(typer, delayed, mode, pt)
792+
} else typer.instantiatePossiblyExpectingUnit(delayed, mode, pt)
793+
} else delayed
772794
case Fallback(fallback) =>
773795
typer.context.withImplicitsEnabled(typer.typed(fallback, EXPRmode, pt))
774796
case Other(result) =>
@@ -886,10 +908,12 @@ trait Macros extends scala.tools.reflect.FastTrack with Traces {
886908
* 2) undetparams (sym.isTypeParameter && !sym.isSkolem)
887909
*/
888910
var hasPendingMacroExpansions = false
911+
private val forced = perRunCaches.newWeakSet[Tree]
889912
private val delayed = perRunCaches.newWeakMap[Tree, scala.collection.mutable.Set[Int]]
890913
private def isDelayed(expandee: Tree) = delayed contains expandee
891914
private def calculateUndetparams(expandee: Tree): scala.collection.mutable.Set[Int] =
892-
delayed.get(expandee).getOrElse {
915+
if (forced(expandee)) scala.collection.mutable.Set[Int]()
916+
else delayed.getOrElse(expandee, {
893917
val calculated = scala.collection.mutable.Set[Symbol]()
894918
expandee foreach (sub => {
895919
def traverse(sym: Symbol) = if (sym != null && (undetparams contains sym.id)) calculated += sym
@@ -898,7 +922,7 @@ trait Macros extends scala.tools.reflect.FastTrack with Traces {
898922
})
899923
macroLogVerbose("calculateUndetparams: %s".format(calculated))
900924
calculated map (_.id)
901-
}
925+
})
902926
private val undetparams = perRunCaches.newSet[Int]
903927
def notifyUndetparamsAdded(newUndets: List[Symbol]): Unit = {
904928
undetparams ++= newUndets map (_.id)

test/files/neg/t5923c.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Test_2.scala:7: error: could not find implicit value for parameter iso: Iso[Test.Foo,L]
2+
val equiv = foo(Foo(23, "foo", true))
3+
^
4+
one error found
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import language.experimental.macros
2+
import scala.reflect.macros.Context
3+
4+
trait Iso[T, U] {
5+
def to(t : T) : U
6+
// def from(u : U) : T
7+
}
8+
9+
object Iso {
10+
implicit def materializeIso[T, U]: Iso[T, U] = macro impl[T, U]
11+
def impl[T: c.WeakTypeTag, U: c.WeakTypeTag](c: Context): c.Expr[Iso[T, U]] = {
12+
import c.universe._
13+
import definitions._
14+
import Flag._
15+
16+
val sym = c.weakTypeOf[T].typeSymbol
17+
if (!sym.isClass || !sym.asClass.isCaseClass) c.abort(c.enclosingPosition, s"$sym is not a case class")
18+
val fields = sym.typeSignature.declarations.toList.collect{ case x: TermSymbol if x.isVal && x.isCaseAccessor => x }
19+
20+
def mkTpt() = {
21+
val core = Ident(TupleClass(fields.length) orElse UnitClass)
22+
if (fields.length == 0) core
23+
else AppliedTypeTree(core, fields map (f => TypeTree(f.typeSignature)))
24+
}
25+
26+
def mkFrom() = {
27+
if (fields.length == 0) Literal(Constant(Unit))
28+
else Apply(Ident(newTermName("Tuple" + fields.length)), fields map (f => Select(Ident(newTermName("f")), newTermName(f.name.toString.trim))))
29+
}
30+
31+
val evidenceClass = ClassDef(Modifiers(FINAL), newTypeName("$anon"), List(), Template(
32+
List(AppliedTypeTree(Ident(newTypeName("Iso")), List(Ident(sym), mkTpt()))),
33+
emptyValDef,
34+
List(
35+
DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))),
36+
DefDef(Modifiers(), newTermName("to"), List(), List(List(ValDef(Modifiers(PARAM), newTermName("f"), Ident(sym), EmptyTree))), TypeTree(), mkFrom()))))
37+
c.Expr[Iso[T, U]](Block(List(evidenceClass), Apply(Select(New(Ident(newTypeName("$anon"))), nme.CONSTRUCTOR), List())))
38+
}
39+
}

test/files/neg/t5923c/Test_2.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// see the comments for macroExpandApply.onDelayed for an explanation of what's tested here
2+
object Test extends App {
3+
case class Foo(i: Int, s: String, b: Boolean)
4+
def foo[C, L](c: C)(implicit iso: Iso[C, L]): L = iso.to(c)
5+
6+
{
7+
val equiv = foo(Foo(23, "foo", true))
8+
def typed[T](t: => T) {}
9+
typed[(Int, String, Boolean)](equiv)
10+
println(equiv)
11+
}
12+
}
File renamed without changes.

test/files/run/t5923a-fundep.flags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Xfundep-materialization
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import scala.reflect.macros.Context
2+
import language.experimental.macros
3+
4+
case class C[T](t: String)
5+
object C {
6+
implicit def foo[T]: C[T] = macro Macros.impl[T]
7+
}
8+
9+
object Macros {
10+
def impl[T](c: Context)(ttag: c.WeakTypeTag[T]) = {
11+
import c.universe._
12+
val ttag0 = ttag;
13+
{
14+
// When we're expanding implicitly[C[Nothing]], the type inferencer will see
15+
// that foo[T] returns C[T] and that we request an implicit of type C[Nothing].
16+
//
17+
// Then the type inferencer will try to match C[T] against C[Nothing] and infer everything it can infer
18+
// from that match, but not more (e.g. if we were returning Iso[T, U] and the type we were looking at was Iso[Foo, L],
19+
// we wouldn't want U to be auto-inferred to Nothing, as it usually happens with normal methods,
20+
// but would rather want it to remain unknown, so that our macro could take a stab at inferring it:
21+
// see the comments in this commit for more information).
22+
//
23+
// Equipped with common sense, in our case of C[T] and C[Nothing] we would expect T to be inferred as Nothing, and then we
24+
// would expect T in the corresponding macro invocation to be Nothing. Unfortunately it is not that simple.
25+
//
26+
// Internally the type inferencer uses Nothing as a dummy value, which stands for "don't know how to
27+
// infer this type parameter". In the Iso example, matching Iso[T, U] against Iso[Foo, L] would result in
28+
// T being inferred as Foo and U being inferred as Nothing (!!). Then the type inferencer will think:
29+
// "Aha! U ended up being Nothing. This means that I failed to infer it,
30+
// therefore the result of my work is: T -> Foo, U -> still unknown".
31+
//
32+
// That's all very good and works very well until Nothing is a genuine result of type inference,
33+
// as in our original example of inferring T in C[T] from C[Nothing]. In that case, the inferencer becomes confused
34+
// and here in the macro implementation we get weakTypeOf[T] equal to some dummy type carrying a type parameter
35+
// instead of Nothing.
36+
//
37+
// This eccentric behavior of the type inferencer is a long-standing problem in scalac,
38+
// so the best one can do for now until it's fixed is to work around, manually converting
39+
// suspicious T's into Nothings. Of course, this means that we would have to approximate,
40+
// because there's no way to know whether having T here stands for a failed attempt to infer Nothing
41+
// or for a failed attempt to infer anything, but at least we're in full control of making the best
42+
// of this sad situation.
43+
implicit def ttag: WeakTypeTag[T] = {
44+
val tpe = ttag0.tpe
45+
val sym = tpe.typeSymbol.asType
46+
if (sym.isParameter && !sym.isSkolem) TypeTag.Nothing.asInstanceOf[TypeTag[T]]
47+
else ttag0
48+
}
49+
reify(C[T](c.literal(weakTypeOf[T].toString).splice))
50+
}
51+
}
52+
}
File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
C(Int)
2+
C(String)
3+
C(Nothing)

0 commit comments

Comments
 (0)