Skip to content

Commit

Permalink
Fix Closure span assignment in makeClosure
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian3k committed Mar 22, 2024
1 parent 3d5cf9c commit 5c830d8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/ast/NavigateAST.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/i9299.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type F <: F = 1 match { // error
case _ => foo.foo // error // error
case _ => foo.foo // error
}
def foo(a: Int): Unit = ???

0 comments on commit 5c830d8

Please sign in to comment.