Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gsps opened this issue Jun 6, 2016 · 3 comments
Closed

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

gsps opened this issue Jun 6, 2016 · 3 comments

Comments

@gsps
Copy link
Contributor

gsps commented Jun 6, 2016

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)

@odersky
Copy link
Contributor

odersky commented Jun 6, 2016

Thanks for pointing this out! That's indeed the right fix. Would you like to do a PR with it?

jvican added a commit to dotty-jvican/dotty that referenced this issue Jul 1, 2016
DarkDimius added a commit that referenced this issue Jul 1, 2016
@nicolasstucki
Copy link
Contributor

@jvican is this issue completely fixed?

@jvican
Copy link
Member

jvican commented Jul 1, 2016

Yes, I forgot to add the test in the PR, I will tomorrow.

@odersky odersky closed this as completed Jul 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants