Skip to content

Fix #5897 and #6200: implicits are not retained in REPL #6237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/ImportInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import printing.Texts.Text

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

Expand Down
9 changes: 6 additions & 3 deletions compiler/src/dotty/tools/repl/ReplCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ class ReplCompiler extends Compiler {
def importPreviousRun(id: Int)(implicit ctx: Context) = {
// we first import the wrapper object id
val path = nme.EMPTY_PACKAGE ++ "." ++ objectNames(id)
val importInfo = ImportInfo.rootImport(() =>
ctx.requiredModuleRef(path))
val ctx0 = ctx.fresh.setNewScope.setImportInfo(importInfo)
def importWrapper(c: Context, importImplied: Boolean) = {
val importInfo = ImportInfo.rootImport(() =>
c.requiredModuleRef(path), importImplied)
c.fresh.setNewScope.setImportInfo(importInfo)
}
val ctx0 = importWrapper(importWrapper(ctx, false), true)

// then its user defined imports
val imports = state.imports.getOrElse(id, Nil)
Expand Down
38 changes: 38 additions & 0 deletions compiler/test/dotty/tools/repl/ReplCompilerTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,42 @@ class ReplCompilerTests extends ReplTest {
run(source)
assertEquals(expected, lines())
}

@Test def i5897 =
fromInitialState { implicit state => run("implied for Int = 10") }
.andThen { implicit state =>
assertEquals(
"def Int_instance: Int",
storedOutput().trim
)
run("implicitly[Int]")
assertEquals(
"val res0: Int = 10",
storedOutput().trim
)
}

@Test def i6200 =
fromInitialState { implicit state =>
run("""
|trait Ord[T] {
| def compare(x: T, y: T): Int
| def (x: T) < (y: T) = compare(x, y) < 0
| def (x: T) > (y: T) = compare(x, y) > 0
|}
|
|implied IntOrd for Ord[Int] {
| def compare(x: Int, y: Int) =
| if (x < y) -1 else if (x > y) +1 else 0
|}
""".stripMargin) }
.andThen { implicit state =>
assertEquals(
"""// defined trait Ord
|// defined object IntOrd""".stripMargin,
storedOutput().trim
)
run("IntOrd")
assertTrue(storedOutput().startsWith("val res0: IntOrd.type ="))
}
}