Skip to content

Can't compile while loop with i' #11371

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
catap opened this issue Feb 10, 2021 · 0 comments · Fixed by #11376
Closed

Can't compile while loop with i' #11371

catap opened this issue Feb 10, 2021 · 0 comments · Fixed by #11376

Comments

@catap
Copy link

catap commented Feb 10, 2021

Compiler version

I can reproduce it on: 3.0.0-M3-bin-20201213-e8d748e-NIGHTLY and 3.0.0-RC1-bin-20210122-6947b0f-NIGHTLY

Minimized code

object HelloWorld {
  def whileLoop: Int = {
    var i = 0
    var acc = 0
    while (i < 3) {
      var `i'` = 0
      while (`i'` < 4) {
        acc += (i * `i'`)
        `i'` += 1
      }
      i += 1
    }
    acc
  }

  def main(args: Array[String]): Unit = {
    println(s"hello world: ${whileLoop}")
  }
}

Output

-- Error: tests/pos/HelloWorld.scala:9:16 --------------------------------------
9 |        `i'` += 1
  |                ^
  |                end of statement expected but integer literal found
-- [E040] Syntax Error: tests/pos/HelloWorld.scala:10:6 ------------------------
10 |      }
   |      ^
   |      ';' expected, but '}' found
-- [E008] Not Found Error: tests/pos/HelloWorld.scala:8:12 ---------------------
8 |        acc += (i * `i'`)
  |        ^^^^^^
  |        value += is not a member of Int - did you mean Int.!=?
-- [E008] Not Found Error: tests/pos/HelloWorld.scala:9:8 ----------------------
8 |        acc += (i * `i'`)
9 |        `i'` += 1
  |               ^
  |              value i' is not a member of Int.
  |              Note that `i'` is treated as an infix operator in Scala 3.
  |              If you do not want that, insert a `;` or empty line in front
  |              or drop any spaces behind the operator.
-- [E006] Not Found Error: tests/pos/HelloWorld.scala:9:13 ---------------------
9 |        `i'` += 1
  |             ^^
  |             Not found: +=

longer explanation available when compiling with `-explain`
5 errors found

Expectation

Should compile and print hello world: 18

Workaround

Replace lines like:

 `i'` += 1

to

 `i'` = `i'` + 1
@odersky odersky linked a pull request Feb 10, 2021 that will close this issue
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants