Skip to content

Commit 0de372b

Browse files
authored
Merge pull request swiftlang#27139 from DougGregor/Self-in-lazy-init
2 parents ec862c5 + 1d6af84 commit 0de372b

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

lib/Sema/CSGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,11 +2478,11 @@ namespace {
24782478
}
24792479

24802480
Type visitDefaultArgumentExpr(DefaultArgumentExpr *expr) {
2481-
llvm_unreachable("Already type checked");
2481+
return expr->getType();
24822482
}
24832483

24842484
Type visitCallerDefaultArgumentExpr(CallerDefaultArgumentExpr *expr) {
2485-
llvm_unreachable("Already type checked");
2485+
return expr->getType();
24862486
}
24872487

24882488
Type visitApplyExpr(ApplyExpr *expr) {

lib/Sema/ConstraintSystem.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,6 +2880,9 @@ ConstraintSystem::getArgumentInfoLocator(ConstraintLocator *locator) {
28802880

28812881
Optional<ConstraintSystem::ArgumentInfo>
28822882
ConstraintSystem::getArgumentInfo(ConstraintLocator *locator) {
2883+
if (!locator)
2884+
return None;
2885+
28832886
if (auto *infoLocator = getArgumentInfoLocator(locator)) {
28842887
auto known = ArgumentInfos.find(infoLocator);
28852888
if (known != ArgumentInfos.end())

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)