Files
swift-mirror/test/expr/closure/implicit_weak_capture.swift
Hamish Knight fe272e6cdd [Sema] Restore 5.10 implicit self behavior prior to Swift 6 mode
Unfortunately we've encountered another source
breaking case here:

```
class C {
  func method() {}

  func foo() {
    Task { [weak self] in
      Task {
        method()
      }
    }
  }
}
```

In 5.10 we'd only do the unqualified lookup for
`self` when directly in a `weak self` closure,
but with the implicit self rework, we'd start
using the `weak self` here, leading to a
type-checker error.

At this point, adding more edge cases to the
existing logic is going to make things much more
complicated. Instead, reinstate the 5.10 implicit
self lookup behavior and diagnostic logic,
switching over to the new logic only under Swift 6
mode.

rdar://129475277
2024-06-10 20:29:16 +01:00

50 lines
1.2 KiB
Swift

// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking -swift-version 6)
// REQUIRES: concurrency
// REQUIRES: executable_test
// rdar://102155748
// UNSUPPORTED: back_deployment_runtime
func runIn10ms(_ closure: @escaping @Sendable () -> Void) {
Task {
try! await Task.sleep(nanoseconds: 10_000_000)
closure()
}
}
final class Weak: Sendable {
let property = "Self exists"
func test() {
runIn10ms { [weak self] in
if let self {
// Use implicit self -- this should not result in a strong capture
_ = property
fatalError("Self was unexpectedly captured strongly")
} else {
print("Self was captured weakly (1)")
}
}
runIn10ms { [weak self] in
guard let self else {
print("Self was captured weakly (2)")
return
}
// Use implicit self -- this should not result in a strong capture
_ = property
runIn10ms { [self] in
// Use implicit self -- this should not result in a strong capture
_ = property
fatalError("Self was unexpectedly captured strongly")
}
}
}
}
Weak().test()
try await Task.sleep(nanoseconds: 30_000_000)