scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
230 stars 21 forks source link

Strange compiler error on lines starting with boolean operators in Scala 2.13.13 with -Xsource:3 #12972

Closed ex-ratt closed 3 months ago

ex-ratt commented 3 months ago

The following code compiles and runs with Scala 2.13.12 and -Xsource:3 compiler flag (we use this flag for nicer syntax, e.g. to allow line breaks before infix operators):

object Main {
  def main(args: Array[String]): Unit = {
    val num1 = 1
    val num2 = 2
    val num3 = 3
    val sum =
      num1
        + num2
        + num3

    val bool1 = true
    val bool2 = false
    val bool3 = true
    val conjunction =
      bool1
        && bool2
        && bool3

    println(s"$sum $conjunction")
  }
}

With Scala 2.13.13, there are errors on the lines starting with operators. Some of them can be demoted to a warning with -Wconf:cat=scala3-migration:w or -Xmigration, but the boolean operators still won't work:

Main.scala:16:9
not found: value &&
        && bool2

Main.scala:17:9
not found: value &&
        && bool3

Main.scala:17:12
postfix operator bool3 needs to be enabled
by making the implicit value scala.language.postfixOps visible.
This can be achieved by adding the import clause 'import scala.language.postfixOps'
or by setting the compiler option -language:postfixOps.
See the Scaladoc for value scala.language.postfixOps for a discussion
why the feature needs to be explicitly enabled.

Is there any reason why the boolean operators should not work? I would have assumed that all operators work and not just specific ones, therefore I suspect a bug. But maybe I have missed something?

PS: We have the same problem with org.http4s.QueryOps.+? (in another project), so it seems like it isn't confined to boolean operators.

PPS: -Xsource:3-cross works by the way. With Scala 3.3.3, the above code also works.

som-snytt commented 3 months ago

The help text indicates that leading infix is available only under -Xsource:3-cross. This was changed to offer more control over migration (of projects to Scala 3). In Scala 2.13.14, -Xsource-features will allow more fine-grained opt-in to these features.

➜  scalac -Xsource:help
-Xsource:3 is for migrating a codebase, -Xsource:3-cross is for cross-building.

-Xsource:3 issues migration warnings in category `cat=scala3-migration`,
  which by default are promoted to errors under the `-Wconf` configuration.
  Examples of promoted warnings:
  * Implicit definitions must have an explicit type
  * (x: Any) + "" is deprecated
  * Args not adapted to unit value
  * Member classes cannot shadow a same-named class defined in a parent
  * Presence or absence of parentheses in overrides must match exactly

Certain benign syntax features are enabled:
  * case C(xs*) =>
  * A & B type intersection
  * import p.*
  * import p.m as n
  * import p.{given, *}
  * Eta-expansion `x.m` of methods without trailing `_`

The following constructs emit a migration warning under -Xsource:3. With
-Xsource:3-cross the semantics change to match Scala 3 and no warning is issued.
  * Unicode escapes in raw interpolations and triple-quoted strings
  * Leading infix operators continue the previous line
  * Interpolator must be selectable from `scala.StringContext`
  * Case class copy and apply have the same access modifier as the constructor
  * The inferred type of an override is taken from the member it overrides
SethTisue commented 3 months ago

references:

som-snytt commented 3 months ago

There is mention on https://github.com/scala/scala/releases/tag/v2.13.13 but I'm not sure how to add big red letters. This confusion has come up on chat as well.

Normally -X means "stable option", and -Xsource:N has specific meaning, although -Xsource:future has always meant an evolving target. Since Scala 2 has no future versions, the semantics here is a gray area. The breakage is in the service of the migration experience.