Skip to content

Commit a08c563

Browse files
committed
Sema: Tidy up conformance synthesis a little
1 parent ba6582d commit a08c563

File tree

2 files changed

+13
-27
lines changed

2 files changed

+13
-27
lines changed

lib/Sema/DerivedConformanceCodable.cpp

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,7 @@ static CodableConformanceType typeConformsToCodable(TypeChecker &tc,
8383
DeclContext *context,
8484
Type target, bool isIUO,
8585
ProtocolDecl *proto) {
86-
target = context->mapTypeIntoContext(target->mapTypeOutOfContext());
87-
// Some generic types need to be introspected to get at their "true" Codable
88-
// conformance.
89-
if (auto referenceType = target->getAs<ReferenceStorageType>()) {
90-
// This is a weak/unowned/unmanaged var. Get the inner type before checking
91-
// conformance.
92-
target = referenceType->getReferentType();
93-
}
86+
target = context->mapTypeIntoContext(target);
9487

9588
if (isIUO)
9689
return typeConformsToCodable(tc, context, target->getOptionalObjectType(),
@@ -129,17 +122,18 @@ static CodableConformanceType varConformsToCodable(TypeChecker &tc,
129122
// }
130123
//
131124
// Validate the decl eagerly.
132-
if (!varDecl->hasType())
125+
if (!varDecl->hasInterfaceType())
133126
tc.validateDecl(varDecl);
134127

135128
// If the var decl didn't validate, it may still not have a type; confirm it
136129
// has a type before ensuring the type conforms to Codable.
137-
if (!varDecl->hasType())
130+
if (!varDecl->hasInterfaceType())
138131
return TypeNotValidated;
139132

140133
bool isIUO =
141134
varDecl->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>();
142-
return typeConformsToCodable(tc, context, varDecl->getType(), isIUO, proto);
135+
return typeConformsToCodable(tc, context, varDecl->getValueInterfaceType(),
136+
isIUO, proto);
143137
}
144138

145139
/// Validates the given CodingKeys enum decl by ensuring its cases are a 1-to-1
@@ -522,8 +516,6 @@ static std::tuple<VarDecl *, Type, bool>
522516
lookupVarDeclForCodingKeysCase(DeclContext *conformanceDC,
523517
EnumElementDecl *elt,
524518
NominalTypeDecl *targetDecl) {
525-
ASTContext &C = elt->getASTContext();
526-
527519
for (auto decl : targetDecl->lookupDirect(DeclName(elt->getName()))) {
528520
if (auto *vd = dyn_cast<VarDecl>(decl)) {
529521
if (!vd->isStatic()) {
@@ -532,17 +524,11 @@ lookupVarDeclForCodingKeysCase(DeclContext *conformanceDC,
532524
auto varType =
533525
conformanceDC->mapTypeIntoContext(vd->getValueInterfaceType());
534526

535-
bool useIfPresentVariant =
536-
varType->getAnyNominal() == C.getOptionalDecl();
537-
538-
if (useIfPresentVariant) {
539-
// The type we request out of decodeIfPresent needs to be unwrapped
540-
// one level.
541-
// e.g. String? => decodeIfPresent(String.self, forKey: ...), not
542-
// decodeIfPresent(String?.self, forKey: ...)
543-
auto boundOptionalType =
544-
dyn_cast<BoundGenericType>(varType->getCanonicalType());
545-
varType = boundOptionalType->getGenericArgs()[0];
527+
bool useIfPresentVariant = false;
528+
529+
if (auto objType = varType->getOptionalObjectType()) {
530+
varType = objType;
531+
useIfPresentVariant = true;
546532
}
547533

548534
return std::make_tuple(vd, varType, useIfPresentVariant);

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static bool allAssociatedValuesConformToProtocol(DeclContext *DC,
5050
continue;
5151

5252
for (auto param : *PL) {
53-
auto type = param->getType()->mapTypeOutOfContext();
53+
auto type = param->getInterfaceType();
5454
if (!TypeChecker::conformsToProtocol(DC->mapTypeIntoContext(type),
5555
protocol, DC,
5656
ConformanceCheckFlags::Used)) {
@@ -73,9 +73,9 @@ static bool allStoredPropertiesConformToProtocol(DeclContext *DC,
7373
auto storedProperties =
7474
theStruct->getStoredProperties(/*skipInaccessible=*/true);
7575
for (auto propertyDecl : storedProperties) {
76-
if (!propertyDecl->hasType())
76+
if (!propertyDecl->hasInterfaceType())
7777
lazyResolver->resolveDeclSignature(propertyDecl);
78-
if (!propertyDecl->hasType())
78+
if (!propertyDecl->hasInterfaceType())
7979
return false;
8080

8181
auto type = propertyDecl->getValueInterfaceType();

0 commit comments

Comments
 (0)