Skip to content

Commit 7143421

Browse files
authored
Merge pull request #6237 from dotty-staging/fix-5897-6200
Fix #5897 and #6200: implicits are not retained in REPL
2 parents 5ce597a + 8539117 commit 7143421

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

compiler/src/dotty/tools/dotc/typer/ImportInfo.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import printing.Texts.Text
1313

1414
object ImportInfo {
1515
/** The import info for a root import from given symbol `sym` */
16-
def rootImport(refFn: () => TermRef)(implicit ctx: Context): ImportInfo = {
16+
def rootImport(refFn: () => TermRef, importImplied: Boolean = false)(implicit ctx: Context): ImportInfo = {
1717
val selectors = untpd.Ident(nme.WILDCARD) :: Nil
1818
def expr(implicit ctx: Context) = tpd.Ident(refFn())
19-
def imp(implicit ctx: Context) = tpd.Import(importImplied = false, expr, selectors)
20-
new ImportInfo(implicit ctx => imp.symbol, selectors, None, importImplied = false, isRootImport = true)
19+
def imp(implicit ctx: Context) = tpd.Import(importImplied = importImplied, expr, selectors)
20+
new ImportInfo(implicit ctx => imp.symbol, selectors, None, importImplied = importImplied, isRootImport = true)
2121
}
2222
}
2323

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ class ReplCompiler extends Compiler {
4848
def importPreviousRun(id: Int)(implicit ctx: Context) = {
4949
// we first import the wrapper object id
5050
val path = nme.EMPTY_PACKAGE ++ "." ++ objectNames(id)
51-
val importInfo = ImportInfo.rootImport(() =>
52-
ctx.requiredModuleRef(path))
53-
val ctx0 = ctx.fresh.setNewScope.setImportInfo(importInfo)
51+
def importWrapper(c: Context, importImplied: Boolean) = {
52+
val importInfo = ImportInfo.rootImport(() =>
53+
c.requiredModuleRef(path), importImplied)
54+
c.fresh.setNewScope.setImportInfo(importInfo)
55+
}
56+
val ctx0 = importWrapper(importWrapper(ctx, false), true)
5457

5558
// then its user defined imports
5659
val imports = state.imports.getOrElse(id, Nil)

compiler/test/dotty/tools/repl/ReplCompilerTests.scala

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,42 @@ class ReplCompilerTests extends ReplTest {
127127
run(source)
128128
assertEquals(expected, lines())
129129
}
130+
131+
@Test def i5897 =
132+
fromInitialState { implicit state => run("implied for Int = 10") }
133+
.andThen { implicit state =>
134+
assertEquals(
135+
"def Int_instance: Int",
136+
storedOutput().trim
137+
)
138+
run("implicitly[Int]")
139+
assertEquals(
140+
"val res0: Int = 10",
141+
storedOutput().trim
142+
)
143+
}
144+
145+
@Test def i6200 =
146+
fromInitialState { implicit state =>
147+
run("""
148+
|trait Ord[T] {
149+
| def compare(x: T, y: T): Int
150+
| def (x: T) < (y: T) = compare(x, y) < 0
151+
| def (x: T) > (y: T) = compare(x, y) > 0
152+
|}
153+
|
154+
|implied IntOrd for Ord[Int] {
155+
| def compare(x: Int, y: Int) =
156+
| if (x < y) -1 else if (x > y) +1 else 0
157+
|}
158+
""".stripMargin) }
159+
.andThen { implicit state =>
160+
assertEquals(
161+
"""// defined trait Ord
162+
|// defined object IntOrd""".stripMargin,
163+
storedOutput().trim
164+
)
165+
run("IntOrd")
166+
assertTrue(storedOutput().startsWith("val res0: IntOrd.type ="))
167+
}
130168
}

0 commit comments

Comments
 (0)