Skip to content

Commit e89fdd3

Browse files
committed
Requestify the computation of the list of memberwise initialized properties.
Fixes rdar://110776763, a case where initialized stored properties introduced by peer macros weren't working alongside declared stored properties.
1 parent ce4ec06 commit e89fdd3

File tree

12 files changed

+93
-17
lines changed

12 files changed

+93
-17
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3995,6 +3995,10 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
39953995
/// this type.
39963996
ArrayRef<VarDecl *> getInitAccessorProperties() const;
39973997

3998+
/// Return a collection of all properties that will be part of the memberwise
3999+
/// initializer.
4000+
ArrayRef<VarDecl *> getMemberwiseInitProperties() const;
4001+
39984002
/// Establish a mapping between properties that could be iniitalized
39994003
/// via other properties by means of init accessors. This mapping is
40004004
/// one-to-many because we allow intersecting `initializes(...)`.

include/swift/AST/DeclContext.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,9 +850,23 @@ class IterableDeclContext {
850850
HasNestedClassDeclarations = 1;
851851
}
852852

853-
/// Retrieve the set of members in this context.
853+
/// Retrieve the current set of members in this context.
854+
///
855+
/// NOTE: This operation is an alias of \c getCurrentMembers() that is considered
856+
/// deprecated. Most clients should not use this or \c getCurrentMembers(), but
857+
/// instead should use one of the projections of members that provides a semantic
858+
/// view, e.g., \c getParsedMembers(), \c getABIMembers(), \c getAllMembers(), and so
859+
/// on.
854860
DeclRange getMembers() const;
855861

862+
/// Retrieve the current set of members in this context, without triggering the
863+
/// creation of new members via code synthesis, macro expansion, etc.
864+
///
865+
/// This operation should only be used in narrow places where any side-effect
866+
/// producing operations have been done earlier. For the most part, this means that
867+
/// it should only be used in the implementation of
868+
DeclRange getCurrentMembers() const;
869+
856870
/// Get the members that were syntactically present in the source code,
857871
/// and will not contain any members that are implicitly synthesized by
858872
/// the implementation.

include/swift/AST/TypeCheckRequests.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,28 @@ class InitAccessorPropertiesRequest :
16931693
bool isCached() const { return true; }
16941694
};
16951695

1696+
/// Request to obtain a list of properties that will be reflected in the parameters of a
1697+
/// memberwise initializer.
1698+
class MemberwiseInitPropertiesRequest :
1699+
public SimpleRequest<MemberwiseInitPropertiesRequest,
1700+
ArrayRef<VarDecl *>(NominalTypeDecl *),
1701+
RequestFlags::Cached> {
1702+
public:
1703+
using SimpleRequest::SimpleRequest;
1704+
1705+
private:
1706+
friend SimpleRequest;
1707+
1708+
ArrayRef<VarDecl *>
1709+
evaluate(Evaluator &evaluator, NominalTypeDecl *decl) const;
1710+
1711+
// Evaluation.
1712+
bool evaluate(Evaluator &evaluator, AbstractStorageDecl *decl) const;
1713+
1714+
public:
1715+
bool isCached() const { return true; }
1716+
};
1717+
16961718
class HasStorageRequest :
16971719
public SimpleRequest<HasStorageRequest,
16981720
bool(AbstractStorageDecl *),

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ SWIFT_REQUEST(TypeChecker, StoredPropertiesRequest,
312312
SWIFT_REQUEST(TypeChecker, InitAccessorPropertiesRequest,
313313
ArrayRef<VarDecl *>(NominalTypeDecl *),
314314
Cached, NoLocationInfo)
315+
SWIFT_REQUEST(TypeChecker, MemberwiseInitPropertiesRequest,
316+
ArrayRef<VarDecl *>(NominalTypeDecl *),
317+
Cached, NoLocationInfo)
315318
SWIFT_REQUEST(TypeChecker, StructuralTypeRequest, Type(TypeAliasDecl *), Cached,
316319
NoLocationInfo)
317320
SWIFT_REQUEST(TypeChecker, SuperclassTypeRequest,

include/swift/AST/TypeMemberVisitor.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ class TypeMemberVisitor : public DeclVisitor<ImplClass, RetTy> {
6161

6262
/// A convenience method to visit all the members.
6363
void visitMembers(NominalTypeDecl *D) {
64-
for (Decl *member : D->getMembers()) {
65-
member->visitAuxiliaryDecls([&](Decl *decl) {
66-
asImpl().visit(decl);
67-
});
64+
for (Decl *member : D->getAllMembers()) {
6865
asImpl().visit(member);
6966
}
7067
}

lib/AST/Decl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4920,6 +4920,16 @@ NominalTypeDecl::getInitAccessorProperties() const {
49204920
{});
49214921
}
49224922

4923+
ArrayRef<VarDecl *>
4924+
NominalTypeDecl::getMemberwiseInitProperties() const {
4925+
auto &ctx = getASTContext();
4926+
auto mutableThis = const_cast<NominalTypeDecl *>(this);
4927+
return evaluateOrDefault(
4928+
ctx.evaluator,
4929+
MemberwiseInitPropertiesRequest{mutableThis},
4930+
{});
4931+
}
4932+
49234933
void NominalTypeDecl::collectPropertiesInitializableByInitAccessors(
49244934
std::multimap<VarDecl *, VarDecl *> &result) const {
49254935
for (auto *property : getInitAccessorProperties()) {

lib/AST/DeclContext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,10 @@ DeclRange IterableDeclContext::getCurrentMembersWithoutLoading() const {
920920
}
921921

922922
DeclRange IterableDeclContext::getMembers() const {
923+
return getCurrentMembers();
924+
}
925+
926+
DeclRange IterableDeclContext::getCurrentMembers() const {
923927
loadAllMembers();
924928

925929
return getCurrentMembersWithoutLoading();

lib/Refactoring/Refactoring.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3415,7 +3415,7 @@ collectMembersForInit(ResolvedCursorInfoPtr CursorInfo,
34153415

34163416
SourceManager &SM = nominalDecl->getASTContext().SourceMgr;
34173417

3418-
for (auto member : nominalDecl->getMembers()) {
3418+
for (auto member : nominalDecl->getMemberwiseInitProperties()) {
34193419
auto varDecl = dyn_cast<VarDecl>(member);
34203420
if (!varDecl) {
34213421
continue;

lib/SILGen/SILGenConstructor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
406406
storedProperties.insert(properties.begin(), properties.end());
407407
}
408408

409-
for (auto *member : decl->getMembers()) {
409+
for (auto *member : decl->getAllMembers()) {
410410
auto *field = dyn_cast<VarDecl>(member);
411411
if (!field)
412412
continue;

lib/Sema/CodeSynthesis.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,7 @@ static ConstructorDecl *createImplicitConstructor(NominalTypeDecl *decl,
297297
std::multimap<VarDecl *, VarDecl *> initializedViaAccessor;
298298
decl->collectPropertiesInitializableByInitAccessors(initializedViaAccessor);
299299

300-
for (auto member : decl->getMembers()) {
301-
auto var = dyn_cast<VarDecl>(member);
302-
if (!var)
303-
continue;
304-
305-
if (!var->isMemberwiseInitialized(/*preferDeclaredProperties=*/true))
306-
continue;
307-
300+
for (auto var : decl->getMemberwiseInitProperties()) {
308301
if (initializedViaAccessor.count(var))
309302
continue;
310303

0 commit comments

Comments
 (0)