Skip to content

Commit 930fe2c

Browse files
committed
Upgrade implicits in Interpreter
* Use `using` for `Env` parameters and arguments * Remove unnecessary `Env` parameters
1 parent e587a81 commit 930fe2c

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

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

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

38+
/** Local variable environment */
3839
type Env = Map[Symbol, Object]
40+
def emptyEnv: Env = Map.empty
41+
inline def env(using e: Env): e.type = e
3942

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

@@ -135,26 +138,26 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
135138
Nil
136139
}
137140

138-
private def interpretBlock(stats: List[Tree], expr: Tree)(implicit env: Env) = {
141+
private def interpretBlock(stats: List[Tree], expr: Tree)(using Env) = {
139142
var unexpected: Option[Object] = None
140-
val newEnv = stats.foldLeft(env)((accEnv, stat) => stat match {
143+
val newEnv = stats.foldLeft(env)((accEnv, stat) => stat match
141144
case stat: ValDef =>
142-
accEnv.updated(stat.symbol, interpretTree(stat.rhs)(accEnv))
145+
accEnv.updated(stat.symbol, interpretTree(stat.rhs)(using accEnv))
143146
case stat =>
144147
if (unexpected.isEmpty)
145148
unexpected = Some(unexpectedTree(stat))
146149
accEnv
147-
})
148-
unexpected.getOrElse(interpretTree(expr)(newEnv))
150+
)
151+
unexpected.getOrElse(interpretTree(expr)(using newEnv))
149152
}
150153

151-
private def interpretLiteral(value: Any)(implicit env: Env): Object =
154+
private def interpretLiteral(value: Any): Object =
152155
value.asInstanceOf[Object]
153156

154-
private def interpretVarargs(args: List[Object])(implicit env: Env): Object =
157+
private def interpretVarargs(args: List[Object]): Object =
155158
args.toSeq
156159

157-
private def interpretedStaticMethodCall(moduleClass: Symbol, fn: Symbol)(implicit env: Env): List[Object] => Object = {
160+
private def interpretedStaticMethodCall(moduleClass: Symbol, fn: Symbol): List[Object] => Object = {
158161
val (inst, clazz) =
159162
try
160163
if (moduleClass.name.startsWith(str.REPL_SESSION_LINE))
@@ -174,22 +177,22 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
174177
(args: List[Object]) => stopIfRuntimeException(method.invoke(inst, args: _*), method)
175178
}
176179

177-
private def interpretedStaticFieldAccess(sym: Symbol)(implicit env: Env): Object = {
180+
private def interpretedStaticFieldAccess(sym: Symbol): Object = {
178181
val clazz = loadClass(sym.owner.fullName.toString)
179182
val field = clazz.getField(sym.name.toString)
180183
field.get(null)
181184
}
182185

183-
private def interpretModuleAccess(fn: Symbol)(implicit env: Env): Object =
186+
private def interpretModuleAccess(fn: Symbol): Object =
184187
loadModule(fn.moduleClass)
185188

186-
private def interpretNew(fn: Symbol, args: => List[Object])(implicit env: Env): Object = {
189+
private def interpretNew(fn: Symbol, args: => List[Object]): Object = {
187190
val clazz = loadClass(fn.owner.fullName.toString)
188191
val constr = clazz.getConstructor(paramsSig(fn): _*)
189192
constr.newInstance(args: _*).asInstanceOf[Object]
190193
}
191194

192-
private def unexpectedTree(tree: Tree)(implicit env: Env): Object =
195+
private def unexpectedTree(tree: Tree): Object =
193196
throw new StopInterpretation("Unexpected tree could not be interpreted: " + tree, tree.srcPos)
194197

195198
private def loadModule(sym: Symbol): Object =
@@ -209,7 +212,7 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
209212
clazz.getConstructor().newInstance().asInstanceOf[Object]
210213
}
211214

212-
private def loadReplLineClass(moduleClass: Symbol)(implicit env: Env): Class[?] = {
215+
private def loadReplLineClass(moduleClass: Symbol): Class[?] = {
213216
val lineClassloader = new AbstractFileClassLoader(ctx.settings.outputDir.value, classLoader)
214217
lineClassloader.loadClass(moduleClass.name.firstPart.toString)
215218
}

0 commit comments

Comments
 (0)