Skip to content

Commit 778a530

Browse files
authored
Merge pull request #27151 from DougGregor/Self-in-lazy-init-5.1-08-28-2019
[5.1 08-28-2019] [Type checker] Allow 'Self' reference in a lazy initializer.
2 parents ddfc441 + 06887ff commit 778a530

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/Sema/TypeCheckCaptures.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,12 @@ void TypeChecker::computeCaptures(AnyFunctionRef AFR) {
769769
maybeDiagnoseCaptures(expr, AFR);
770770
}
771771

772+
static bool isLazy(PatternBindingDecl *PBD) {
773+
if (auto var = PBD->getSingleVar())
774+
return var->getAttrs().hasAttribute<LazyAttr>();
775+
return false;
776+
}
777+
772778
void TypeChecker::checkPatternBindingCaptures(NominalTypeDecl *typeDecl) {
773779
for (auto member : typeDecl->getMembers()) {
774780
// Ignore everything other than PBDs.
@@ -791,7 +797,7 @@ void TypeChecker::checkPatternBindingCaptures(NominalTypeDecl *typeDecl) {
791797
/*ObjC=*/false);
792798
init->walk(finder);
793799

794-
if (finder.getDynamicSelfCaptureLoc().isValid()) {
800+
if (finder.getDynamicSelfCaptureLoc().isValid() && !isLazy(PBD)) {
795801
diagnose(finder.getDynamicSelfCaptureLoc(),
796802
diag::dynamic_self_stored_property_init);
797803
}

test/type/self.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,12 @@ class SelfStoredPropertyInit {
183183

184184
var value = Self.myValue() // expected-error {{covariant 'Self' type cannot be referenced from a stored property initializer}}
185185
}
186+
187+
// rdar://problem/55273931 - erroneously rejecting 'Self' in lazy initializer
188+
class Foo {
189+
static var value: Int = 17
190+
191+
lazy var doubledValue: Int = {
192+
Self.value * 2
193+
}()
194+
}

0 commit comments

Comments
 (0)