Skip to content

Commit 5cf8a58

Browse files
authored
Upgrade implicits in interpreter (#16359)
2 parents 93eb3bb + 16488eb commit 5cf8a58

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

compiler/src/dotty/tools/dotc/quoted/Interpreter.scala

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,17 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
3636
import Interpreter._
3737
import tpd._
3838

39+
/** Local variable environment */
3940
type Env = Map[Symbol, Object]
41+
def emptyEnv: Env = Map.empty
42+
inline def env(using e: Env): e.type = e
4043

4144
/** Returns the result of interpreting the code in the tree.
4245
* Return Some of the result or None if the result type is not consistent with the expected type.
4346
* Throws a StopInterpretation if the tree could not be interpreted or a runtime exception ocurred.
4447
*/
45-
final def interpret[T](tree: Tree)(implicit ct: ClassTag[T]): Option[T] =
46-
interpretTree(tree)(Map.empty) match {
48+
final def interpret[T](tree: Tree)(using ct: ClassTag[T]): Option[T] =
49+
interpretTree(tree)(using emptyEnv) match {
4750
case obj: T => Some(obj)
4851
case obj =>
4952
// TODO upgrade to a full type tag check or something similar
@@ -54,7 +57,7 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
5457
/** Returns the result of interpreting the code in the tree.
5558
* Throws a StopInterpretation if the tree could not be interpreted or a runtime exception ocurred.
5659
*/
57-
protected def interpretTree(tree: Tree)(implicit env: Env): Object = tree match {
60+
protected def interpretTree(tree: Tree)(using Env): Object = tree match {
5861
case Literal(Constant(value)) =>
5962
interpretLiteral(value)
6063

@@ -70,8 +73,7 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
7073
else if (fn.symbol.is(Module))
7174
interpretModuleAccess(fn.symbol)
7275
else if (fn.symbol.is(Method) && fn.symbol.isStatic) {
73-
val staticMethodCall = interpretedStaticMethodCall(fn.symbol.owner, fn.symbol)
74-
staticMethodCall(interpretArgs(args, fn.symbol.info))
76+
interpretedStaticMethodCall(fn.symbol.owner, fn.symbol, interpretArgs(args, fn.symbol.info))
7577
}
7678
else if fn.symbol.isStatic then
7779
assert(args.isEmpty)
@@ -80,8 +82,7 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
8082
if (fn.name == nme.asInstanceOfPM)
8183
interpretModuleAccess(fn.qualifier.symbol)
8284
else {
83-
val staticMethodCall = interpretedStaticMethodCall(fn.qualifier.symbol.moduleClass, fn.symbol)
84-
staticMethodCall(interpretArgs(args, fn.symbol.info))
85+
interpretedStaticMethodCall(fn.qualifier.symbol.moduleClass, fn.symbol, interpretArgs(args, fn.symbol.info))
8586
}
8687
else if (env.contains(fn.symbol))
8788
env(fn.symbol)
@@ -136,26 +137,26 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
136137
Nil
137138
}
138139

139-
private def interpretBlock(stats: List[Tree], expr: Tree)(implicit env: Env) = {
140+
private def interpretBlock(stats: List[Tree], expr: Tree)(using Env) = {
140141
var unexpected: Option[Object] = None
141-
val newEnv = stats.foldLeft(env)((accEnv, stat) => stat match {
142+
val newEnv = stats.foldLeft(env)((accEnv, stat) => stat match
142143
case stat: ValDef =>
143-
accEnv.updated(stat.symbol, interpretTree(stat.rhs)(accEnv))
144+
accEnv.updated(stat.symbol, interpretTree(stat.rhs)(using accEnv))
144145
case stat =>
145146
if (unexpected.isEmpty)
146147
unexpected = Some(unexpectedTree(stat))
147148
accEnv
148-
})
149-
unexpected.getOrElse(interpretTree(expr)(newEnv))
149+
)
150+
unexpected.getOrElse(interpretTree(expr)(using newEnv))
150151
}
151152

152-
private def interpretLiteral(value: Any)(implicit env: Env): Object =
153+
private def interpretLiteral(value: Any): Object =
153154
value.asInstanceOf[Object]
154155

155-
private def interpretVarargs(args: List[Object])(implicit env: Env): Object =
156+
private def interpretVarargs(args: List[Object]): Object =
156157
args.toSeq
157158

158-
private def interpretedStaticMethodCall(moduleClass: Symbol, fn: Symbol)(implicit env: Env): List[Object] => Object = {
159+
private def interpretedStaticMethodCall(moduleClass: Symbol, fn: Symbol, args: List[Object]): Object = {
159160
val (inst, clazz) =
160161
try
161162
if (moduleClass.name.startsWith(str.REPL_SESSION_LINE))
@@ -172,25 +173,25 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
172173

173174
val name = fn.name.asTermName
174175
val method = getMethod(clazz, name, paramsSig(fn))
175-
(args: List[Object]) => stopIfRuntimeException(method.invoke(inst, args: _*), method)
176+
stopIfRuntimeException(method.invoke(inst, args: _*), method)
176177
}
177178

178-
private def interpretedStaticFieldAccess(sym: Symbol)(implicit env: Env): Object = {
179+
private def interpretedStaticFieldAccess(sym: Symbol): Object = {
179180
val clazz = loadClass(sym.owner.fullName.toString)
180181
val field = clazz.getField(sym.name.toString)
181182
field.get(null)
182183
}
183184

184-
private def interpretModuleAccess(fn: Symbol)(implicit env: Env): Object =
185+
private def interpretModuleAccess(fn: Symbol): Object =
185186
loadModule(fn.moduleClass)
186187

187-
private def interpretNew(fn: Symbol, args: => List[Object])(implicit env: Env): Object = {
188+
private def interpretNew(fn: Symbol, args: => List[Object]): Object = {
188189
val clazz = loadClass(fn.owner.fullName.toString)
189190
val constr = clazz.getConstructor(paramsSig(fn): _*)
190191
constr.newInstance(args: _*).asInstanceOf[Object]
191192
}
192193

193-
private def unexpectedTree(tree: Tree)(implicit env: Env): Object =
194+
private def unexpectedTree(tree: Tree): Object =
194195
throw new StopInterpretation(em"Unexpected tree could not be interpreted: ${tree.toString}", tree.srcPos)
195196

196197
private def loadModule(sym: Symbol): Object =
@@ -210,7 +211,7 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
210211
clazz.getConstructor().newInstance().asInstanceOf[Object]
211212
}
212213

213-
private def loadReplLineClass(moduleClass: Symbol)(implicit env: Env): Class[?] = {
214+
private def loadReplLineClass(moduleClass: Symbol): Class[?] = {
214215
val lineClassloader = new AbstractFileClassLoader(ctx.settings.outputDir.value, classLoader)
215216
lineClassloader.loadClass(moduleClass.name.firstPart.toString)
216217
}

0 commit comments

Comments
 (0)