From b93523d686f0dbeee06367d6df31b0a3a74b3429 Mon Sep 17 00:00:00 2001 From: Florian3k Date: Mon, 8 Aug 2022 10:28:42 +0200 Subject: [PATCH] Fix Closure span assignment in makeClosure --- .../src/dotty/tools/dotc/ast/Desugar.scala | 4 +- .../backend/jvm/DottyBytecodeTests.scala | 60 +++++++++++++++++++ tests/neg/i9299.scala | 2 +- 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/ast/Desugar.scala b/compiler/src/dotty/tools/dotc/ast/Desugar.scala index 10d4fed7f058..b589222e5599 100644 --- a/compiler/src/dotty/tools/dotc/ast/Desugar.scala +++ b/compiler/src/dotty/tools/dotc/ast/Desugar.scala @@ -1412,7 +1412,9 @@ object desugar { DefDef(nme.ANON_FUN, params :: Nil, if (tpt == null) TypeTree() else tpt, body) .withSpan(span) .withMods(synthetic | Artifact), - Closure(Nil, Ident(nme.ANON_FUN), if (isContextual) ContextualEmptyTree else EmptyTree)) + Closure(Nil, Ident(nme.ANON_FUN), if (isContextual) ContextualEmptyTree else EmptyTree) + .withSpan(span) + ) /** If `nparams` == 1, expand partial function * diff --git a/compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala b/compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala index 2c618ea91e96..ea79605b4672 100644 --- a/compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala +++ b/compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala @@ -1684,6 +1684,66 @@ class DottyBytecodeTests extends DottyBytecodeTest { assertSameCode(instructions, expected) } } + + @Test def i15098 = { + val source = + """object Main { + | def main(args: Array[String]): Unit = { + | Array(1).foreach { n => + | val x = 123 + | println(n) + | } + | } + |} + """.stripMargin + + checkBCode(source) { dir => + val clsIn = dir.lookupName("Main$.class", directory = false).input + val clsNode = loadClassNode(clsIn, skipDebugInfo = false) + val method = getMethod(clsNode, "main") + val instructions = instructionsFromMethod(method).filter(_.isInstanceOf[LineNumber]) + + val expected = List( + LineNumber(2, Label(0)), + LineNumber(3, Label(0)), + ) + + assertSameCode(instructions, expected) + } + } + + @Test def i15098_2 = { + val source = + """object Main { + | def main(args: Array[String]): Unit = { + | Array(1).map { n => + | val x = 123 + | x + n + | }.foreach { n => + | println(n) + | println(n) + | } + | } + |} + """.stripMargin + + checkBCode(source) { dir => + val clsIn = dir.lookupName("Main$.class", directory = false).input + val clsNode = loadClassNode(clsIn, skipDebugInfo = false) + val method = getMethod(clsNode, "main") + val instructions = instructionsFromMethod(method).filter(_.isInstanceOf[LineNumber]) + + val expected = List( + LineNumber(2, Label(0)), + LineNumber(3, Label(0)), + LineNumber(6, Label(17)), + LineNumber(3, Label(26)), + LineNumber(6, Label(29)), + ) + + assertSameCode(instructions, expected) + } + } } object invocationReceiversTestCode { diff --git a/tests/neg/i9299.scala b/tests/neg/i9299.scala index 6c23d11553ff..c3ae55ab9d18 100644 --- a/tests/neg/i9299.scala +++ b/tests/neg/i9299.scala @@ -1,4 +1,4 @@ type F <: F = 1 match { // error - case _ => foo.foo // error // error + case _ => foo.foo // error } def foo(a: Int): Unit = ???