Skip to content

[6.0] [SE-0365] Fix issue where implicit self was unexpectedly disallowed in nested closures #73482

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

Merged
merged 1 commit into from
May 28, 2024

Conversation

calda
Copy link
Contributor

@calda calda commented May 7, 2024

Explanation

This PR fixes issues related to SE-0365 and SE-0269 where the following code would unexpectedly fail to compile:

class Test {
  var x = 0

  func test() {
    withEscapingClosure { [weak self] in
      guard let self else { return }
            
      withEscapingClosure { [self] in
        x += 1 // unexpected error in 5.10: reference to property 'x' in closure requires explicit use of 'self' to make capture semantics explicit
      }

      withNonEscapingClosure {
        x += 1 // unexpected error in 5.10: reference to property 'x' in closure requires explicit use of 'self' to make capture semantics explicit
      }
    }
  }
}

func withEscapingClosure(_: @escaping () -> Void) {}
func withNonEscapingClosure(_: () -> Void) {}

Scope

This change fixes several bugs related to SE-0365 and SE-0269. This bug fixes are important usability improvements to using nested closures, and match the expected behavior documented in those proposals.

Most relevant to Swift 6, this change also includes other minor bug fixes that are source breaking, where invalid code was previously allowed to compile. Examples include:

class Test {
  var x = 0
  static var staticOptional: Test? = Test()

  func testInvalidCodeUnexpectedlyAllowed() {
    withEscapingClosure { [weak self = Test()] in
      guard let self else { return }
      x += 1 // unexpectedly allowed in 5.10, now a warning in Swift 5 mode and an error in Swift 6 mode
    }

    withEscapingClosure { [self = Test()] in
      withEscapingClosure { [weak self] in
        guard let self else { return }
        x += 1 // unexpectedly allowed in 5.10, now a warning in Swift 5 mode and an error in Swift 6 mode
      }
    }

    withEscapingClosure { [weak self] in
      guard let self = self ?? .staticOptional else { return }
      withEscapingClosure { [weak self] in
        guard let self else { return }
        x += 1 // unexpectedly allowed in 5.10, now a warning in Swift 5 mode and an error in Swift 6 mode
      }
    }

    let `self` = Test()
    withEscapingClosure { [weak self] in
      guard let self else { return }
      x += 1 // unexpectedly allowed in 5.10, now a warning in Swift 5 mode and an error in Swift 6 mode
    }
  }
}

func withEscapingClosure(_: @escaping () -> Void) {}

All of these examples are supposed to be rejected, but are currently accepted. This PR fixes those issues by making them emit a warning in Swift 5 mode, and be an error in Swift 6 mode.

If we don't land this change in Swift 6, we won't be able to fully fix those issues until the next major language mode.

Risk

The main risk is that this change could unintentionally introduce a regression in edge cases that we have not found yet. If there are regressions and we don't catch them before the release of Swift 6, we may not be able to fix the regression until Swift 7 (if the bug fix is source breaking, e.g. because the bug accidentally permits code that should be rejected). The best mitigation approach here is thorough testing:

Testing

We have extensively tested this change in both the Swift 5 and Swift 6 language modes. We added a large number of new test cases for basically every combination of nested closure varieties that we could think of. @hamishknight in particular was very helpful in coming up with even more cases that needed test coverage.

In addition to these new tests, all of the existing tests pass, including the source compatibility suite.

Reviewers

Please review:

Thanks!

@hamishknight
Copy link
Contributor

@swift-ci please test

@calda
Copy link
Contributor Author

calda commented May 20, 2024

Hi @hborla, are you the best person to review this Swift 6 release PR?

Copy link
Member

@hborla hborla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thank you for fixing this!

@hborla
Copy link
Member

hborla commented May 20, 2024

@swift-ci please test

@hborla
Copy link
Member

hborla commented May 21, 2024

@swift-ci please test Linux

@calda
Copy link
Contributor Author

calda commented May 28, 2024

Thanks @hborla! Is this ready to be merged?

@hborla
Copy link
Member

hborla commented May 28, 2024

Yep!

@hborla hborla merged commit e37ba18 into swiftlang:release/6.0 May 28, 2024
5 checks passed
slavapestov added a commit to slavapestov/swift that referenced this pull request Jun 6, 2024
This fixes a regression from swiftlang#73482.

Fixes rdar://problem/129255769.
slavapestov added a commit to slavapestov/swift that referenced this pull request Jun 6, 2024
This fixes a regression from swiftlang#73482.

Fixes rdar://problem/129255769.
slavapestov added a commit to slavapestov/swift that referenced this pull request Jun 6, 2024
This fixes a regression from swiftlang#73482.

Fixes rdar://problem/129255769.
ktoso pushed a commit to ktoso/swift that referenced this pull request Jun 14, 2024
This fixes a regression from swiftlang#73482.

Fixes rdar://problem/129255769.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants