Skip to content

Commit a1b8913

Browse files
committed
Sema: Synthesize accessors for computed local and global properties
If a stored property has a didSet/willSet, we currently synthesize the getter and the setter inside the parser. I want to get rid of the parser's code path for doing that and consolidate all accessor synthesis here in Sema. Note that the parser did not synthesize a modify. This is now more explicit in the code. Also, we have to relax an assertion since addMemberToContextIfNeeded() now gets called on declarations in local contexts.
1 parent 0f0d14f commit a1b8913

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

lib/AST/Decl.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1907,6 +1907,16 @@ bool AbstractStorageDecl::requiresOpaqueModifyCoroutine() const {
19071907
if (!supportsMutation())
19081908
return false;
19091909

1910+
auto *dc = getDeclContext();
1911+
1912+
// Local properties don't have modify accessors.
1913+
if (dc->isLocalContext())
1914+
return false;
1915+
1916+
// Fixed-layout global properties don't have modify accessors.
1917+
if (dc->isModuleScopeContext() && !isResilient())
1918+
return false;
1919+
19101920
// Imported storage declarations don't have eagerly-generated modify
19111921
// accessors.
19121922
if (hasClangNode())
@@ -1919,7 +1929,6 @@ bool AbstractStorageDecl::requiresOpaqueModifyCoroutine() const {
19191929
return false;
19201930

19211931
// Requirements of ObjC protocols don't support the modify coroutine.
1922-
auto *dc = getDeclContext();
19231932
if (auto protoDecl = dyn_cast<ProtocolDecl>(dc))
19241933
if (protoDecl->isObjC())
19251934
return false;

lib/Sema/CodeSynthesis.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static void addMemberToContextIfNeeded(Decl *D, DeclContext *DC,
7272
} else if (auto *ed = dyn_cast<ExtensionDecl>(DC)) {
7373
ed->addMember(D, Hint);
7474
} else {
75-
assert((isa<AbstractFunctionDecl>(DC) || isa<FileUnit>(DC)) &&
75+
assert((DC->isLocalContext() || isa<FileUnit>(DC)) &&
7676
"Unknown declcontext");
7777
}
7878
}
@@ -2036,8 +2036,8 @@ void swift::maybeAddAccessorsToStorage(AbstractStorageDecl *storage) {
20362036

20372037
auto *dc = storage->getDeclContext();
20382038

2039-
// Local variables don't otherwise get accessors.
2040-
if (dc->isLocalContext())
2039+
// Local stored variables don't otherwise get accessors.
2040+
if (dc->isLocalContext() && storage->getImplInfo().isSimpleStored())
20412041
return;
20422042

20432043
// Implicit properties don't get accessors.
@@ -2053,7 +2053,7 @@ void swift::maybeAddAccessorsToStorage(AbstractStorageDecl *storage) {
20532053
return;
20542054
}
20552055
// Fixed-layout global variables don't get accessors.
2056-
if (!storage->isResilient())
2056+
if (!storage->isResilient() && storage->getImplInfo().isSimpleStored())
20572057
return;
20582058

20592059
// In a protocol context, variables written as just "var x : Int" or

0 commit comments

Comments
 (0)