Skip to content

Commit 9e9b4f5

Browse files
authored
Merge pull request #35540 from DougGregor/all-members-determinism-5.4
2 parents 2b924c2 + e56c66c commit 9e9b4f5

31 files changed

+245
-139
lines changed

include/swift/AST/Decl.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ class alignas(1 << DeclAlignInBits) Decl {
333333
NumElements : 32
334334
);
335335

336-
SWIFT_INLINE_BITFIELD(ValueDecl, Decl, 1+1+1,
336+
SWIFT_INLINE_BITFIELD(ValueDecl, Decl, 1+1+1+1,
337337
AlreadyInLookupTable : 1,
338338

339339
/// Whether we have already checked whether this declaration is a
@@ -342,7 +342,11 @@ class alignas(1 << DeclAlignInBits) Decl {
342342

343343
/// Whether the decl can be accessed by swift users; for instance,
344344
/// a.storage for lazy var a is a decl that cannot be accessed.
345-
IsUserAccessible : 1
345+
IsUserAccessible : 1,
346+
347+
/// Whether this member was synthesized as part of a derived
348+
/// protocol conformance.
349+
Synthesized : 1
346350
);
347351

348352
SWIFT_INLINE_BITFIELD(AbstractStorageDecl, ValueDecl, 1,
@@ -387,7 +391,7 @@ class alignas(1 << DeclAlignInBits) Decl {
387391
SWIFT_INLINE_BITFIELD(SubscriptDecl, VarDecl, 2,
388392
StaticSpelling : 2
389393
);
390-
SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+8+1+1+1+1+1+1+1,
394+
SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+8+1+1+1+1+1+1,
391395
/// \see AbstractFunctionDecl::BodyKind
392396
BodyKind : 3,
393397

@@ -406,10 +410,6 @@ class alignas(1 << DeclAlignInBits) Decl {
406410
/// Whether the function body throws.
407411
Throws : 1,
408412

409-
/// Whether this member was synthesized as part of a derived
410-
/// protocol conformance.
411-
Synthesized : 1,
412-
413413
/// Whether this member's body consists of a single expression.
414414
HasSingleExpressionBody : 1,
415415

@@ -2020,6 +2020,7 @@ class ValueDecl : public Decl {
20202020
Bits.ValueDecl.AlreadyInLookupTable = false;
20212021
Bits.ValueDecl.CheckedRedeclaration = false;
20222022
Bits.ValueDecl.IsUserAccessible = true;
2023+
Bits.ValueDecl.Synthesized = false;
20232024
}
20242025

20252026
// MemberLookupTable borrows a bit from this type
@@ -2057,6 +2058,14 @@ class ValueDecl : public Decl {
20572058
return Bits.ValueDecl.IsUserAccessible;
20582059
}
20592060

2061+
bool isSynthesized() const {
2062+
return Bits.ValueDecl.Synthesized;
2063+
}
2064+
2065+
void setSynthesized(bool value = true) {
2066+
Bits.ValueDecl.Synthesized = value;
2067+
}
2068+
20602069
bool hasName() const { return bool(Name); }
20612070
bool isOperator() const { return Name.isOperator(); }
20622071

@@ -3532,7 +3541,7 @@ class ClassDecl final : public NominalTypeDecl {
35323541

35333542
friend class SuperclassDeclRequest;
35343543
friend class SuperclassTypeRequest;
3535-
friend class SemanticMembersRequest;
3544+
friend class ABIMembersRequest;
35363545
friend class HasMissingDesignatedInitializersRequest;
35373546
friend class InheritsSuperclassInitializersRequest;
35383547

@@ -5577,7 +5586,6 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
55775586
Bits.AbstractFunctionDecl.Overridden = false;
55785587
Bits.AbstractFunctionDecl.Async = Async;
55795588
Bits.AbstractFunctionDecl.Throws = Throws;
5580-
Bits.AbstractFunctionDecl.Synthesized = false;
55815589
Bits.AbstractFunctionDecl.HasSingleExpressionBody = false;
55825590
Bits.AbstractFunctionDecl.HasNestedTypeDeclarations = false;
55835591
}
@@ -5784,14 +5792,6 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
57845792
/// vtable.
57855793
bool needsNewVTableEntry() const;
57865794

5787-
bool isSynthesized() const {
5788-
return Bits.AbstractFunctionDecl.Synthesized;
5789-
}
5790-
5791-
void setSynthesized(bool value = true) {
5792-
Bits.AbstractFunctionDecl.Synthesized = value;
5793-
}
5794-
57955795
public:
57965796
/// Retrieve the source range of the function body.
57975797
SourceRange getBodySourceRange() const;

include/swift/AST/DeclContext.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -778,10 +778,17 @@ class IterableDeclContext {
778778
/// the implementation.
779779
ArrayRef<Decl *> getParsedMembers() const;
780780

781-
/// Get all the members that are semantically within this context,
782-
/// including any implicitly-synthesized members.
781+
/// Get all of the members within this context that can affect ABI, including
782+
/// any implicitly-synthesized members.
783+
///
784+
/// The resulting list of members will be stable across translation units.
785+
ArrayRef<Decl *> getABIMembers() const;
786+
787+
/// Get all of the members within this context, including any
788+
/// implicitly-synthesized members.
789+
///
783790
/// The resulting list of members will be stable across translation units.
784-
ArrayRef<Decl *> getSemanticMembers() const;
791+
ArrayRef<Decl *> getAllMembers() const;
785792

786793
/// Retrieve the set of members in this context without loading any from the
787794
/// associated lazy loader; this should only be used as part of implementing

include/swift/AST/PrintOptions.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ struct PrintOptions {
304304
/// such as _silgen_name, transparent, etc.
305305
bool PrintUserInaccessibleAttrs = true;
306306

307+
/// Whether to limit ourselves to printing only the "current" set of members
308+
/// in a nominal type or extension, which is semantically unstable but can
309+
/// prevent printing from doing "extra" work.
310+
bool PrintCurrentMembersOnly = false;
311+
307312
/// List of attribute kinds that should not be printed.
308313
std::vector<AnyAttrKind> ExcludeAttrList = {DAK_Transparent, DAK_Effects,
309314
DAK_FixedLayout,
@@ -517,6 +522,7 @@ struct PrintOptions {
517522
result.ShouldQualifyNestedDeclarations =
518523
QualifyNestedDeclarations::TypesOnly;
519524
result.PrintDocumentationComments = false;
525+
result.PrintCurrentMembersOnly = true;
520526
return result;
521527
}
522528

@@ -538,6 +544,7 @@ struct PrintOptions {
538544
result.SkipUnderscoredKeywords = true;
539545
result.EnumRawValues = EnumRawValueMode::PrintObjCOnly;
540546
result.MapCrossImportOverlaysToDeclaringModule = true;
547+
result.PrintCurrentMembersOnly = false;
541548
return result;
542549
}
543550

include/swift/AST/TypeCheckRequests.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,8 +1221,26 @@ class SynthesizeAccessorRequest :
12211221
void cacheResult(AccessorDecl *value) const;
12221222
};
12231223

1224-
class SemanticMembersRequest :
1225-
public SimpleRequest<SemanticMembersRequest,
1224+
class ABIMembersRequest :
1225+
public SimpleRequest<ABIMembersRequest,
1226+
ArrayRef<Decl *>(IterableDeclContext *),
1227+
RequestFlags::Cached> {
1228+
public:
1229+
using SimpleRequest::SimpleRequest;
1230+
1231+
private:
1232+
friend SimpleRequest;
1233+
1234+
// Evaluation.
1235+
ArrayRef<Decl *>
1236+
evaluate(Evaluator &evaluator, IterableDeclContext *idc) const;
1237+
1238+
public:
1239+
bool isCached() const { return true; }
1240+
};
1241+
1242+
class AllMembersRequest :
1243+
public SimpleRequest<AllMembersRequest,
12261244
ArrayRef<Decl *>(IterableDeclContext *),
12271245
RequestFlags::Cached> {
12281246
public:

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ SWIFT_REQUEST(TypeChecker, TypeEraserHasViableInitRequest,
6565
SWIFT_REQUEST(TypeChecker, DynamicallyReplacedDeclRequest,
6666
ValueDecl *(ValueDecl *),
6767
Cached, NoLocationInfo)
68-
SWIFT_REQUEST(TypeChecker, SemanticMembersRequest,
68+
SWIFT_REQUEST(TypeChecker, ABIMembersRequest,
69+
ArrayRef<Decl *>(IterableDeclContext *), Cached, NoLocationInfo)
70+
SWIFT_REQUEST(TypeChecker, AllMembersRequest,
6971
ArrayRef<Decl *>(IterableDeclContext *), Cached, NoLocationInfo)
7072
SWIFT_REQUEST(TypeChecker, SpecializeAttrTargetDeclRequest,
7173
ValueDecl *(const ValueDecl *, SpecializeAttr *),

include/swift/SIL/SILVTableVisitor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ template <class T> class SILVTableVisitor {
148148
if (!theClass->hasKnownSwiftImplementation())
149149
return;
150150

151-
for (auto member : theClass->getSemanticMembers())
151+
for (auto member : theClass->getABIMembers())
152152
maybeAddMember(member);
153153
}
154154
};

lib/AST/ASTPrinter.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
189189
localModule->isImportedImplementationOnly(nominalModule)) {
190190

191191
bool shouldPrintMembers = llvm::any_of(
192-
ED->getMembers(),
192+
ED->getAllMembers(),
193193
[&](const Decl *member) -> bool {
194194
return shouldPrint(member, options);
195195
});
@@ -1750,7 +1750,7 @@ bool ShouldPrintChecker::shouldPrint(const Decl *D,
17501750
getInheritedForPrinting(Ext, Options, ProtocolsToPrint);
17511751
if (ProtocolsToPrint.empty()) {
17521752
bool HasMemberToPrint = false;
1753-
for (auto Member : Ext->getMembers()) {
1753+
for (auto Member : Ext->getAllMembers()) {
17541754
if (shouldPrint(Member, Options)) {
17551755
HasMemberToPrint = true;
17561756
break;
@@ -2050,24 +2050,29 @@ void PrintAST::printMembersOfDecl(Decl *D, bool needComma,
20502050
bool openBracket,
20512051
bool closeBracket) {
20522052
llvm::SmallVector<Decl *, 3> Members;
2053-
auto AddDeclFunc = [&](DeclRange Range) {
2054-
for (auto RD : Range)
2055-
Members.push_back(RD);
2053+
auto AddMembers = [&](IterableDeclContext *idc) {
2054+
if (Options.PrintCurrentMembersOnly) {
2055+
for (auto RD : idc->getMembers())
2056+
Members.push_back(RD);
2057+
} else {
2058+
for (auto RD : idc->getAllMembers())
2059+
Members.push_back(RD);
2060+
}
20562061
};
20572062

20582063
if (auto Ext = dyn_cast<ExtensionDecl>(D)) {
2059-
AddDeclFunc(Ext->getMembers());
2064+
AddMembers(Ext);
20602065
} else if (auto NTD = dyn_cast<NominalTypeDecl>(D)) {
2061-
AddDeclFunc(NTD->getMembers());
2066+
AddMembers(NTD);
20622067
for (auto Ext : NTD->getExtensions()) {
20632068
if (Options.printExtensionContentAsMembers(Ext))
2064-
AddDeclFunc(Ext->getMembers());
2069+
AddMembers(Ext);
20652070
}
20662071
if (Options.PrintExtensionFromConformingProtocols) {
20672072
for (auto Conf : NTD->getAllConformances()) {
20682073
for (auto Ext : Conf->getProtocol()->getExtensions()) {
20692074
if (Options.printExtensionContentAsMembers(Ext))
2070-
AddDeclFunc(Ext->getMembers());
2075+
AddMembers(Ext);
20712076
}
20722077
}
20732078
}

lib/AST/DeclContext.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,11 +758,19 @@ ArrayRef<Decl *> IterableDeclContext::getParsedMembers() const {
758758
.members;
759759
}
760760

761-
ArrayRef<Decl *> IterableDeclContext::getSemanticMembers() const {
761+
ArrayRef<Decl *> IterableDeclContext::getABIMembers() const {
762762
ASTContext &ctx = getASTContext();
763763
return evaluateOrDefault(
764764
ctx.evaluator,
765-
SemanticMembersRequest{const_cast<IterableDeclContext *>(this)},
765+
ABIMembersRequest{const_cast<IterableDeclContext *>(this)},
766+
ArrayRef<Decl *>());
767+
}
768+
769+
ArrayRef<Decl *> IterableDeclContext::getAllMembers() const {
770+
ASTContext &ctx = getASTContext();
771+
return evaluateOrDefault(
772+
ctx.evaluator,
773+
AllMembersRequest{const_cast<IterableDeclContext *>(this)},
766774
ArrayRef<Decl *>());
767775
}
768776

lib/SILGen/SILGenType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ class SILGenType : public TypeMemberVisitor<SILGenType> {
10381038

10391039
// Build a vtable if this is a class.
10401040
if (auto theClass = dyn_cast<ClassDecl>(theType)) {
1041-
for (Decl *member : theClass->getSemanticMembers())
1041+
for (Decl *member : theClass->getABIMembers())
10421042
visit(member);
10431043

10441044
SILGenVTable genVTable(SGM, theClass);

lib/Sema/CodeSynthesis.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ static ConstructorDecl *createImplicitConstructor(NominalTypeDecl *decl,
319319

320320
// Mark implicit.
321321
ctor->setImplicit();
322+
ctor->setSynthesized();
322323
ctor->setAccess(accessLevel);
323324

324325
if (ICK == ImplicitConstructorKind::Memberwise) {

lib/Sema/DerivedConformanceCodable.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ static EnumDecl *synthesizeCodingKeysEnum(DerivedConformance &derived) {
239239
auto *enumDecl = new (C) EnumDecl(SourceLoc(), C.Id_CodingKeys, SourceLoc(),
240240
inherited, nullptr, target);
241241
enumDecl->setImplicit();
242+
enumDecl->setSynthesized();
242243
enumDecl->setAccess(AccessLevel::Private);
243244

244245
// For classes which inherit from something Encodable or Decodable, we
@@ -349,6 +350,7 @@ static VarDecl *createKeyedContainer(ASTContext &C, DeclContext *DC,
349350
auto *containerDecl = new (C) VarDecl(/*IsStatic=*/false, introducer,
350351
SourceLoc(), C.Id_container, DC);
351352
containerDecl->setImplicit();
353+
containerDecl->setSynthesized();
352354
containerDecl->setInterfaceType(containerType);
353355
return containerDecl;
354356
}

lib/Sema/DerivedConformanceDifferentiable.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ static ValueDecl *deriveDifferentiable_method(
579579
/*Async=*/false,
580580
/*Throws=*/false,
581581
/*GenericParams=*/nullptr, params, returnType, parentDC);
582+
funcDecl->setSynthesized();
582583
if (!nominal->getSelfClassDecl())
583584
funcDecl->setSelfAccessKind(SelfAccessKind::Mutating);
584585
funcDecl->setBodySynthesizer(bodySynthesizer.Fn, bodySynthesizer.Context);
@@ -732,6 +733,7 @@ getOrSynthesizeTangentVectorStruct(DerivedConformance &derived, Identifier id) {
732733
/*GenericParams*/ {}, parentDC);
733734
structDecl->setBraces({synthesizedLoc, synthesizedLoc});
734735
structDecl->setImplicit();
736+
structDecl->setSynthesized();
735737
structDecl->copyFormalAccessFrom(nominal, /*sourceIsParentContext*/ true);
736738

737739
// Add stored properties to the `TangentVector` struct.
@@ -741,6 +743,7 @@ getOrSynthesizeTangentVectorStruct(DerivedConformance &derived, Identifier id) {
741743
auto *tangentProperty = new (C) VarDecl(
742744
member->isStatic(), member->getIntroducer(),
743745
/*NameLoc*/ SourceLoc(), member->getName(), structDecl);
746+
tangentProperty->setSynthesized();
744747
// Note: `tangentProperty` is not marked as implicit here, because that
745748
// incorrectly affects memberwise initializer synthesis.
746749
auto memberContextualType =

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ static ValueDecl *deriveHashable_hashValue(DerivedConformance &derived) {
882882
new (C) VarDecl(/*IsStatic*/false, VarDecl::Introducer::Var,
883883
SourceLoc(), C.Id_hashValue, parentDC);
884884
hashValueDecl->setInterfaceType(intType);
885+
hashValueDecl->setSynthesized();
885886

886887
ParameterList *params = ParameterList::createEmpty(C);
887888

@@ -894,6 +895,7 @@ static ValueDecl *deriveHashable_hashValue(DerivedConformance &derived) {
894895
intType, parentDC);
895896
getterDecl->setImplicit();
896897
getterDecl->setBodySynthesizer(&deriveBodyHashable_hashValue);
898+
getterDecl->setSynthesized();
897899
getterDecl->setIsTransparent(false);
898900

899901
getterDecl->copyFormalAccessFrom(derived.Nominal,

lib/Sema/DerivedConformances.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ DerivedConformance::declareDerivedProperty(Identifier name,
468468
VarDecl(/*IsStatic*/ isStatic, VarDecl::Introducer::Var,
469469
SourceLoc(), name, parentDC);
470470
propDecl->setImplicit();
471+
propDecl->setSynthesized();
471472
propDecl->copyFormalAccessFrom(Nominal, /*sourceIsParentContext*/ true);
472473
propDecl->setInterfaceType(propertyInterfaceType);
473474

0 commit comments

Comments
 (0)