Skip to content

Commit d91b464

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: #84041 (cherry picked from commit 6e7de5c)
1 parent 7885b07 commit d91b464

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
@@ -2608,6 +2608,25 @@ class ExprAvailabilityWalker : public BaseDiagnosticWalker {
26082608
return;
26092609
}
26102610

2611+
// Avoid checking lazy property accessors if they haven't been
2612+
// synthesized yet, their availability is going to match the
2613+
// property itself. This is a workaround for lazy type-checking
2614+
// because synthesis of accessors for such properties requires
2615+
// a reference to `self` which won't be available because pattern
2616+
// binding for the variable was skipped.
2617+
//
2618+
// TODO: To fix this properly `getParentPatternBinding()`
2619+
// has to be refactored into a request to help establish association
2620+
// between a variable declaration and its pattern binding on demand
2621+
// instead of by using `typeCheckPatternBinding` called from the
2622+
// declaration checker.
2623+
if (D->getAttrs().hasAttribute<LazyAttr>()) {
2624+
auto *DC = D->getDeclContext();
2625+
if (DC->isTypeContext() && !(D->getAccessor(AccessorKind::Get) &&
2626+
D->getAccessor(AccessorKind::Set)))
2627+
return;
2628+
}
2629+
26112630
// Check availability of accessor functions.
26122631
// TODO: if we're talking about an inlineable storage declaration,
26132632
// this probably needs to be refined to not assume that the accesses are

test/Inputs/lazy_typecheck.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,7 @@ extension PublicStruct {
378378
lhs = rhs
379379
}
380380
}
381+
382+
public class LazyPropertyWithClosureType {
383+
public lazy var lazyVar: Int = { 2 }()
384+
}

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)