Allow while loop after do statement?

I just bumped into some strange unexpected errors that took me a while to understand. The situation can be simplified as follows.

This program compiles (as expected):

func foo() {
  do {
    print("A")
  }
  print("B")
  while true {
    print("C")
    break
  }
}

Removing print("B") will result in three (unexpected and nonsensical) compile time errors:

func foo() {
  do { // ERROR: 'do-while' statement is not allowed; use 'repeat-while' instead
    print("A")
  }
  while true { // ERROR: Closure expression is unused
    print("C")
    break // ERROR: break' is only allowed inside a loop, if, do, or switch
  }
}

I guess the first error was introduced to aid migration from Swift's old do-while loop to the current repeat-while loop, but that was a long time ago, so perhaps it's time to let the latter version of the program compile now?

7 Likes

Definitely, this is a bug. Thanks for bringing it to our attention!

11 Likes

This bug is fixed in the Swift 5.3 snapshots.

6 Likes