Skip to content

Commit 50e17d1

Browse files
committed
Handle out of sync imports after require + tests
1 parent 0745c34 commit 50e17d1

File tree

6 files changed

+57
-1
lines changed

6 files changed

+57
-1
lines changed

Diff for: compiler/src/dotty/tools/repl/ReplCompiler.scala

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

5155
def importPreviousRun(id: Int)(using Context) = {
5256
// we first import the wrapper object id

Diff for: compiler/test-resources/jars/MyLibrary.scala

+10
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+

Diff for: compiler/test-resources/jars/MyLibrary2.scala

+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+

Diff for: compiler/test-resources/jars/mylibrary.jar

3.32 KB
Binary file not shown.

Diff for: compiler/test-resources/jars/mylibrary2.jar

3.35 KB
Binary file not shown.

Diff for: compiler/test-resources/repl/require-multiple

+32
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)