Skip to content

Commit 1d6af84

Browse files
committed
[Type checker] Allow 'Self' reference in a lazy initializer.
Fixes rdar://problem/51561208
1 parent c109f90 commit 1d6af84

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/Sema/TypeCheckCaptures.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,12 @@ void TypeChecker::computeCaptures(AnyFunctionRef AFR) {
645645
}
646646
}
647647

648+
static bool isLazy(PatternBindingDecl *PBD) {
649+
if (auto var = PBD->getSingleVar())
650+
return var->getAttrs().hasAttribute<LazyAttr>();
651+
return false;
652+
}
653+
648654
void TypeChecker::checkPatternBindingCaptures(NominalTypeDecl *typeDecl) {
649655
auto &ctx = typeDecl->getASTContext();
650656

@@ -669,7 +675,7 @@ void TypeChecker::checkPatternBindingCaptures(NominalTypeDecl *typeDecl) {
669675
/*ObjC=*/false);
670676
init->walk(finder);
671677

672-
if (finder.getDynamicSelfCaptureLoc().isValid()) {
678+
if (finder.getDynamicSelfCaptureLoc().isValid() && !isLazy(PBD)) {
673679
ctx.Diags.diagnose(finder.getDynamicSelfCaptureLoc(),
674680
diag::dynamic_self_stored_property_init);
675681
}

test/type/self.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,13 @@ class SelfStoredPropertyInit {
253253
static func myValue() -> Int { return 123 }
254254

255255
var value = Self.myValue() // expected-error {{covariant 'Self' type cannot be referenced from a stored property initializer}}
256-
}
256+
}
257+
258+
// rdar://problem/55273931 - erroneously rejecting 'Self' in lazy initializer
259+
class Foo {
260+
static var value: Int = 17
261+
262+
lazy var doubledValue: Int = {
263+
Self.value * 2
264+
}()
265+
}

0 commit comments

Comments
 (0)