Skip to content

Commit 4d1e2b0

Browse files
aherlihydwijnand
authored andcommitted
Handle out of sync imports after require + tests
1 parent 2babf5c commit 4d1e2b0

File tree

6 files changed

+57
-1
lines changed

6 files changed

+57
-1
lines changed

compiler/src/dotty/tools/repl/ReplCompiler.scala

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ class ReplCompiler extends Compiler:
4747
/** Import previous runs and user defined imports */
4848
override protected def rootContext(using Context): Context = {
4949
def importContext(imp: tpd.Import)(using Context) =
50-
ctx.importContext(imp, imp.symbol)
50+
// TODO: only when context has changed?
51+
val typer = ctx.typer
52+
typer.index(imp)
53+
val imp2 = typer.typed(imp).asInstanceOf[tpd.Import]
54+
ctx.importContext(imp2, imp2.symbol)
5155

5256
def importPreviousRun(id: Int)(using Context) = {
5357
// we first import the wrapper object id
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// MyLibrary.scala
2+
package mylibrary
3+
4+
object Utils:
5+
def greet(name: String): String = s"Hello, $name!"
6+
7+
class Calculator:
8+
def add(x: Int, y: Int): Int = x + y
9+
def subtract(x: Int, y: Int): Int = x - y
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// MyLibrary2.scala
2+
package mylibrary2
3+
4+
object Utils2:
5+
def greet(name: String): String = s"Hello, $name!"
6+
7+
class Calculator2:
8+
def add(x: Int, y: Int): Int = x + y
9+
def subtract(x: Int, y: Int): Int = x - y
10+
3.32 KB
Binary file not shown.
3.35 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
scala> val z = 1
2+
val z: Int = 1
3+
4+
scala>:require compiler/test-resources/jars/mylibrary.jar
5+
Added 'compiler/test-resources/jars/mylibrary.jar' to classpath.
6+
7+
scala> import mylibrary.Utils
8+
9+
scala> Utils.greet("Alice")
10+
val res0: String = Hello, Alice!
11+
12+
scala>:require compiler/test-resources/jars/mylibrary2.jar
13+
Added 'compiler/test-resources/jars/mylibrary2.jar' to classpath.
14+
15+
scala> import mylibrary2.Utils2
16+
17+
scala> Utils2.greet("Alice")
18+
val res1: String = Hello, Alice!
19+
20+
scala> Utils.greet("Alice")
21+
val res2: String = Hello, Alice!
22+
23+
scala> import mylibrary.Utils.greet
24+
25+
scala> greet("Tom")
26+
val res3: String = Hello, Tom!
27+
28+
scala> Utils.greet("Alice")
29+
val res4: String = Hello, Alice!
30+
31+
scala> z
32+
val res5: Int = 1

0 commit comments

Comments
 (0)