diff --git a/lib/Sema/TypeCheckAvailability.cpp b/lib/Sema/TypeCheckAvailability.cpp index 88e26d1179bc4..ee97470c1d97b 100644 --- a/lib/Sema/TypeCheckAvailability.cpp +++ b/lib/Sema/TypeCheckAvailability.cpp @@ -2608,6 +2608,25 @@ class ExprAvailabilityWalker : public BaseDiagnosticWalker { return; } + // Avoid checking lazy property accessors if they haven't been + // synthesized yet, their availability is going to match the + // property itself. This is a workaround for lazy type-checking + // because synthesis of accessors for such properties requires + // a reference to `self` which won't be available because pattern + // binding for the variable was skipped. + // + // TODO: To fix this properly `getParentPatternBinding()` + // has to be refactored into a request to help establish association + // between a variable declaration and its pattern binding on demand + // instead of by using `typeCheckPatternBinding` called from the + // declaration checker. + if (D->getAttrs().hasAttribute()) { + auto *DC = D->getDeclContext(); + if (DC->isTypeContext() && !(D->getAccessor(AccessorKind::Get) && + D->getAccessor(AccessorKind::Set))) + return; + } + // Check availability of accessor functions. // TODO: if we're talking about an inlineable storage declaration, // this probably needs to be refined to not assume that the accesses are diff --git a/test/Inputs/lazy_typecheck.swift b/test/Inputs/lazy_typecheck.swift index 553a8f82d6b25..02acffa628a52 100644 --- a/test/Inputs/lazy_typecheck.swift +++ b/test/Inputs/lazy_typecheck.swift @@ -378,3 +378,7 @@ extension PublicStruct { lhs = rhs } } + +public class LazyPropertyWithClosureType { + public lazy var lazyVar: Int = { 2 }() +} diff --git a/test/Inputs/lazy_typecheck_client.swift b/test/Inputs/lazy_typecheck_client.swift index 8f251d5867476..1dbfc33716921 100644 --- a/test/Inputs/lazy_typecheck_client.swift +++ b/test/Inputs/lazy_typecheck_client.swift @@ -142,3 +142,8 @@ func testOperators() { var a: PublicStruct a <<< PublicStruct(x: 2) } + +func testLazyPropertyWithInitClosureReference(t: LazyPropertyWithClosureType) { + _ = t.lazyVar + t.lazyVar = 42 +}