-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instead of parisng identifiers and fallthrough to self, treat `kw_self` the same as identifiers. This enables binding to self.
- Loading branch information
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// RUN: %target-typecheck-verify-swift | ||
|
||
class Writer { | ||
private var articleWritten = 47 | ||
|
||
func stop() { | ||
let rest: () -> Void = { [weak self] in | ||
let articleWritten = self?.articleWritten ?? 0 | ||
guard let `self` = self else { | ||
return | ||
} | ||
|
||
self.articleWritten = articleWritten | ||
} | ||
|
||
fatalError("I'm running out of time") | ||
rest() | ||
} | ||
|
||
func nonStop() { | ||
let write: () -> Void = { [weak self] in | ||
self?.articleWritten += 1 | ||
|
||
if let self = self { | ||
self.articleWritten += 1 | ||
} | ||
|
||
if let `self` = self { | ||
self.articleWritten += 1 | ||
} | ||
|
||
guard let self = self else { | ||
return | ||
} | ||
|
||
self.articleWritten += 1 | ||
} | ||
|
||
write() | ||
} | ||
} | ||
|
||
struct T { | ||
var mutable: Int = 0 | ||
func f() { | ||
// expected-error @+2 {{keyword 'self' cannot be used as an identifier here}} | ||
// expected-note @+1 {{if this name is unavoidable, use backticks to escape it}} | ||
let self = self | ||
} | ||
} | ||
|
||
class MyCls { | ||
func something() {} | ||
|
||
func test() { | ||
// expected-warning @+1 {{initialization of immutable value 'self' was never used}} | ||
let `self` = Writer() // Even if `self` is shadowed, | ||
something() // this should still refer `MyCls.something`. | ||
} | ||
} |