Skip to content

Else branches carry the If's rather than their own position #1308

Closed
@gsps

Description

@gsps

Here's a test that can be dropped in TreeInfoTest.scala (for the lack of a more appropriate existing test suite) to demonstrate the faulty behavior. In particular, ifTree.pos == elsep.pos holds.

@Test
def testElseBranchPosition = 
  checkCompile("frontend", "class A { def f(x: Int): Boolean = if (x < 0) true else false}") {
    (tree, context) =>
      implicit val ctx = context
      val fTree   = tree.find(tree => tree.symbol.name == termName("f")).get.asInstanceOf[DefDef]
      val ifTree  = fTree.rhs.asInstanceOf[If]
      val (cond, thenp, elsep) = (ifTree.cond, ifTree.thenp, ifTree.elsep)
      assertTrue(ifTree.pos.start < cond.pos.start)
      assertTrue(cond.pos.start   < thenp.pos.start)
      assertTrue(thenp.pos.start  < elsep.pos.start)
  }

The problem originates from the typedIf(...) in Typer overriding the else branch's position even when the else branch was provided by the user. Unless I'm mistaken, this should be the two-character fix:
val elsep1 = typed(tree.elsep orElse(untpd.unitLiteral withPos tree.pos), pt)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions