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 May 10, 2023
1 parent 5a9b616 commit a382793
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,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
*
Expand Down
60 changes: 60 additions & 0 deletions compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,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 {
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 a382793

Please sign in to comment.