Skip to content

Commit a5a902a

Browse files
authored
Merge pull request swiftlang#24333 from slavapestov/small-conformance-cleanups
Small conformance checking-related cleanups
2 parents 9587a3a + fa12d85 commit a5a902a

15 files changed

+47
-67
lines changed

include/swift/AST/Decl.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,14 +3058,22 @@ class AssociatedTypeDecl : public AbstractTypeParamDecl {
30583058
return cast<ProtocolDecl>(getDeclContext());
30593059
}
30603060

3061+
/// Check if we have a default definition type.
3062+
bool hasDefaultDefinitionType() const {
3063+
// If we have a TypeRepr, return true immediately without kicking off
3064+
// a request.
3065+
return !DefaultDefinition.isNull() || getDefaultDefinitionType();
3066+
}
3067+
30613068
/// Retrieve the default definition type.
3062-
Type getDefaultDefinitionType() const {
3063-
return getDefaultDefinitionLoc().getType();
3069+
Type getDefaultDefinitionType() const;
3070+
3071+
TypeLoc &getDefaultDefinitionLoc() {
3072+
return DefaultDefinition;
30643073
}
30653074

3066-
TypeLoc &getDefaultDefinitionLoc();
30673075
const TypeLoc &getDefaultDefinitionLoc() const {
3068-
return const_cast<AssociatedTypeDecl *>(this)->getDefaultDefinitionLoc();
3076+
return DefaultDefinition;
30693077
}
30703078

30713079
/// Retrieve the trailing where clause for this associated type, if any.
@@ -3076,7 +3084,7 @@ class AssociatedTypeDecl : public AbstractTypeParamDecl {
30763084
TrailingWhere = trailingWhereClause;
30773085
}
30783086

3079-
/// Set the interface type of this associated type declaration to a dependen
3087+
/// Set the interface type of this associated type declaration to a dependent
30803088
/// member type of 'Self'.
30813089
void computeType();
30823090

include/swift/AST/LazyResolver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ class alignas(void*) LazyMemberLoader {
159159
SmallVectorImpl<ProtocolConformance *> &Conformances) = 0;
160160

161161
/// Returns the default definition type for \p ATD.
162-
virtual TypeLoc loadAssociatedTypeDefault(const AssociatedTypeDecl *ATD,
163-
uint64_t contextData) = 0;
162+
virtual Type loadAssociatedTypeDefault(const AssociatedTypeDecl *ATD,
163+
uint64_t contextData) = 0;
164164

165165
/// Returns the generic environment.
166166
virtual GenericEnvironment *loadGenericEnvironment(const DeclContext *decl,

include/swift/Serialization/ModuleFile.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,8 +827,8 @@ class ModuleFile
827827
loadAllConformances(const Decl *D, uint64_t contextData,
828828
SmallVectorImpl<ProtocolConformance*> &Conforms) override;
829829

830-
virtual TypeLoc loadAssociatedTypeDefault(const AssociatedTypeDecl *ATD,
831-
uint64_t contextData) override;
830+
virtual Type loadAssociatedTypeDefault(const AssociatedTypeDecl *ATD,
831+
uint64_t contextData) override;
832832

833833
virtual void finishNormalConformance(NormalProtocolConformance *conformance,
834834
uint64_t contextData) override;

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,9 +2321,9 @@ void PrintAST::visitAssociatedTypeDecl(AssociatedTypeDecl *decl) {
23212321
printInherited(decl);
23222322
}
23232323

2324-
if (!decl->getDefaultDefinitionLoc().isNull()) {
2324+
if (decl->hasDefaultDefinitionType()) {
23252325
Printer << " = ";
2326-
decl->getDefaultDefinitionLoc().getType().print(Printer, Options);
2326+
decl->getDefaultDefinitionType().print(Printer, Options);
23272327
}
23282328

23292329
// As with protocol's trailing where clauses, use the requirement signature

lib/AST/Decl.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3526,13 +3526,14 @@ void AssociatedTypeDecl::computeType() {
35263526
setInterfaceType(MetatypeType::get(interfaceTy, ctx));
35273527
}
35283528

3529-
TypeLoc &AssociatedTypeDecl::getDefaultDefinitionLoc() {
3529+
Type AssociatedTypeDecl::getDefaultDefinitionType() const {
35303530
if (Resolver) {
3531-
DefaultDefinition =
3532-
Resolver->loadAssociatedTypeDefault(this, ResolverContextData);
3533-
Resolver = nullptr;
3531+
const_cast<AssociatedTypeDecl *>(this)->DefaultDefinition
3532+
= TypeLoc::withoutLoc(
3533+
Resolver->loadAssociatedTypeDefault(this, ResolverContextData));
3534+
const_cast<AssociatedTypeDecl *>(this)->Resolver = nullptr;
35343535
}
3535-
return DefaultDefinition;
3536+
return DefaultDefinition.getType();
35363537
}
35373538

35383539
SourceRange AssociatedTypeDecl::getSourceRange() const {

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4282,7 +4282,7 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
42824282
source->kind == RequirementSource::RequirementSignatureSelf &&
42834283
!assocTypeDecl->getAttrs().hasAttribute<NonOverrideAttr>() &&
42844284
!assocTypeDecl->getAttrs().hasAttribute<OverrideAttr>() &&
4285-
assocTypeDecl->getDefaultDefinitionLoc().isNull() &&
4285+
!assocTypeDecl->hasDefaultDefinitionType() &&
42864286
(!assocTypeDecl->getInherited().empty() ||
42874287
assocTypeDecl->getTrailingWhereClause() ||
42884288
getASTContext().LangOpts.WarnImplicitOverrides);

lib/ClangImporter/ImporterImpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,8 +1261,8 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
12611261
uint64_t unused) override;
12621262

12631263
/// Returns the default definition type for \p ATD.
1264-
TypeLoc loadAssociatedTypeDefault(const AssociatedTypeDecl *ATD,
1265-
uint64_t contextData) override {
1264+
Type loadAssociatedTypeDefault(const AssociatedTypeDecl *ATD,
1265+
uint64_t contextData) override {
12661266
llvm_unreachable("unimplemented for ClangImporter");
12671267
}
12681268

lib/IDE/CodeCompletion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4360,7 +4360,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
43604360
continue;
43614361
// FIXME: Also exclude the type alias that has already been specified.
43624362
if (!Conformance->hasTypeWitness(ATD) ||
4363-
!ATD->getDefaultDefinitionLoc().isNull())
4363+
ATD->hasDefaultDefinitionType())
43644364
continue;
43654365
addTypeAlias(ATD,
43664366
DeclVisibilityKind::MemberOfProtocolImplementedByCurrentNominal);

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)