Skip to content

Commit 6312029

Browse files
authored
Merge pull request swiftlang#28392 from CodaFi/a-captive-audience
Run checkPatternBindingCaptures on Extensions
2 parents ac1685e + c4f833e commit 6312029

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

lib/Sema/TypeCheckCaptures.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,12 @@ class FindCapturedVars : public ASTWalker {
4545
bool NoEscape, ObjC, IsGenericFunction;
4646

4747
public:
48-
FindCapturedVars(ASTContext &Context,
49-
SourceLoc CaptureLoc,
48+
FindCapturedVars(SourceLoc CaptureLoc,
5049
DeclContext *CurDC,
5150
bool NoEscape,
5251
bool ObjC,
5352
bool IsGenericFunction)
54-
: Context(Context), CaptureLoc(CaptureLoc), CurDC(CurDC),
53+
: Context(CurDC->getASTContext()), CaptureLoc(CaptureLoc), CurDC(CurDC),
5554
NoEscape(NoEscape), ObjC(ObjC), IsGenericFunction(IsGenericFunction) {}
5655

5756
CaptureInfo getCaptureInfo() const {
@@ -595,8 +594,7 @@ void TypeChecker::computeCaptures(AnyFunctionRef AFR) {
595594
isGeneric = (AFD->getGenericParams() != nullptr);
596595

597596
auto &Context = AFR.getAsDeclContext()->getASTContext();
598-
FindCapturedVars finder(Context,
599-
AFR.getLoc(),
597+
FindCapturedVars finder(AFR.getLoc(),
600598
AFR.getAsDeclContext(),
601599
AFR.isKnownNoEscape(),
602600
AFR.isObjC(),
@@ -613,8 +611,7 @@ void TypeChecker::computeCaptures(AnyFunctionRef AFR) {
613611
if (auto *AFD = AFR.getAbstractFunctionDecl()) {
614612
for (auto *P : *AFD->getParameters()) {
615613
if (auto E = P->getTypeCheckedDefaultExpr()) {
616-
FindCapturedVars finder(Context,
617-
E->getLoc(),
614+
FindCapturedVars finder(E->getLoc(),
618615
AFD,
619616
/*isNoEscape=*/false,
620617
/*isObjC=*/false,
@@ -663,10 +660,8 @@ static bool isLazy(PatternBindingDecl *PBD) {
663660
return false;
664661
}
665662

666-
void TypeChecker::checkPatternBindingCaptures(NominalTypeDecl *typeDecl) {
667-
auto &ctx = typeDecl->getASTContext();
668-
669-
for (auto member : typeDecl->getMembers()) {
663+
void TypeChecker::checkPatternBindingCaptures(IterableDeclContext *DC) {
664+
for (auto member : DC->getMembers()) {
670665
// Ignore everything other than PBDs.
671666
auto *PBD = dyn_cast<PatternBindingDecl>(member);
672667
if (!PBD) continue;
@@ -680,14 +675,15 @@ void TypeChecker::checkPatternBindingCaptures(NominalTypeDecl *typeDecl) {
680675
if (init == nullptr)
681676
continue;
682677

683-
FindCapturedVars finder(ctx,
684-
init->getLoc(),
685-
PBD->getInitContext(i),
678+
auto *DC = PBD->getInitContext(i);
679+
FindCapturedVars finder(init->getLoc(),
680+
DC,
686681
/*NoEscape=*/false,
687682
/*ObjC=*/false,
688683
/*IsGenericFunction*/false);
689684
init->walk(finder);
690685

686+
auto &ctx = DC->getASTContext();
691687
if (finder.getDynamicSelfCaptureLoc().isValid() && !isLazy(PBD)) {
692688
ctx.Diags.diagnose(finder.getDynamicSelfCaptureLoc(),
693689
diag::dynamic_self_stored_property_init);

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,6 +2265,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
22652265
for (Decl *Member : ED->getMembers())
22662266
visit(Member);
22672267

2268+
TypeChecker::checkPatternBindingCaptures(ED);
2269+
22682270
TypeChecker::checkConformancesInContext(ED, ED);
22692271

22702272
TypeChecker::checkDeclAttributes(ED);

lib/Sema/TypeChecker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ class TypeChecker final {
10461046
static void computeCaptures(AnyFunctionRef AFR);
10471047

10481048
/// Check for invalid captures from stored property initializers.
1049-
static void checkPatternBindingCaptures(NominalTypeDecl *typeDecl);
1049+
static void checkPatternBindingCaptures(IterableDeclContext *DC);
10501050

10511051
/// Change the context of closures in the given initializer
10521052
/// expression to the given context.

test/type/self.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ class C {
162162
}
163163
}
164164

165+
extension C {
166+
static var rdar57188331 = Self.staticFunc() // expected-error {{covariant 'Self' type cannot be referenced from a stored property initializer}} expected-error {{stored property cannot have covariant 'Self' type}}
167+
static var rdar57188331Var = ""
168+
static let rdar57188331Ref = UnsafeRawPointer(&Self.rdar57188331Var) // expected-error {{covariant 'Self' type cannot be referenced from a stored property initializer}}
169+
}
170+
171+
165172
struct S1 {
166173
typealias _SELF = Self
167174
let j = 99.1

0 commit comments

Comments
 (0)