Skip to content

[TypeChecker] Improve error about mutating self capture by escaping c… #20847

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
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2944,8 +2944,11 @@ ERROR(closure_implicit_capture_without_noescape,none,
"escaping closures can only capture inout parameters explicitly by value",
())
ERROR(closure_implicit_capture_mutating_self,none,
"closure cannot implicitly capture a mutating self parameter",
"escaping closure cannot capture a mutating self parameter",
())
NOTE(create_mutating_copy_or_capture_self,none,
"create a mutating copy of self, or explicitly capture self for immutability",
())
ERROR(nested_function_with_implicit_capture_argument,none,
"nested function with %select{an |}0implicitly captured inout "
"parameter%select{|s}0 can only be used as a non-escaping argument", (bool))
Expand Down
4 changes: 3 additions & 1 deletion lib/Sema/TypeCheckCaptures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ class FindCapturedVars : public ASTWalker {
if (isInOut && !AFR.isKnownNoEscape() && !isNested) {
if (D->getBaseName() == D->getASTContext().Id_self) {
TC.diagnose(DRE->getLoc(),
diag::closure_implicit_capture_mutating_self);
diag::closure_implicit_capture_mutating_self);
TC.diagnose(DRE->getLoc(),
diag::create_mutating_copy_or_capture_self);
} else {
TC.diagnose(DRE->getLoc(),
diag::closure_implicit_capture_without_noescape);
Expand Down
23 changes: 22 additions & 1 deletion test/Sema/diag_invalid_inout_captures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ func do_escape(_ not_a_price_you_are_willing_to_pay: @escaping () -> ()) {}
struct you_cry_in_your_tea {
mutating func which_you_hurl_in_the_sea_when_you_see_me_go_by() {
no_escape { _ = self } // OK
do_escape { _ = self } // expected-error {{closure cannot implicitly capture a mutating self parameter}}
do_escape { _ = self } // expected-error {{escaping closure cannot capture a mutating self parameter}}
// expected-note@-1 {{create a mutating copy of self, or explicitly capture self for immutability}}
do_escape {
[self] in
_ = self // OK
Expand Down Expand Up @@ -40,3 +41,23 @@ func remember(line: inout String) -> () -> Void {
func its_complicated(condition: inout Int) {
no_escape(condition == 0 ? { condition = 1 } : { condition = 2}) // expected-error 2 {{escaping closures can only capture inout parameters explicitly by value}}
}

// rdar://problem/46322943 - Improve error message about mutating self capture in escaping closures
func rdar46322943() {
struct S {
var bar = 1

func foo(_ fn: () -> Void) {
fn()
}

mutating func main() {
let fn = {
self.bar += 1
// expected-error@-1 {{escaping closure cannot capture a mutating self parameter}}
// expected-note@-2 {{create a mutating copy of self, or explicitly capture self for immutability}}
}
self.foo(fn)
}
}
}