diff --git a/compiler/src/dotty/tools/dotc/ast/Desugar.scala b/compiler/src/dotty/tools/dotc/ast/Desugar.scala index 2d99cf201375..6ed05976a19e 100644 --- a/compiler/src/dotty/tools/dotc/ast/Desugar.scala +++ b/compiler/src/dotty/tools/dotc/ast/Desugar.scala @@ -1523,7 +1523,7 @@ object desugar { DefDef(nme.ANON_FUN, paramss, if (tpt == null) TypeTree() else tpt, body) .withSpan(span) .withMods(synthetic | Artifact), - Closure(Nil, Ident(nme.ANON_FUN), EmptyTree)) + Closure(Nil, Ident(nme.ANON_FUN), EmptyTree).withSpan(span)) /** If `nparams` == 1, expand partial function * diff --git a/compiler/src/dotty/tools/dotc/ast/NavigateAST.scala b/compiler/src/dotty/tools/dotc/ast/NavigateAST.scala index 2960af8fcdec..f83f12e1c027 100644 --- a/compiler/src/dotty/tools/dotc/ast/NavigateAST.scala +++ b/compiler/src/dotty/tools/dotc/ast/NavigateAST.scala @@ -4,7 +4,7 @@ package ast import core.Contexts.* import core.Decorators.* import util.Spans.* -import Trees.{MemberDef, DefTree, WithLazyFields} +import Trees.{Closure, MemberDef, DefTree, WithLazyFields} import dotty.tools.dotc.core.Types.AnnotatedType import dotty.tools.dotc.core.Types.ImportType import dotty.tools.dotc.core.Types.Type @@ -76,7 +76,7 @@ object NavigateAST { var bestFit: List[Positioned] = path while (it.hasNext) { val path1 = it.next() match { - case p: Positioned => singlePath(p, path) + case p: Positioned if !p.isInstanceOf[Closure[?]] => singlePath(p, path) case m: untpd.Modifiers => childPath(m.productIterator, path) case xs: List[?] => childPath(xs.iterator, path) case _ => path diff --git a/compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala b/compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala index 51390e35b527..f446913d7964 100644 --- a/compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala +++ b/compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala @@ -1785,6 +1785,64 @@ class DottyBytecodeTests extends DottyBytecodeTest { } } + + @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(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(3, Label(0)), + LineNumber(6, Label(15)), + LineNumber(3, Label(24)), + LineNumber(6, Label(27)), + ) + + 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 = ???