Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<LazyAttr>()) {
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
Expand Down
4 changes: 4 additions & 0 deletions test/Inputs/lazy_typecheck.swift
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,7 @@ extension PublicStruct {
lhs = rhs
}
}

public class LazyPropertyWithClosureType {
public lazy var lazyVar: Int = { 2 }()
}
5 changes: 5 additions & 0 deletions test/Inputs/lazy_typecheck_client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,8 @@ func testOperators() {
var a: PublicStruct
a <<< PublicStruct(x: 2)
}

func testLazyPropertyWithInitClosureReference(t: LazyPropertyWithClosureType) {
_ = t.lazyVar
t.lazyVar = 42
}