Skip to content

Commit 041c685

Browse files
committed
Simplify diagnostics
1 parent 684c908 commit 041c685

File tree

2 files changed

+30
-41
lines changed

2 files changed

+30
-41
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,16 +1849,14 @@ class ImplicitSelfUsageChecker : public BaseDiagnosticWalker {
18491849
return selfDeclAllowsImplicitSelf510(DRE, ty, inClosure);
18501850

18511851
return selfDeclAllowsImplicitSelf(DRE->getDecl(), ty, inClosure,
1852-
/*validateParentClosures:*/ true,
1853-
/*validatingNestedClosure:*/ false);
1852+
/*isValidatingParentClosures:*/ false);
18541853
}
18551854

18561855
/// Whether or not implicit self is allowed for this implicit self decl
18571856
static bool selfDeclAllowsImplicitSelf(const ValueDecl *selfDecl,
18581857
const Type captureType,
18591858
const AbstractClosureExpr *inClosure,
1860-
bool validateParentClosures,
1861-
bool validatingNestedClosure) {
1859+
bool isValidatingParentClosures) {
18621860
ASTContext &ctx = inClosure->getASTContext();
18631861

18641862
auto requiresSelfQualification =
@@ -1919,7 +1917,14 @@ class ImplicitSelfUsageChecker : public BaseDiagnosticWalker {
19191917
// isn't unwrapped correctly in the nested closure, we would have
19201918
// already noticed when validating that closure.
19211919
if (closureHasWeakSelfCapture(inClosure) &&
1922-
!selfDeclDefinedInConditionalStmt && !validatingNestedClosure) {
1920+
!selfDeclDefinedInConditionalStmt && !isValidatingParentClosures) {
1921+
return false;
1922+
}
1923+
1924+
// If the self decl refers to a weak capture in a parent closure,
1925+
// then implicit self is not allowed.
1926+
if (selfDecl->getInterfaceType()->is<WeakStorageType>() &&
1927+
!closureHasWeakSelfCapture(inClosure)) {
19231928
return false;
19241929
}
19251930

@@ -1945,7 +1950,7 @@ class ImplicitSelfUsageChecker : public BaseDiagnosticWalker {
19451950
// - We have to do this for all closures, even closures that typically
19461951
// don't require self qualificationm since an invalid self capture in
19471952
// a parent closure still disallows implicit self in a nested closure.
1948-
if (validateParentClosures) {
1953+
if (!isValidatingParentClosures) {
19491954
return !implicitSelfDisallowedDueToInvalidParent(selfDecl, captureType,
19501955
inClosure);
19511956
} else {
@@ -2043,25 +2048,6 @@ class ImplicitSelfUsageChecker : public BaseDiagnosticWalker {
20432048
return nullptr;
20442049
}
20452050

2046-
// If this closure has a non-weak capture, the parent closure
2047-
// has a weak capture, the implicit self decl refers directly
2048-
// to this closure's self capture, and that capture directly
2049-
// refers to the parent closure's weak self capture, then
2050-
// this is invalid.
2051-
// - Normally this is rejected by the type checker because
2052-
// self is optional here, but that doesn't happen when calling
2053-
// a method defined on `Optional<Self>`.
2054-
if (!closureHasWeakSelfCapture(inClosure)) {
2055-
if (auto closureSelfCapture = selfCapture(inClosure)) {
2056-
if (auto parentWeakSelfCapture = weakSelfCapture(outerClosure)) {
2057-
if (selfDecl == closureSelfCapture &&
2058-
parentWeakSelfCapture == outerSelfDecl) {
2059-
return outerClosure;
2060-
}
2061-
}
2062-
}
2063-
}
2064-
20652051
// Check if this closure contains the self decl.
20662052
// - If the self decl is defined in the closure's body, its
20672053
// decl context will be the closure itself.
@@ -2092,8 +2078,7 @@ class ImplicitSelfUsageChecker : public BaseDiagnosticWalker {
20922078
// is sufficient to enable implicit self.
20932079
if (!selfDeclAllowsImplicitSelf(outerSelfDecl, captureType,
20942080
outerClosure,
2095-
/*validateParentClosures:*/ false,
2096-
/*validatingNestedClosure:*/ true)) {
2081+
/*isValidatingParentClosures:*/ true)) {
20972082
return outerClosure;
20982083
}
20992084

@@ -2106,8 +2091,7 @@ class ImplicitSelfUsageChecker : public BaseDiagnosticWalker {
21062091
// parent closures, we don't need to do that separate for this closure.
21072092
if (validateIntermediateParents) {
21082093
if (!selfDeclAllowsImplicitSelf(selfDecl, captureType, outerClosure,
2109-
/*validateParentClosures*/ false,
2110-
/*validatingNestedClosure*/ true)) {
2094+
/*isValidatingParentClosures*/ true)) {
21112095
return outerClosure;
21122096
}
21132097
}

test/expr/closure/closures_swift6.swift

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -501,14 +501,14 @@ class TestGithubIssue70089 {
501501
}
502502

503503
doVoidStuff { [weak self] in
504-
doVoidStuff { [self] in
505-
x += 1 // expected-error@-1 {{value of optional type 'TestGithubIssue70089?' must be unwrapped to a value of type 'TestGithubIssue70089'}} expected-note@-1 {{coalesce using '??' to provide a default when the optional value contains 'nil'}} expected-note@-1 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
504+
doVoidStuff { [self] in // expected-error {{value of optional type 'TestGithubIssue70089?' must be unwrapped to a value of type 'TestGithubIssue70089'}} expected-note {{coalesce using '??' to provide a default when the optional value contains 'nil'}} expected-note {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
505+
x += 1
506506
}
507507
}
508508

509509
doVoidStuff { [weak self] in
510-
doVoidStuff { [self] in //
511-
self.x += 1 // expected-error@-1 {{value of optional type 'TestGithubIssue70089?' must be unwrapped to a value of type 'TestGithubIssue70089'}} expected-note@-1 {{coalesce using '??' to provide a default when the optional value contains 'nil'}} expected-note@-1 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
510+
doVoidStuff { [self] in // expected-error {{value of optional type 'TestGithubIssue70089?' must be unwrapped to a value of type 'TestGithubIssue70089'}} expected-note {{coalesce using '??' to provide a default when the optional value contains 'nil'}} expected-note {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
511+
self.x += 1
512512
}
513513
}
514514

@@ -939,8 +939,6 @@ extension TestExtensionOnOptionalSelf? {
939939
func foo() {
940940
_ = { [weak self] in
941941
foo() // expected-error {{implicit use of 'self' in closure; use 'self.' to make capture semantics explicit}}
942-
self.foo()
943-
self?.bar()
944942
}
945943

946944
_ = {
@@ -951,15 +949,15 @@ extension TestExtensionOnOptionalSelf? {
951949

952950
_ = { [weak self] in
953951
_ = {
954-
foo()
952+
foo() // expected-error {{implicit use of 'self' in closure; use 'self.' to make capture semantics explicit}}
955953
self.foo()
956954
self?.bar()
957955
}
958956
}
959957

960958
_ = { [weak self] in
961959
_ = { [self] in
962-
foo() // expected-error {{call to method 'foo' in closure requires explicit use of 'self' to make capture semantics explicit}}
960+
foo()
963961
self.foo()
964962
self?.bar()
965963
}
@@ -983,22 +981,22 @@ extension TestExtensionOnOptionalSelf {
983981

984982
_ = { [weak self] in
985983
_ = {
986-
foo()
984+
foo() // expected-error {{implicit use of 'self' in closure; use 'self.' to make capture semantics explicit}}
987985
self.foo()
988986
}
989987
}
990988

991989
_ = { [weak self] in
992990
_ = { [self] in
993-
foo() // expected-error {{call to method 'foo' in closure requires explicit use of 'self' to make capture semantics explicit}}
991+
foo()
994992
self.foo()
995993
}
996994
}
997995

998996
_ = { [weak self] in
999997
_ = { [self] in
1000998
_ = { [self] in
1001-
foo() // expected-error {{call to method 'foo' in closure requires explicit use of 'self' to make capture semantics explicit}}
999+
foo()
10021000
self.foo()
10031001
}
10041002
}
@@ -1007,11 +1005,18 @@ extension TestExtensionOnOptionalSelf {
10071005
_ = { [weak self] in
10081006
doVoidStuffNonEscaping {
10091007
_ = { [self] in
1010-
foo() // expected-error {{call to method 'foo' in closure requires explicit use of 'self' to make capture semantics explicit}}
1008+
foo()
10111009
self.foo()
10121010
}
10131011
}
10141012
}
1013+
1014+
_ = { [weak self] in
1015+
guard case let self = self else { return }
1016+
_ = { [self] in
1017+
foo()
1018+
}
1019+
}
10151020
}
10161021
}
10171022

0 commit comments

Comments
 (0)