Parsing conditionals

Using Scala 3.4.0, this works fine:

import scala.math.Ordering.Implicits.infixOrderingOps

if ((1, 2) < (2, 3)) then println("T")

However, this cannot be parsed:

import scala.math.Ordering.Implicits.infixOrderingOps

if (1, 2) < (2, 3) then println("T")

Is that an overlooked case in the parser or something I need to learn to live with as a limitation of the grammar (which needs to work with two syntaxes for conditionals)?
(Also, to add insult to injury, IDEA flags the additional parentheses as unnecessary…)

1 Like

Looks like a bug… (1, 2) < (2, 3) by itself returns true. It also works for other infix stuff like if x contains y+1 then println("T").

This works:

@main def test() = println:
  if ((1, 2)) < (2, 3) then "T" else "U"

To accommodate the obsolete syntax, it takes an expression starting with the paren, then checks if there is more expression after that.

I was going to guess that it trips up on < for XML syntax, but it seems to stumble on the comma.

It’s a bug. Fixed in Fix parsing of conditional expressions in parentheses by odersky · Pull Request #19985 · scala/scala3 · GitHub.

6 Likes

You have to wake up pretty early in the morning to contribute parser fixes, especially if local time is not Odersky time.

1 Like