diff --git a/compiler/src/dotty/tools/repl/ReplCompiler.scala b/compiler/src/dotty/tools/repl/ReplCompiler.scala index 61d7c244fb70..f32d247c7635 100644 --- a/compiler/src/dotty/tools/repl/ReplCompiler.scala +++ b/compiler/src/dotty/tools/repl/ReplCompiler.scala @@ -103,7 +103,7 @@ class ReplCompiler extends Compiler { Definitions( defs.toList, state.copy( - objectIndex = state.objectIndex + (if (defs.isEmpty) 0 else 1), + objectIndex = state.objectIndex + 1, valIndex = valIdx ) ) @@ -123,21 +123,19 @@ class ReplCompiler extends Compiler { * } * ``` */ - private def wrapped(defs: Definitions, objectTermName: TermName): untpd.PackageDef = { + private def wrapped(defs: Definitions, objectTermName: TermName, span: Span): untpd.PackageDef = { import untpd._ - assert(defs.stats.nonEmpty) - implicit val ctx: Context = defs.state.context val tmpl = Template(emptyConstructor, Nil, Nil, EmptyValDef, defs.stats) val module = ModuleDef(objectTermName, tmpl) - .withSpan(Span(0, defs.stats.last.span.end)) + .withSpan(span) PackageDef(Ident(nme.EMPTY_PACKAGE), List(module)) } - private def createUnit(defs: Definitions)(implicit ctx: Context): CompilationUnit = { + private def createUnit(defs: Definitions, span: Span)(implicit ctx: Context): CompilationUnit = { val objectName = ctx.source.file.toString assert(objectName.startsWith(str.REPL_SESSION_LINE)) assert(objectName.endsWith(defs.state.objectIndex.toString)) @@ -145,7 +143,7 @@ class ReplCompiler extends Compiler { objectNames.update(defs.state.objectIndex, objectTermName) val unit = CompilationUnit(ctx.source) - unit.untpdTree = wrapped(defs, objectTermName) + unit.untpdTree = wrapped(defs, objectTermName, span) unit } @@ -158,8 +156,9 @@ class ReplCompiler extends Compiler { } final def compile(parsed: Parsed)(implicit state: State): Result[(CompilationUnit, State)] = { + assert(!parsed.trees.isEmpty) val defs = definitions(parsed.trees, state) - val unit = createUnit(defs)(state.context) + val unit = createUnit(defs, Span(0, parsed.trees.last.span.end))(state.context) runCompilationUnit(unit, defs.state) } diff --git a/compiler/test-resources/repl/i5890 b/compiler/test-resources/repl/i5890 new file mode 100644 index 000000000000..6e3c38d5b287 --- /dev/null +++ b/compiler/test-resources/repl/i5890 @@ -0,0 +1,12 @@ +scala> + +scala> {} + +scala> {};{} + +scala> {{}} + +scala> {()} + +scala> () +