Skip to content

Commit 4affa87

Browse files
authored
Merge pull request #20796 from rjmccall/lazy-property-is-mutating-5.0
Mark lazy properties as having mutating getters immediately.
2 parents 2ee7464 + 05bfc28 commit 4affa87

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

lib/Sema/CodeSynthesis.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,6 @@ static AccessorDecl *createGetterPrototype(TypeChecker &TC,
189189
ParamDecl *selfDecl = nullptr;
190190
if (storage->getDeclContext()->isTypeContext()) {
191191
if (storage->getAttrs().hasAttribute<LazyAttr>()) {
192-
// The getter is considered mutating if it's on a value type.
193-
if (!storage->getDeclContext()->getSelfClassDecl() &&
194-
!storage->isStatic()) {
195-
storage->setIsGetterMutating(true);
196-
}
197-
198192
// For lazy properties, steal the 'self' from the initializer context.
199193
auto *varDecl = cast<VarDecl>(storage);
200194
auto *bindingDecl = varDecl->getParentPatternBinding();

lib/Sema/TypeCheckDecl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,6 +2282,15 @@ static bool validateAccessorIsMutating(TypeChecker &TC, FuncDecl *accessor) {
22822282

22832283
static bool computeIsGetterMutating(TypeChecker &TC,
22842284
AbstractStorageDecl *storage) {
2285+
// 'lazy' overrides the normal accessor-based rules and heavily
2286+
// restricts what accessors can be used. The getter is considered
2287+
// mutating if this is instance storage on a value type.
2288+
if (storage->getAttrs().hasAttribute<LazyAttr>()) {
2289+
return storage->getDeclContext()->isTypeContext() &&
2290+
!storage->getDeclContext()->getSelfClassDecl() &&
2291+
!storage->isStatic();
2292+
}
2293+
22852294
switch (storage->getReadImpl()) {
22862295
case ReadImplKind::Stored:
22872296
return false;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public struct Test1 {
2+
lazy var property: String = "help"
3+
}

test/multifile/lazy.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %target-swift-frontend -emit-sil -verify -primary-file %s %S/Inputs/external_lazy_property.swift
2+
3+
// rdar://45712204
4+
func test1(s: Test1) {
5+
s.property // expected-error {{cannot use mutating getter on immutable value: 's' is a 'let' constant}}
6+
}

0 commit comments

Comments
 (0)