Skip to content

Mark lazy properties as having mutating getters immediately. #20796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
6 changes: 0 additions & 6 deletions lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,6 @@ static AccessorDecl *createGetterPrototype(TypeChecker &TC,
ParamDecl *selfDecl = nullptr;
if (storage->getDeclContext()->isTypeContext()) {
if (storage->getAttrs().hasAttribute<LazyAttr>()) {
// The getter is considered mutating if it's on a value type.
if (!storage->getDeclContext()->getSelfClassDecl() &&
!storage->isStatic()) {
storage->setIsGetterMutating(true);
}

// For lazy properties, steal the 'self' from the initializer context.
auto *varDecl = cast<VarDecl>(storage);
auto *bindingDecl = varDecl->getParentPatternBinding();
Expand Down
9 changes: 9 additions & 0 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2280,6 +2280,15 @@ static bool validateAccessorIsMutating(TypeChecker &TC, FuncDecl *accessor) {

static bool computeIsGetterMutating(TypeChecker &TC,
AbstractStorageDecl *storage) {
// 'lazy' overrides the normal accessor-based rules and heavily
// restricts what accessors can be used. The getter is considered
// mutating if this is instance storage on a value type.
if (storage->getAttrs().hasAttribute<LazyAttr>()) {
return storage->getDeclContext()->isTypeContext() &&
!storage->getDeclContext()->getSelfClassDecl() &&
!storage->isStatic();
}

switch (storage->getReadImpl()) {
case ReadImplKind::Stored:
return false;
Expand Down
3 changes: 3 additions & 0 deletions test/multifile/Inputs/external_lazy_property.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public struct Test1 {
lazy var property: String = "help"
}
6 changes: 6 additions & 0 deletions test/multifile/lazy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: %target-swift-frontend -emit-sil -verify -primary-file %s %S/Inputs/external_lazy_property.swift

// rdar://45712204
func test1(s: Test1) {
s.property // expected-error {{cannot use mutating getter on immutable value: 's' is a 'let' constant}}
}