Skip to content

Commit 34f78df

Browse files
authored
Merge pull request #66529 from DougGregor/requestify-has-storage-5.9
2 parents 8215e8f + beeadb2 commit 34f78df

21 files changed

+341
-83
lines changed

include/swift/AST/Decl.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5295,10 +5295,10 @@ class AbstractStorageDecl : public ValueDecl {
52955295

52965296
/// Overwrite the registered implementation-info. This should be
52975297
/// used carefully.
5298-
void setImplInfo(StorageImplInfo implInfo) {
5299-
LazySemanticInfo.ImplInfoComputed = 1;
5300-
ImplInfo = implInfo;
5301-
}
5298+
void setImplInfo(StorageImplInfo implInfo);
5299+
5300+
/// Cache the implementation-info, for use by the request-evaluator.
5301+
void cacheImplInfo(StorageImplInfo implInfo);
53025302

53035303
ReadImplKind getReadImpl() const {
53045304
return getImplInfo().getReadImpl();
@@ -5313,9 +5313,7 @@ class AbstractStorageDecl : public ValueDecl {
53135313

53145314
/// Return true if this is a VarDecl that has storage associated with
53155315
/// it.
5316-
bool hasStorage() const {
5317-
return getImplInfo().hasStorage();
5318-
}
5316+
bool hasStorage() const;
53195317

53205318
/// Return true if this storage has the basic accessors/capability
53215319
/// to be mutated. This is generally constant after the accessors are

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7095,6 +7095,13 @@ ERROR(invalid_macro_role_for_macro_syntax,none,
70957095
(unsigned))
70967096
ERROR(macro_cannot_introduce_names,none,
70977097
"'%0' macros are not allowed to introduce names", (StringRef))
7098+
ERROR(macro_accessor_missing_from_expansion,none,
7099+
"expansion of macro %0 did not produce a %select{non-|}1observing "
7100+
"accessor",
7101+
(DeclName, bool))
7102+
ERROR(macro_init_accessor_not_documented,none,
7103+
"expansion of macro %0 produced an unexpected 'init' accessor",
7104+
(DeclName))
70987105

70997106
ERROR(macro_resolve_circular_reference, none,
71007107
"circular reference resolving %select{freestanding|attached}0 macro %1",

include/swift/AST/Evaluator.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ class Evaluator {
316316
cache.insert<Request>(request, std::move(output));
317317
}
318318

319+
template<typename Request,
320+
typename std::enable_if<!Request::hasExternalCache>::type* = nullptr>
321+
bool hasCachedResult(const Request &request) {
322+
return cache.find_as(request) != cache.end<Request>();
323+
}
324+
319325
/// Do not introduce new callers of this function.
320326
template<typename Request,
321327
typename std::enable_if<!Request::hasExternalCache>::type* = nullptr>

include/swift/AST/NameLookup.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,13 @@ void forEachPotentialResolvedMacro(
555555
llvm::function_ref<void(MacroDecl *, const MacroRoleAttr *)> body
556556
);
557557

558+
/// For each macro with the given role that might be attached to the given
559+
/// declaration, call the body.
560+
void forEachPotentialAttachedMacro(
561+
Decl *decl, MacroRole role,
562+
llvm::function_ref<void(MacroDecl *macro, const MacroRoleAttr *)> body
563+
);
564+
558565
} // end namespace namelookup
559566

560567
/// Describes an inherited nominal entry.

include/swift/AST/TypeCheckRequests.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,26 @@ class InitAccessorPropertiesRequest :
16861686
ArrayRef<VarDecl *>
16871687
evaluate(Evaluator &evaluator, NominalTypeDecl *decl) const;
16881688

1689+
// Evaluation.
1690+
bool evaluate(Evaluator &evaluator, AbstractStorageDecl *decl) const;
1691+
1692+
public:
1693+
bool isCached() const { return true; }
1694+
};
1695+
1696+
class HasStorageRequest :
1697+
public SimpleRequest<HasStorageRequest,
1698+
bool(AbstractStorageDecl *),
1699+
RequestFlags::Cached> {
1700+
public:
1701+
using SimpleRequest::SimpleRequest;
1702+
1703+
private:
1704+
friend SimpleRequest;
1705+
1706+
// Evaluation.
1707+
bool evaluate(Evaluator &evaluator, AbstractStorageDecl *decl) const;
1708+
16891709
public:
16901710
bool isCached() const { return true; }
16911711
};

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ SWIFT_REQUEST(TypeChecker, SelfAccessKindRequest, SelfAccessKind(FuncDecl *),
302302
SWIFT_REQUEST(TypeChecker, StorageImplInfoRequest,
303303
StorageImplInfo(AbstractStorageDecl *), SeparatelyCached,
304304
NoLocationInfo)
305+
SWIFT_REQUEST(TypeChecker, HasStorageRequest,
306+
bool(AbstractStorageDecl *), Cached,
307+
NoLocationInfo)
305308
SWIFT_REQUEST(TypeChecker, StoredPropertiesAndMissingMembersRequest,
306309
ArrayRef<Decl *>(NominalTypeDecl *), Cached, NoLocationInfo)
307310
SWIFT_REQUEST(TypeChecker, StoredPropertiesRequest,

lib/AST/Decl.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2525,7 +2525,7 @@ AbstractStorageDecl::getAccessStrategy(AccessSemantics semantics,
25252525
ResilienceExpansion expansion) const {
25262526
switch (semantics) {
25272527
case AccessSemantics::DirectToStorage:
2528-
assert(hasStorage());
2528+
assert(hasStorage() || getASTContext().Diags.hadAnyError());
25292529
return AccessStrategy::getStorage();
25302530

25312531
case AccessSemantics::DistributedThunk:
@@ -6384,13 +6384,40 @@ bool ProtocolDecl::hasCircularInheritedProtocols() const {
63846384
ctx.evaluator, HasCircularInheritedProtocolsRequest{mutableThis}, true);
63856385
}
63866386

6387+
bool AbstractStorageDecl::hasStorage() const {
6388+
ASTContext &ctx = getASTContext();
6389+
return evaluateOrDefault(ctx.evaluator,
6390+
HasStorageRequest{const_cast<AbstractStorageDecl *>(this)},
6391+
false);
6392+
}
6393+
63876394
StorageImplInfo AbstractStorageDecl::getImplInfo() const {
63886395
ASTContext &ctx = getASTContext();
63896396
return evaluateOrDefault(ctx.evaluator,
63906397
StorageImplInfoRequest{const_cast<AbstractStorageDecl *>(this)},
63916398
StorageImplInfo::getSimpleStored(StorageIsMutable));
63926399
}
63936400

6401+
void AbstractStorageDecl::cacheImplInfo(StorageImplInfo implInfo) {
6402+
LazySemanticInfo.ImplInfoComputed = 1;
6403+
ImplInfo = implInfo;
6404+
}
6405+
6406+
void AbstractStorageDecl::setImplInfo(StorageImplInfo implInfo) {
6407+
cacheImplInfo(implInfo);
6408+
6409+
if (isImplicit()) {
6410+
auto &evaluator = getASTContext().evaluator;
6411+
HasStorageRequest request{this};
6412+
if (!evaluator.hasCachedResult(request))
6413+
evaluator.cacheOutput(request, implInfo.hasStorage());
6414+
else {
6415+
assert(
6416+
evaluateOrDefault(evaluator, request, false) == implInfo.hasStorage());
6417+
}
6418+
}
6419+
}
6420+
63946421
bool AbstractStorageDecl::hasPrivateAccessor() const {
63956422
for (auto accessor : getAllAccessors()) {
63966423
if (hasPrivateOrFilePrivateFormalAccess(accessor))

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,7 @@ void namelookup::forEachPotentialResolvedMacro(
16271627

16281628
/// For each macro with the given role that might be attached to the given
16291629
/// declaration, call the body.
1630-
static void forEachPotentialAttachedMacro(
1630+
void namelookup::forEachPotentialAttachedMacro(
16311631
Decl *decl, MacroRole role,
16321632
llvm::function_ref<void(MacroDecl *macro, const MacroRoleAttr *)> body
16331633
) {

lib/AST/TypeCheckRequests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ StorageImplInfoRequest::getCachedResult() const {
687687

688688
void StorageImplInfoRequest::cacheResult(StorageImplInfo value) const {
689689
auto *storage = std::get<0>(getStorage());
690-
storage->setImplInfo(value);
690+
storage->cacheImplInfo(value);
691691
}
692692

693693
//----------------------------------------------------------------------------//

lib/ClangImporter/ClangImporter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "swift/AST/NameLookupRequests.h"
3535
#include "swift/AST/PrettyStackTrace.h"
3636
#include "swift/AST/SourceFile.h"
37+
#include "swift/AST/TypeCheckRequests.h"
3738
#include "swift/AST/Types.h"
3839
#include "swift/Basic/Defer.h"
3940
#include "swift/Basic/Platform.h"
@@ -5042,6 +5043,7 @@ cloneBaseMemberDecl(ValueDecl *decl, DeclContext *newContext) {
50425043
out->setIsObjC(var->isObjC());
50435044
out->setIsDynamic(var->isDynamic());
50445045
out->copyFormalAccessFrom(var);
5046+
out->getASTContext().evaluator.cacheOutput(HasStorageRequest{out}, false);
50455047
out->setAccessors(SourceLoc(),
50465048
makeBaseClassMemberAccessors(newContext, out, var),
50475049
SourceLoc());

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ void ClangImporter::Implementation::makeComputed(AbstractStorageDecl *storage,
126126
AccessorDecl *getter,
127127
AccessorDecl *setter) {
128128
assert(getter);
129+
storage->getASTContext().evaluator.cacheOutput(HasStorageRequest{storage}, false);
129130
if (setter) {
130131
storage->setImplInfo(StorageImplInfo::getMutableComputed());
131132
storage->setAccessors(SourceLoc(), {getter, setter}, SourceLoc());

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,10 @@ static ValueDecl *deriveHashable_hashValue(DerivedConformance &derived) {
890890
SourceLoc(), C.Id_hashValue, parentDC);
891891
hashValueDecl->setInterfaceType(intType);
892892
hashValueDecl->setSynthesized();
893+
hashValueDecl->setImplicit();
894+
hashValueDecl->setImplInfo(StorageImplInfo::getImmutableComputed());
895+
hashValueDecl->copyFormalAccessFrom(derived.Nominal,
896+
/*sourceIsParentContext*/ true);
893897

894898
ParameterList *params = ParameterList::createEmpty(C);
895899

@@ -908,12 +912,7 @@ static ValueDecl *deriveHashable_hashValue(DerivedConformance &derived) {
908912
/*sourceIsParentContext*/ true);
909913

910914
// Finish creating the property.
911-
hashValueDecl->setImplicit();
912-
hashValueDecl->setInterfaceType(intType);
913-
hashValueDecl->setImplInfo(StorageImplInfo::getImmutableComputed());
914915
hashValueDecl->setAccessors(SourceLoc(), {getterDecl}, SourceLoc());
915-
hashValueDecl->copyFormalAccessFrom(derived.Nominal,
916-
/*sourceIsParentContext*/ true);
917916

918917
// The derived hashValue of an actor must be nonisolated.
919918
if (!addNonIsolatedToSynthesized(derived.Nominal, hashValueDecl) &&

lib/Sema/TypeCheckMacros.cpp

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,50 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
12411241
return macroSourceFile;
12421242
}
12431243

1244+
bool swift::accessorMacroOnlyIntroducesObservers(
1245+
MacroDecl *macro,
1246+
const MacroRoleAttr *attr
1247+
) {
1248+
// Will this macro introduce observers?
1249+
bool foundObserver = false;
1250+
for (auto name : attr->getNames()) {
1251+
if (name.getKind() == MacroIntroducedDeclNameKind::Named &&
1252+
(name.getName().getBaseName().userFacingName() == "willSet" ||
1253+
name.getName().getBaseName().userFacingName() == "didSet")) {
1254+
foundObserver = true;
1255+
} else {
1256+
// Introduces something other than an observer.
1257+
return false;
1258+
}
1259+
}
1260+
1261+
if (foundObserver)
1262+
return true;
1263+
1264+
// WORKAROUND: Older versions of the Observation library make
1265+
// `ObservationIgnored` an accessor macro that implies that it makes a
1266+
// stored property computed. Override that, because we know it produces
1267+
// nothing.
1268+
if (macro->getName().getBaseName().userFacingName() == "ObservationIgnored") {
1269+
return true;
1270+
}
1271+
1272+
return false;
1273+
}
1274+
1275+
bool swift::accessorMacroIntroducesInitAccessor(
1276+
MacroDecl *macro, const MacroRoleAttr *attr
1277+
) {
1278+
for (auto name : attr->getNames()) {
1279+
if (name.getKind() == MacroIntroducedDeclNameKind::Named &&
1280+
(name.getName().getBaseName().getKind() ==
1281+
DeclBaseName::Kind::Constructor))
1282+
return true;
1283+
}
1284+
1285+
return false;
1286+
}
1287+
12441288
Optional<unsigned> swift::expandAccessors(
12451289
AbstractStorageDecl *storage, CustomAttr *attr, MacroDecl *macro
12461290
) {
@@ -1252,30 +1296,54 @@ Optional<unsigned> swift::expandAccessors(
12521296
return None;
12531297

12541298
PrettyStackTraceDecl debugStack(
1255-
"type checking expanded declaration macro", storage);
1299+
"type checking expanded accessor macro", storage);
12561300

12571301
// Trigger parsing of the sequence of accessor declarations. This has the
12581302
// side effect of registering those accessor declarations with the storage
12591303
// declaration, so there is nothing further to do.
1304+
bool foundNonObservingAccessor = false;
1305+
bool foundInitAccessor = false;
12601306
for (auto decl : macroSourceFile->getTopLevelItems()) {
12611307
auto accessor = dyn_cast_or_null<AccessorDecl>(decl.dyn_cast<Decl *>());
12621308
if (!accessor)
12631309
continue;
12641310

1265-
if (accessor->isObservingAccessor())
1266-
continue;
1311+
if (accessor->isInitAccessor())
1312+
foundInitAccessor = true;
12671313

1314+
if (!accessor->isObservingAccessor())
1315+
foundNonObservingAccessor = true;
1316+
}
1317+
1318+
auto roleAttr = macro->getMacroRoleAttr(MacroRole::Accessor);
1319+
bool expectedNonObservingAccessor =
1320+
!accessorMacroOnlyIntroducesObservers(macro, roleAttr);
1321+
if (foundNonObservingAccessor) {
12681322
// If any non-observing accessor was added, mark the initializer as
12691323
// subsumed.
12701324
if (auto var = dyn_cast<VarDecl>(storage)) {
12711325
if (auto binding = var->getParentPatternBinding()) {
12721326
unsigned index = binding->getPatternEntryIndexForVarDecl(var);
12731327
binding->setInitializerSubsumed(index);
1274-
break;
12751328
}
12761329
}
12771330
}
12781331

1332+
// Make sure we got non-observing accessors exactly where we expected to.
1333+
if (foundNonObservingAccessor != expectedNonObservingAccessor) {
1334+
storage->diagnose(
1335+
diag::macro_accessor_missing_from_expansion, macro->getName(),
1336+
!expectedNonObservingAccessor);
1337+
}
1338+
1339+
// 'init' accessors must be documented in the macro role attribute.
1340+
if (foundInitAccessor &&
1341+
!accessorMacroIntroducesInitAccessor(macro, roleAttr)) {
1342+
storage->diagnose(
1343+
diag::macro_init_accessor_not_documented, macro->getName());
1344+
// FIXME: Add the appropriate "names: named(init)".
1345+
}
1346+
12791347
return macroSourceFile->getBufferID();
12801348
}
12811349

lib/Sema/TypeCheckMacros.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ Optional<unsigned> expandPeers(CustomAttr *attr, MacroDecl *macro, Decl *decl);
7777
Optional<unsigned> expandConformances(CustomAttr *attr, MacroDecl *macro,
7878
NominalTypeDecl *nominal);
7979

80+
/// Determine whether an accessor macro with the given attribute only
81+
/// introduces observers like willSet and didSet.
82+
bool accessorMacroOnlyIntroducesObservers(
83+
MacroDecl *macro, const MacroRoleAttr *attr);
84+
85+
/// Determine whether an accessor macro (defined with the given role attribute)
86+
/// introduces an init accessor.
87+
bool accessorMacroIntroducesInitAccessor(
88+
MacroDecl *macro, const MacroRoleAttr *attr);
89+
8090
} // end namespace swift
8191

8292
#endif /* SWIFT_SEMA_TYPECHECKMACROS_H */

0 commit comments

Comments
 (0)