Skip to content

Commit 5c830d8

Browse files
committed
Fix Closure span assignment in makeClosure
1 parent 3d5cf9c commit 5c830d8

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ object desugar {
15231523
DefDef(nme.ANON_FUN, paramss, if (tpt == null) TypeTree() else tpt, body)
15241524
.withSpan(span)
15251525
.withMods(synthetic | Artifact),
1526-
Closure(Nil, Ident(nme.ANON_FUN), EmptyTree))
1526+
Closure(Nil, Ident(nme.ANON_FUN), EmptyTree).withSpan(span))
15271527

15281528
/** If `nparams` == 1, expand partial function
15291529
*

compiler/src/dotty/tools/dotc/ast/NavigateAST.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package ast
44
import core.Contexts.*
55
import core.Decorators.*
66
import util.Spans.*
7-
import Trees.{MemberDef, DefTree, WithLazyFields}
7+
import Trees.{Closure, MemberDef, DefTree, WithLazyFields}
88
import dotty.tools.dotc.core.Types.AnnotatedType
99
import dotty.tools.dotc.core.Types.ImportType
1010
import dotty.tools.dotc.core.Types.Type
@@ -76,7 +76,7 @@ object NavigateAST {
7676
var bestFit: List[Positioned] = path
7777
while (it.hasNext) {
7878
val path1 = it.next() match {
79-
case p: Positioned => singlePath(p, path)
79+
case p: Positioned if !p.isInstanceOf[Closure[?]] => singlePath(p, path)
8080
case m: untpd.Modifiers => childPath(m.productIterator, path)
8181
case xs: List[?] => childPath(xs.iterator, path)
8282
case _ => path

compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,6 +1785,64 @@ class DottyBytecodeTests extends DottyBytecodeTest {
17851785
}
17861786
}
17871787

1788+
1789+
@Test def i15098 = {
1790+
val source =
1791+
"""object Main {
1792+
| def main(args: Array[String]): Unit = {
1793+
| Array(1).foreach { n =>
1794+
| val x = 123
1795+
| println(n)
1796+
| }
1797+
| }
1798+
|}
1799+
""".stripMargin
1800+
1801+
checkBCode(source) { dir =>
1802+
val clsIn = dir.lookupName("Main$.class", directory = false).input
1803+
val clsNode = loadClassNode(clsIn, skipDebugInfo = false)
1804+
val method = getMethod(clsNode, "main")
1805+
val instructions = instructionsFromMethod(method).filter(_.isInstanceOf[LineNumber])
1806+
1807+
val expected = List(
1808+
LineNumber(3, Label(0)),
1809+
)
1810+
1811+
assertSameCode(instructions, expected)
1812+
}
1813+
}
1814+
1815+
@Test def i15098_2 = {
1816+
val source =
1817+
"""object Main {
1818+
| def main(args: Array[String]): Unit = {
1819+
| Array(1).map { n =>
1820+
| val x = 123
1821+
| x + n
1822+
| }.foreach { n =>
1823+
| println(n)
1824+
| println(n)
1825+
| }
1826+
| }
1827+
|}
1828+
""".stripMargin
1829+
1830+
checkBCode(source) { dir =>
1831+
val clsIn = dir.lookupName("Main$.class", directory = false).input
1832+
val clsNode = loadClassNode(clsIn, skipDebugInfo = false)
1833+
val method = getMethod(clsNode, "main")
1834+
val instructions = instructionsFromMethod(method).filter(_.isInstanceOf[LineNumber])
1835+
1836+
val expected = List(
1837+
LineNumber(3, Label(0)),
1838+
LineNumber(6, Label(15)),
1839+
LineNumber(3, Label(24)),
1840+
LineNumber(6, Label(27)),
1841+
)
1842+
1843+
assertSameCode(instructions, expected)
1844+
}
1845+
}
17881846
}
17891847

17901848
object invocationReceiversTestCode {

tests/neg/i9299.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
type F <: F = 1 match { // error
2-
case _ => foo.foo // error // error
2+
case _ => foo.foo // error
33
}
44
def foo(a: Int): Unit = ???

0 commit comments

Comments
 (0)