@@ -18,32 +18,54 @@ private[macwire] class DependencyResolver[C <: blackbox.Context](val c: C, debug
1818 */
1919 def resolve (param : Symbol , t : Type ): Tree = {
2020
21- eligibleValues.findInFirstScope(t).toList match {
22- case Nil if isOption(t) =>
23- getOptionArg(t).flatMap(u => eligibleValues.findInFirstScope(u).toList.headOption) match { // FIXME: handle multiple values (tests, tests...)
24- case Some (argTree) => q " Some( $argTree) "
25- case None => q " None "
26- }
27- case Nil => c.abort(c.enclosingPosition, s " Cannot find a value of type: [ $t] " )
21+ def resolve (t : Type , handler : (Type , List [Tree ]) => Tree ): Tree = {
22+ val trees : List [Tree ] = eligibleValues.findInFirstScope(t).toList match {
23+ case Nil if isOption(t) => List (resolve(getOptionArg(t).get, optionHandler))
24+ case ts => ts
25+ }
26+ handler(t, trees)
27+ }
28+
29+ def basicHandler (t : Type , trees : List [Tree ]): Tree = trees match {
30+ case Nil => noValueError(t)
2831 case value :: Nil =>
29- val forwardValues = eligibleValues.findInScope(t, LocalForward )
30- if (forwardValues.nonEmpty) {
31- c.warning(c.enclosingPosition, s " Found [ $value] for parameter [ ${param.name}], " +
32- s " but a forward reference [ ${forwardValues.mkString(" , " )}] was also eligible " )
33- }
32+ forwardValuesWarn(value)
3433 value
35- case values => c.abort(c.enclosingPosition, s " Found multiple values of type [ $t ]: [ $values ] " )
34+ case values => multipleValuesError(t, values)
3635 }
37- }
3836
39- private def isOption (t : Type ): Boolean = getOptionArg(t).nonEmpty
37+ def optionHandler (t : Type , trees : List [Tree ]): Tree = trees match {
38+ case Nil => q " None "
39+ case value :: Nil if value.equalsStructure(q " None " ) => q " None "
40+ case value :: Nil =>
41+ forwardValuesWarn(value)
42+ q " Some( $value) "
43+ case values => multipleValuesError(t, values)
44+ }
4045
41- private def getOptionArg (t : Type ): Option [Type ] = t.baseType(typeOf[Option [_]].typeSymbol) match {
42- case TypeRef (_, _, arg :: Nil ) => Some (arg)
43- case NoType => None
44- }
46+ def noValueError (t : Type ): Nothing =
47+ c.abort(c.enclosingPosition, s " Cannot find a value of type: [ $t] " )
48+
49+ def multipleValuesError (t : Type , values : List [Tree ]): Nothing =
50+ c.abort(c.enclosingPosition, s " Found multiple values of type [ $t]: [ $values] " )
4551
46-
52+ def forwardValuesWarn (value : Tree ): Unit = {
53+ val forwardValues = eligibleValues.findInScope(t, LocalForward )
54+ if (forwardValues.nonEmpty) {
55+ c.warning(c.enclosingPosition, s " Found [ $value] for parameter [ ${param.name}], " +
56+ s " but a forward reference [ ${forwardValues.mkString(" , " )}] was also eligible " )
57+ }
58+ }
59+
60+ def isOption (t : Type ): Boolean = getOptionArg(t).nonEmpty
61+
62+ def getOptionArg (t : Type ): Option [Type ] = t.baseType(typeOf[Option [_]].typeSymbol) match {
63+ case TypeRef (_, _, arg :: Nil ) => Some (arg)
64+ case NoType => None
65+ }
66+
67+ resolve(t, basicHandler)
68+ }
4769
4870 /** @return all the instances of type `t` that are accessible.
4971 */
0 commit comments