Skip to content

Commit 6e7de5c

Browse files
committed
[TypeChecker] Avoid checking lazy property accessors if they haven't been synthesized yet
Ttheir 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. Resolves: rdar://129359362 Resolves: rdar://159463230 Resolves: swiftlang#84041
1 parent a80ba34 commit 6e7de5c

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,6 +2707,25 @@ class ExprAvailabilityWalker : public BaseDiagnosticWalker {
27072707
return;
27082708
}
27092709

2710+
// Avoid checking lazy property accessors if they haven't been
2711+
// synthesized yet, their availability is going to match the
2712+
// property itself. This is a workaround for lazy type-checking
2713+
// because synthesis of accessors for such properties requires
2714+
// a reference to `self` which won't be available because pattern
2715+
// binding for the variable was skipped.
2716+
//
2717+
// TODO: To fix this properly `getParentPatternBinding()`
2718+
// has to be refactored into a request to help establish association
2719+
// between a variable declaration and its pattern binding on demand
2720+
// instead of by using `typeCheckPatternBinding` called from the
2721+
// declaration checker.
2722+
if (D->getAttrs().hasAttribute<LazyAttr>()) {
2723+
auto *DC = D->getDeclContext();
2724+
if (DC->isTypeContext() && !(D->getAccessor(AccessorKind::Get) &&
2725+
D->getAccessor(AccessorKind::Set)))
2726+
return;
2727+
}
2728+
27102729
DeclAvailabilityFlags flags;
27112730
if (InInOutExpr)
27122731
flags |= DeclAvailabilityFlag::ForInout;

test/Inputs/lazy_typecheck.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,7 @@ extension PublicStruct {
402402
lhs = rhs
403403
}
404404
}
405+
406+
public class LazyPropertyWithClosureType {
407+
public lazy var lazyVar: Int = { 2 }()
408+
}

test/Inputs/lazy_typecheck_client.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,8 @@ func testOperators() {
142142
var a: PublicStruct
143143
a <<< PublicStruct(x: 2)
144144
}
145+
146+
func testLazyPropertyWithInitClosureReference(t: LazyPropertyWithClosureType) {
147+
_ = t.lazyVar
148+
t.lazyVar = 42
149+
}

0 commit comments

Comments
 (0)