Skip to content

Commit 56f8f05

Browse files
committed
AST: Move 'private set' attribute computation to SetterAccessLevelRequest
1 parent 8561407 commit 56f8f05

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

lib/AST/AccessRequests.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,37 @@ void AccessLevelRequest::cacheResult(AccessLevel value) const {
148148
// the cycle of computation associated with formal accesses, we give it its own
149149
// request.
150150

151+
// In a .swiftinterface file, a stored property with an explicit @_hasStorage
152+
// attribute but no setter is assumed to have originally been a private(set).
153+
static bool isStoredWithPrivateSetter(VarDecl *VD) {
154+
auto *HSA = VD->getAttrs().getAttribute<HasStorageAttr>();
155+
if (!HSA || HSA->isImplicit())
156+
return false;
157+
158+
auto *DC = VD->getDeclContext();
159+
auto *SF = DC->getParentSourceFile();
160+
if (!SF || SF->Kind != SourceFileKind::Interface)
161+
return false;
162+
163+
if (VD->isLet() ||
164+
(VD->getSetter() &&
165+
!VD->getSetter()->isImplicit()))
166+
return false;
167+
168+
return true;
169+
}
170+
151171
llvm::Expected<AccessLevel>
152172
SetterAccessLevelRequest::evaluate(Evaluator &evaluator,
153173
AbstractStorageDecl *ASD) const {
154174
assert(!ASD->Accessors.getInt().hasValue());
155-
if (auto *AA = ASD->getAttrs().getAttribute<SetterAccessAttr>())
156-
return AA->getAccess();
175+
if (auto *SAA = ASD->getAttrs().getAttribute<SetterAccessAttr>())
176+
return SAA->getAccess();
177+
178+
if (auto *VD = dyn_cast<VarDecl>(ASD))
179+
if (isStoredWithPrivateSetter(VD))
180+
return AccessLevel::Private;
181+
157182
return ASD->getFormalAccess();
158183
}
159184

lib/Parse/ParseDecl.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5024,14 +5024,6 @@ static StorageImplInfo classifyWithHasStorageAttr(
50245024
return false;
50255025
};
50265026

5027-
// Determines if the storage had a private setter, i.e. it's not a 'let' and
5028-
// it had a setter.
5029-
auto isPrivateSet = [&]() {
5030-
if (auto varDecl = dyn_cast<VarDecl>(storage))
5031-
return !varDecl->isLet() && accessors.Set == nullptr;
5032-
return false;
5033-
};
5034-
50355027
// Default to stored writes.
50365028
WriteImplKind writeImpl = WriteImplKind::Stored;
50375029
ReadWriteImplKind readWriteImpl = ReadWriteImplKind::Stored;
@@ -5048,14 +5040,6 @@ static StorageImplInfo classifyWithHasStorageAttr(
50485040
readWriteImpl = ReadWriteImplKind::Immutable;
50495041
}
50505042

5051-
if (isPrivateSet()) {
5052-
// If we saw a 'var' with no setter, that means it was
5053-
// private/internal(set). Honor that with a synthesized attribute.
5054-
storage->getAttrs().add(
5055-
new (ctx) SetterAccessAttr(
5056-
SourceLoc(), SourceLoc(), AccessLevel::Private, /*implicit: */true));
5057-
}
5058-
50595043
// Always force Stored reads if @_hasStorage is present.
50605044
return StorageImplInfo(ReadImplKind::Stored, writeImpl, readWriteImpl);
50615045
}

0 commit comments

Comments
 (0)