Skip to content

Commit 99e60b0

Browse files
authored
Merge pull request #29237 from brentdax/revenge-of-the-synth
Revert "Merge pull request #28665 from CodaFi/the-phantom-menace"
2 parents ffc10a5 + 0c478b6 commit 99e60b0

23 files changed

+157
-221
lines changed

include/swift/AST/Attr.def

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -535,11 +535,7 @@ DECL_ATTR(derivative, Derivative,
535535
ABIStableToAdd | ABIBreakingToRemove | APIStableToAdd | APIBreakingToRemove,
536536
97)
537537

538-
DECL_ATTR(_implicitly_synthesizes_nested_requirement, ImplicitlySynthesizesNestedRequirement,
539-
OnProtocol |
540-
UserInaccessible |
541-
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
542-
98)
538+
// NOTE: 98 is unused
543539

544540
DECL_ATTR(transpose, Transpose,
545541
OnFunc | LongAttribute | AllowMultipleAttributes |

include/swift/AST/Attr.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -685,23 +685,6 @@ class SwiftNativeObjCRuntimeBaseAttr : public DeclAttribute {
685685
}
686686
};
687687

688-
/// Defines the @_implicitly_synthesizes_nested_requirement attribute.
689-
class ImplicitlySynthesizesNestedRequirementAttr : public DeclAttribute {
690-
public:
691-
ImplicitlySynthesizesNestedRequirementAttr(StringRef Value, SourceLoc AtLoc,
692-
SourceRange Range)
693-
: DeclAttribute(DAK_ImplicitlySynthesizesNestedRequirement,
694-
AtLoc, Range, /*Implicit*/false),
695-
Value(Value) {}
696-
697-
/// The name of the phantom requirement.
698-
const StringRef Value;
699-
700-
static bool classof(const DeclAttribute *DA) {
701-
return DA->getKind() == DAK_ImplicitlySynthesizesNestedRequirement;
702-
}
703-
};
704-
705688
/// Determine the result of comparing an availability attribute to a specific
706689
/// platform or language version.
707690
enum class AvailableVersionComparison {

include/swift/AST/Decl.h

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -476,15 +476,11 @@ class alignas(1 << DeclAlignInBits) Decl {
476476
IsDebuggerAlias : 1
477477
);
478478

479-
SWIFT_INLINE_BITFIELD(NominalTypeDecl, GenericTypeDecl, 1+1+1+1,
479+
SWIFT_INLINE_BITFIELD(NominalTypeDecl, GenericTypeDecl, 1+1+1,
480480
/// Whether we have already added implicitly-defined initializers
481481
/// to this declaration.
482482
AddedImplicitInitializers : 1,
483483

484-
/// Whether we have already added the phantom CodingKeys
485-
/// nested requirement to this declaration.
486-
AddedPhantomCodingKeys : 1,
487-
488484
/// Whether there is are lazily-loaded conformances for this nominal type.
489485
HasLazyConformances : 1,
490486

@@ -3340,7 +3336,6 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
33403336
IterableDeclContext(IterableDeclContextKind::NominalTypeDecl)
33413337
{
33423338
Bits.NominalTypeDecl.AddedImplicitInitializers = false;
3343-
Bits.NominalTypeDecl.AddedPhantomCodingKeys = false;
33443339
ExtensionGeneration = 0;
33453340
Bits.NominalTypeDecl.HasLazyConformances = false;
33463341
Bits.NominalTypeDecl.IsComputingSemanticMembers = false;
@@ -3381,18 +3376,6 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
33813376
Bits.NominalTypeDecl.AddedImplicitInitializers = true;
33823377
}
33833378

3384-
/// Determine whether we have already attempted to add the
3385-
/// phantom CodingKeys type to this declaration.
3386-
bool addedPhantomCodingKeys() const {
3387-
return Bits.NominalTypeDecl.AddedPhantomCodingKeys;
3388-
}
3389-
3390-
/// Note that we have attempted to add the phantom CodingKeys type
3391-
/// to this declaration.
3392-
void setAddedPhantomCodingKeys() {
3393-
Bits.NominalTypeDecl.AddedPhantomCodingKeys = true;
3394-
}
3395-
33963379
/// getDeclaredType - Retrieve the type declared by this entity, without
33973380
/// any generic parameters bound if this is a generic type.
33983381
Type getDeclaredType() const;

include/swift/AST/PrintOptions.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,7 @@ struct PrintOptions {
296296
/// List of attribute kinds that should not be printed.
297297
std::vector<AnyAttrKind> ExcludeAttrList = {DAK_Transparent, DAK_Effects,
298298
DAK_FixedLayout,
299-
DAK_ShowInInterface,
300-
DAK_ImplicitlySynthesizesNestedRequirement};
299+
DAK_ShowInInterface};
301300

302301
/// List of attribute kinds that should be printed exclusively.
303302
/// Empty means allow all.

include/swift/AST/TypeCheckRequests.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,8 @@ class InheritsSuperclassInitializersRequest
16821682
enum class ImplicitMemberAction : uint8_t {
16831683
ResolveImplicitInit,
16841684
ResolveCodingKeys,
1685+
ResolveEncodable,
1686+
ResolveDecodable,
16851687
};
16861688

16871689
class ResolveImplicitMemberRequest

lib/AST/Attr.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -958,11 +958,6 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
958958
break;
959959
}
960960

961-
case DAK_ImplicitlySynthesizesNestedRequirement:
962-
Printer.printAttrName("@_implicitly_synthesizes_nested_requirement");
963-
Printer << "(\"" << cast<ImplicitlySynthesizesNestedRequirementAttr>(this)->Value << "\")";
964-
break;
965-
966961
case DAK_Count:
967962
llvm_unreachable("exceed declaration attribute kinds");
968963

@@ -1024,8 +1019,6 @@ StringRef DeclAttribute::getAttrName() const {
10241019
return "_swift_native_objc_runtime_base";
10251020
case DAK_Semantics:
10261021
return "_semantics";
1027-
case DAK_ImplicitlySynthesizesNestedRequirement:
1028-
return "_implicitly_synthesizes_nested_requirement";
10291022
case DAK_Available:
10301023
return "availability";
10311024
case DAK_ObjC:

lib/AST/Decl.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4047,6 +4047,19 @@ void NominalTypeDecl::synthesizeSemanticMembersIfNeeded(DeclName member) {
40474047
if (baseName.getIdentifier() == getASTContext().Id_CodingKeys) {
40484048
action.emplace(ImplicitMemberAction::ResolveCodingKeys);
40494049
}
4050+
} else {
4051+
auto argumentNames = member.getArgumentNames();
4052+
if (!member.isCompoundName() || argumentNames.size() == 1) {
4053+
if (baseName == DeclBaseName::createConstructor() &&
4054+
(member.isSimpleName() || argumentNames.front() == Context.Id_from)) {
4055+
action.emplace(ImplicitMemberAction::ResolveDecodable);
4056+
} else if (!baseName.isSpecial() &&
4057+
baseName.getIdentifier() == Context.Id_encode &&
4058+
(member.isSimpleName() ||
4059+
argumentNames.front() == Context.Id_to)) {
4060+
action.emplace(ImplicitMemberAction::ResolveEncodable);
4061+
}
4062+
}
40504063
}
40514064

40524065
if (auto actionToTake = action) {

lib/AST/TypeCheckRequests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,12 @@ void swift::simple_display(llvm::raw_ostream &out,
10741074
case ImplicitMemberAction::ResolveCodingKeys:
10751075
out << "resolve CodingKeys";
10761076
break;
1077+
case ImplicitMemberAction::ResolveEncodable:
1078+
out << "resolve Encodable.encode(to:)";
1079+
break;
1080+
case ImplicitMemberAction::ResolveDecodable:
1081+
out << "resolve Decodable.init(from:)";
1082+
break;
10771083
}
10781084
}
10791085

lib/Parse/ParseDecl.cpp

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,43 +1867,6 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
18671867
}
18681868
break;
18691869
}
1870-
1871-
case DAK_ImplicitlySynthesizesNestedRequirement: {
1872-
if (!consumeIf(tok::l_paren)) {
1873-
diagnose(Loc, diag::attr_expected_lparen, AttrName,
1874-
DeclAttribute::isDeclModifier(DK));
1875-
return false;
1876-
}
1877-
1878-
if (Tok.isNot(tok::string_literal)) {
1879-
diagnose(Loc, diag::attr_expected_string_literal, AttrName);
1880-
return false;
1881-
}
1882-
1883-
auto Value = getStringLiteralIfNotInterpolated(
1884-
Loc, ("'" + AttrName + "'").str());
1885-
1886-
consumeToken(tok::string_literal);
1887-
1888-
if (Value.hasValue())
1889-
AttrRange = SourceRange(Loc, Tok.getRange().getStart());
1890-
else
1891-
DiscardAttribute = true;
1892-
1893-
if (!consumeIf(tok::r_paren)) {
1894-
diagnose(Loc, diag::attr_expected_rparen, AttrName,
1895-
DeclAttribute::isDeclModifier(DK));
1896-
return false;
1897-
}
1898-
1899-
if (!DiscardAttribute) {
1900-
Attributes.add(new (Context)
1901-
ImplicitlySynthesizesNestedRequirementAttr(Value.getValue(), AtLoc,
1902-
AttrRange));
1903-
}
1904-
break;
1905-
}
1906-
19071870
case DAK_Available: {
19081871
if (!consumeIf(tok::l_paren)) {
19091872
diagnose(Loc, diag::attr_expected_lparen, AttrName,

lib/Sema/CodeSynthesis.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,8 +1076,11 @@ ResolveImplicitMemberRequest::evaluate(Evaluator &evaluator,
10761076
// FIXME: This entire request is a layering violation made of smaller,
10771077
// finickier layering violations. See rdar://56844567
10781078

1079-
auto &Context = target->getASTContext();
1080-
auto tryToInstallCodingKeys = [&](ProtocolDecl *protocol) {
1079+
// Checks whether the target conforms to the given protocol. If the
1080+
// conformance is incomplete, force the conformance.
1081+
//
1082+
// Returns whether the target conforms to the protocol.
1083+
auto evaluateTargetConformanceTo = [&](ProtocolDecl *protocol) {
10811084
if (!protocol)
10821085
return false;
10831086

@@ -1100,6 +1103,7 @@ ResolveImplicitMemberRequest::evaluate(Evaluator &evaluator,
11001103
return true;
11011104
};
11021105

1106+
auto &Context = target->getASTContext();
11031107
switch (action) {
11041108
case ImplicitMemberAction::ResolveImplicitInit:
11051109
TypeChecker::addImplicitConstructors(target);
@@ -1117,11 +1121,30 @@ ResolveImplicitMemberRequest::evaluate(Evaluator &evaluator,
11171121
// synthesized, it will be synthesized.
11181122
auto *decodableProto = Context.getProtocol(KnownProtocolKind::Decodable);
11191123
auto *encodableProto = Context.getProtocol(KnownProtocolKind::Encodable);
1120-
if (!tryToInstallCodingKeys(decodableProto)) {
1121-
(void)tryToInstallCodingKeys(encodableProto);
1124+
if (!evaluateTargetConformanceTo(decodableProto)) {
1125+
(void)evaluateTargetConformanceTo(encodableProto);
11221126
}
11231127
}
11241128
break;
1129+
case ImplicitMemberAction::ResolveEncodable: {
1130+
// encode(to:) may be synthesized as part of derived conformance to the
1131+
// Encodable protocol.
1132+
// If the target should conform to the Encodable protocol, check the
1133+
// conformance here to attempt synthesis.
1134+
auto *encodableProto = Context.getProtocol(KnownProtocolKind::Encodable);
1135+
(void)evaluateTargetConformanceTo(encodableProto);
1136+
}
1137+
break;
1138+
case ImplicitMemberAction::ResolveDecodable: {
1139+
// init(from:) may be synthesized as part of derived conformance to the
1140+
// Decodable protocol.
1141+
// If the target should conform to the Decodable protocol, check the
1142+
// conformance here to attempt synthesis.
1143+
TypeChecker::addImplicitConstructors(target);
1144+
auto *decodableProto = Context.getProtocol(KnownProtocolKind::Decodable);
1145+
(void)evaluateTargetConformanceTo(decodableProto);
1146+
}
1147+
break;
11251148
}
11261149
return true;
11271150
}

0 commit comments

Comments
 (0)