mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Merge remote-tracking branch 'origin/master' into master-next
This commit is contained in:
@@ -203,7 +203,8 @@ struct SynthesizedExtensionAnalyzer::Implementation {
|
||||
unsigned countInherits(ExtensionDecl *ED) {
|
||||
unsigned Count = 0;
|
||||
for (auto TL : ED->getInherited()) {
|
||||
if (shouldPrint(TL.getType()->getAnyNominal(), Options))
|
||||
auto *nominal = TL.getType()->getAnyNominal();
|
||||
if (nominal && shouldPrint(nominal, Options))
|
||||
Count ++;
|
||||
}
|
||||
return Count;
|
||||
|
||||
@@ -2106,9 +2106,42 @@ bool NominalTypeDecl::derivesProtocolConformance(ProtocolDecl *protocol) const {
|
||||
return isObjC() && enumDecl->hasCases()
|
||||
&& enumDecl->hasOnlyCasesWithoutAssociatedValues();
|
||||
|
||||
// Enums without associated values and enums with a raw type of String
|
||||
// or Int can explicitly derive CodingKey conformance.
|
||||
case KnownProtocolKind::CodingKey: {
|
||||
Type rawType = enumDecl->getRawType();
|
||||
if (rawType) {
|
||||
auto parentDC = enumDecl->getDeclContext();
|
||||
ASTContext &C = parentDC->getASTContext();
|
||||
|
||||
auto nominal = rawType->getAnyNominal();
|
||||
return nominal == C.getStringDecl() || nominal == C.getIntDecl();
|
||||
}
|
||||
|
||||
// hasOnlyCasesWithoutAssociatedValues will return true for empty enums;
|
||||
// empty enumas are allowed to conform as well.
|
||||
return enumDecl->hasOnlyCasesWithoutAssociatedValues();
|
||||
}
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
} else if (isa<StructDecl>(this) || isa<ClassDecl>(this)) {
|
||||
// Structs and classes can explicitly derive Encodable and Decodable
|
||||
// conformance (explicitly meaning we can synthesize an implementation if
|
||||
// a type conforms manually).
|
||||
if (*knownProtocol == KnownProtocolKind::Encodable ||
|
||||
*knownProtocol == KnownProtocolKind::Decodable) {
|
||||
// FIXME: This is not actually correct. We cannot promise to always
|
||||
// provide a witness here for all structs and classes. Unfortunately,
|
||||
// figuring out whether this is actually possible requires much more
|
||||
// context -- a TypeChecker and the parent decl context at least -- and is
|
||||
// tightly coupled to the logic within DerivedConformance.
|
||||
// This unfortunately means that we expect a witness even if one will not
|
||||
// be produced, which requires DerivedConformance::deriveCodable to output
|
||||
// its own diagnostics.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user