Should this misleading warning be here?

func foo() -> Int {

  return _E.bar

  struct _E { // WARNING: Code after 'return' will never be executed
    private static var count = 0
    static var bar: Int {
      count += (Bool.random() ? foo() : 1)
      return count
    }
  }

}


for _ in 0 ..< 7 { print(foo()) }

(The program compiles and works as intended, printing eg:

1
4
80
162
1304
1305
2612

)

2 Likes

Nobody tests nested stuff after what uses it, so this hasn't been dealt with yet. We're in a weird, inconsistent place at the moment.

func ƒ1() {
  return void() // Error: Use of local variable 'void' before its declaration
  func void() { }
}

That's …not a variable. :thinking:

func ƒ2() {
  return Nested()()

  struct Nested { // Warning: Code after 'return' will never be executed
    func callAsFunction() { }
  }
}

It looks like a bug to me. I would recommend filing at report on bugs.swift.org

2 Likes

Filed SR-13639.

2 Likes
Terms of Service

Privacy Policy

Cookie Policy