Skip to content

Commit b78069e

Browse files
authored
Merge pull request swiftlang#38166 from DougGregor/nested-closure-self-5.5
Revert "Diagnose the implicit use of self in nested closures."
2 parents 0541613 + 0814ac7 commit b78069e

File tree

2 files changed

+6
-84
lines changed

2 files changed

+6
-84
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,31 +1509,14 @@ static void diagnoseImplicitSelfUseInClosure(const Expr *E,
15091509
Closures.push_back(ACE);
15101510
}
15111511

1512-
static bool isEnclosingSelfReference(VarDecl *var,
1513-
const AbstractClosureExpr *inClosure) {
1514-
if (var->isSelfParameter())
1515-
return true;
1516-
1517-
// Capture variables have a DC of the parent function.
1518-
if (inClosure && var->isSelfParamCapture() &&
1519-
var->getDeclContext() != inClosure->getParent())
1520-
return true;
1521-
1522-
return false;
1523-
}
1524-
15251512
/// Return true if this is an implicit reference to self which is required
15261513
/// to be explicit in an escaping closure. Metatype references and value
15271514
/// type references are excluded.
1528-
static bool isImplicitSelfParamUseLikelyToCauseCycle(Expr *E,
1529-
const AbstractClosureExpr *inClosure) {
1515+
static bool isImplicitSelfParamUseLikelyToCauseCycle(Expr *E) {
15301516
auto *DRE = dyn_cast<DeclRefExpr>(E);
15311517

1532-
if (!DRE || !DRE->isImplicit())
1533-
return false;
1534-
1535-
auto var = dyn_cast<VarDecl>(DRE->getDecl());
1536-
if (!var || !isEnclosingSelfReference(var, inClosure))
1518+
if (!DRE || !DRE->isImplicit() || !isa<VarDecl>(DRE->getDecl()) ||
1519+
!cast<VarDecl>(DRE->getDecl())->isSelfParameter())
15371520
return false;
15381521

15391522
// Defensive check for type. If the expression doesn't have type here, it
@@ -1616,7 +1599,7 @@ static void diagnoseImplicitSelfUseInClosure(const Expr *E,
16161599

16171600
SourceLoc memberLoc = SourceLoc();
16181601
if (auto *MRE = dyn_cast<MemberRefExpr>(E))
1619-
if (isImplicitSelfParamUseLikelyToCauseCycle(MRE->getBase(), ACE)) {
1602+
if (isImplicitSelfParamUseLikelyToCauseCycle(MRE->getBase())) {
16201603
auto baseName = MRE->getMember().getDecl()->getBaseName();
16211604
memberLoc = MRE->getLoc();
16221605
Diags.diagnose(memberLoc,
@@ -1626,7 +1609,7 @@ static void diagnoseImplicitSelfUseInClosure(const Expr *E,
16261609

16271610
// Handle method calls with a specific diagnostic + fixit.
16281611
if (auto *DSCE = dyn_cast<DotSyntaxCallExpr>(E))
1629-
if (isImplicitSelfParamUseLikelyToCauseCycle(DSCE->getBase(), ACE) &&
1612+
if (isImplicitSelfParamUseLikelyToCauseCycle(DSCE->getBase()) &&
16301613
isa<DeclRefExpr>(DSCE->getFn())) {
16311614
auto MethodExpr = cast<DeclRefExpr>(DSCE->getFn());
16321615
memberLoc = DSCE->getLoc();
@@ -1641,7 +1624,7 @@ static void diagnoseImplicitSelfUseInClosure(const Expr *E,
16411624
}
16421625

16431626
// Catch any other implicit uses of self with a generic diagnostic.
1644-
if (isImplicitSelfParamUseLikelyToCauseCycle(E, ACE))
1627+
if (isImplicitSelfParamUseLikelyToCauseCycle(E))
16451628
Diags.diagnose(E->getLoc(), diag::implicit_use_of_self_in_closure);
16461629

16471630
return { true, E };

test/expr/closure/closures.swift

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ func anonymousClosureArgsInClosureWithArgs() {
151151

152152
func doStuff(_ fn : @escaping () -> Int) {}
153153
func doVoidStuff(_ fn : @escaping () -> ()) {}
154-
func doVoidStuffNonEscaping(_ fn: () -> ()) {}
155154

156155
// <rdar://problem/16193162> Require specifying self for locations in code where strong reference cycles are likely
157156
class ExplicitSelfRequiredTest {
@@ -529,63 +528,3 @@ class SR3186 {
529528
print("\(v)")
530529
}
531530
}
532-
533-
// Apply the explicit 'self' rule even if it referrs to a capture, if
534-
// we're inside a nested closure
535-
class SR14120 {
536-
func operation() {}
537-
538-
func test1() {
539-
doVoidStuff { [self] in
540-
operation()
541-
}
542-
}
543-
544-
func test2() {
545-
doVoidStuff { [self] in
546-
doVoidStuff {
547-
// expected-error@+3 {{call to method 'operation' in closure requires explicit use of 'self'}}
548-
// expected-note@-2 {{capture 'self' explicitly to enable implicit 'self' in this closure}}
549-
// expected-note@+1 {{reference 'self.' explicitly}}
550-
operation()
551-
}
552-
}
553-
}
554-
555-
func test3() {
556-
doVoidStuff { [self] in
557-
doVoidStuff { [self] in
558-
operation()
559-
}
560-
}
561-
}
562-
563-
func test4() {
564-
doVoidStuff { [self] in
565-
doVoidStuff {
566-
self.operation()
567-
}
568-
}
569-
}
570-
571-
func test5() {
572-
doVoidStuff { [self] in
573-
doVoidStuffNonEscaping {
574-
operation()
575-
}
576-
}
577-
}
578-
579-
func test6() {
580-
doVoidStuff { [self] in
581-
doVoidStuff { [self] in
582-
doVoidStuff {
583-
// expected-error@+3 {{call to method 'operation' in closure requires explicit use of 'self'}}
584-
// expected-note@-2 {{capture 'self' explicitly to enable implicit 'self' in this closure}}
585-
// expected-note@+1 {{reference 'self.' explicitly}}
586-
operation()
587-
}
588-
}
589-
}
590-
}
591-
}

0 commit comments

Comments
 (0)