Skip to content

Commit 80f8cfb

Browse files
authored
Merge pull request #84654 from xedin/rdar-129359362-6.2
[6.2][TypeChecker] Avoid checking lazy property accessors if they haven't …
2 parents 7885b07 + d91b464 commit 80f8cfb

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)