Skip to content

Commit 4e64a27

Browse files
committed
[nomaster] removes duplication in inferImplicitValue
Shame-driven development at its best.
1 parent 3edde27 commit 4e64a27

File tree

3 files changed

+28
-50
lines changed

3 files changed

+28
-50
lines changed

src/compiler/scala/reflect/macros/runtime/Typers.scala

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,13 @@ trait Typers {
3535

3636
def inferImplicitValue(pt: Type, silent: Boolean = true, withMacrosDisabled: Boolean = false, pos: Position = enclosingPosition): Tree = {
3737
macroLogVerbose("inferring implicit value of type %s, macros = %s".format(pt, !withMacrosDisabled))
38-
inferImplicit(universe.EmptyTree, pt, isView = false, silent = silent, withMacrosDisabled = withMacrosDisabled, pos = pos)
38+
universe.analyzer.inferImplicit(universe.EmptyTree, pt, false, callsiteTyper.context, silent, withMacrosDisabled, pos, (pos, msg) => throw TypecheckException(pos, msg))
3939
}
4040

4141
def inferImplicitView(tree: Tree, from: Type, to: Type, silent: Boolean = true, withMacrosDisabled: Boolean = false, pos: Position = enclosingPosition): Tree = {
4242
macroLogVerbose("inferring implicit view from %s to %s for %s, macros = %s".format(from, to, tree, !withMacrosDisabled))
4343
val viewTpe = universe.appliedType(universe.definitions.FunctionClass(1).toTypeConstructor, List(from, to))
44-
inferImplicit(tree, viewTpe, isView = true, silent = silent, withMacrosDisabled = withMacrosDisabled, pos = pos)
45-
}
46-
47-
private def inferImplicit(tree: Tree, pt: Type, isView: Boolean, silent: Boolean, withMacrosDisabled: Boolean, pos: Position): Tree = {
48-
import universe.analyzer.SearchResult
49-
import scala.tools.nsc.typechecker.DivergentImplicit
50-
val context = callsiteTyper.context
51-
val wrapper1 = if (!withMacrosDisabled) (context.withMacrosEnabled[SearchResult] _) else (context.withMacrosDisabled[SearchResult] _)
52-
def wrapper (inference: => SearchResult) = wrapper1(inference)
53-
def fail(reason: Option[String]) = {
54-
macroLogVerbose("implicit search has failed. to find out the reason, turn on -Xlog-implicits")
55-
if (!silent) {
56-
if (context.hasErrors) throw new TypecheckException(context.errBuffer.head.errPos, context.errBuffer.head.errMsg)
57-
else throw new TypecheckException(pos, reason getOrElse "implicit search has failed. to find out the reason, turn on -Xlog-implicits")
58-
}
59-
universe.EmptyTree
60-
}
61-
try {
62-
wrapper(universe.analyzer.inferImplicit(tree, pt, reportAmbiguous = true, isView = isView, context = context, saveAmbiguousDivergent = !silent, pos = pos)) match {
63-
case failure if failure.tree.isEmpty => fail(None)
64-
case success => success.tree
65-
}
66-
} catch {
67-
case ex: DivergentImplicit =>
68-
if (universe.settings.Xdivergence211.value)
69-
universe.debuglog("this shouldn't happen. DivergentImplicit exception has been thrown with -Xdivergence211 turned on: "+ex)
70-
fail(Some("divergent implicit expansion"))
71-
}
44+
universe.analyzer.inferImplicit(tree, viewTpe, true, callsiteTyper.context, silent, withMacrosDisabled, pos, (pos, msg) => throw TypecheckException(pos, msg))
7245
}
7346

7447
def resetAllAttrs(tree: Tree): Tree = universe.resetAllAttrs(tree)

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,31 @@ trait Implicits {
9696
result
9797
}
9898

99+
/** A friendly wrapper over inferImplicit to be used in macro contexts and toolboxes.
100+
*/
101+
def inferImplicit(tree: Tree, pt: Type, isView: Boolean, context: Context, silent: Boolean, withMacrosDisabled: Boolean, pos: Position, onError: (Position, String) => Unit): Tree = {
102+
val wrapper1 = if (!withMacrosDisabled) (context.withMacrosEnabled[SearchResult] _) else (context.withMacrosDisabled[SearchResult] _)
103+
def wrapper(inference: => SearchResult) = wrapper1(inference)
104+
def fail(reason: Option[String]) = {
105+
if (!silent) {
106+
if (context.hasErrors) onError(context.errBuffer.head.errPos, context.errBuffer.head.errMsg)
107+
else onError(pos, reason getOrElse "implicit search has failed. to find out the reason, turn on -Xlog-implicits")
108+
}
109+
EmptyTree
110+
}
111+
try {
112+
wrapper(inferImplicit(tree, pt, reportAmbiguous = true, isView = isView, context = context, saveAmbiguousDivergent = !silent, pos = pos)) match {
113+
case failure if failure.tree.isEmpty => fail(None)
114+
case success => success.tree
115+
}
116+
} catch {
117+
case ex: DivergentImplicit =>
118+
if (settings.Xdivergence211.value)
119+
debugwarn("this shouldn't happen. DivergentImplicit exception has been thrown with -Xdivergence211 turned on: "+ex)
120+
fail(Some("divergent implicit expansion"))
121+
}
122+
}
123+
99124
/** Find all views from type `tp` (in which `tpars` are free)
100125
*
101126
* Note that the trees in the search results in the returned list share the same type variables.

src/compiler/scala/tools/reflect/ToolBoxFactory.scala

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -180,28 +180,8 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf =>
180180
def inferImplicit(tree: Tree, pt: Type, isView: Boolean, silent: Boolean, withMacrosDisabled: Boolean, pos: Position): Tree =
181181
transformDuringTyper(tree, withImplicitViewsDisabled = false, withMacrosDisabled = withMacrosDisabled)(
182182
(currentTyper, tree) => {
183-
import scala.tools.nsc.typechecker.DivergentImplicit
184183
trace("inferring implicit %s (macros = %s): ".format(if (isView) "view" else "value", !withMacrosDisabled))(showAttributed(pt, true, true, settings.Yshowsymkinds.value))
185-
val context = currentTyper.context
186-
def fail(reason: Option[String]) = {
187-
debuglog("reflective implicit search has failed. to find out the reason, turn on -Xlog-implicits")
188-
if (!silent) {
189-
if (context.hasErrors) throw ToolBoxError("reflective implicit search has failed: %s".format(context.errBuffer.head.errMsg))
190-
else throw ToolBoxError(reason getOrElse "reflective implicit search has failed. to find out the reason, turn on -Xlog-implicits")
191-
}
192-
EmptyTree
193-
}
194-
try {
195-
analyzer.inferImplicit(tree, pt, reportAmbiguous = true, isView = isView, context = context, saveAmbiguousDivergent = !silent, pos = pos) match {
196-
case failure if failure.tree.isEmpty => fail(None)
197-
case success => success.tree
198-
}
199-
} catch {
200-
case ex: DivergentImplicit =>
201-
if (settings.Xdivergence211.value)
202-
debuglog("this shouldn't happen. DivergentImplicit exception has been thrown with -Xdivergence211 turned on: "+ex)
203-
fail(Some("divergent implicit expansion"))
204-
}
184+
analyzer.inferImplicit(tree, pt, isView, currentTyper.context, silent, withMacrosDisabled, pos, (pos, msg) => throw ToolBoxError(msg))
205185
})
206186

207187
def compile(expr0: Tree): () => Any = {

0 commit comments

Comments
 (0)