Skip to content

Commit 07d145a

Browse files
authored
Merge pull request #72206 from tshortli/conformance-complete-assertion
2 parents eff456f + 109860c commit 07d145a

File tree

10 files changed

+197
-110
lines changed

10 files changed

+197
-110
lines changed

include/swift/AST/Decl.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5074,6 +5074,35 @@ enum class KnownDerivableProtocolKind : uint8_t {
50745074

50755075
using PrimaryAssociatedTypeName = std::pair<Identifier, SourceLoc>;
50765076

5077+
/// A wrapper for a dictionary that maps Obj-C protocol requirement selectors to
5078+
/// a list of function decls.
5079+
class ObjCRequirementMap {
5080+
public:
5081+
using FunctionList = TinyPtrVector<AbstractFunctionDecl *>;
5082+
5083+
private:
5084+
using MethodKey = std::pair<ObjCSelector, char>;
5085+
llvm::SmallDenseMap<MethodKey, FunctionList, 4> storage;
5086+
5087+
static MethodKey getObjCMethodKey(AbstractFunctionDecl *func);
5088+
5089+
public:
5090+
void addRequirement(AbstractFunctionDecl *requirement) {
5091+
storage[getObjCMethodKey(requirement)].push_back(requirement);
5092+
}
5093+
5094+
/// Retrieve the Objective-C requirements in this protocol that have the
5095+
/// given Objective-C method key.
5096+
FunctionList getRequirements(AbstractFunctionDecl *requirement) const {
5097+
auto key = getObjCMethodKey(requirement);
5098+
auto known = storage.find(key);
5099+
if (known == storage.end())
5100+
return {};
5101+
5102+
return known->second;
5103+
}
5104+
};
5105+
50775106
/// ProtocolDecl - A declaration of a protocol, for example:
50785107
///
50795108
/// protocol Drawable {
@@ -5303,6 +5332,10 @@ class ProtocolDecl final : public NominalTypeDecl {
53035332
/// i.e., for a protocol P, returns the kind if inverse constraint ~P exists.
53045333
std::optional<InvertibleProtocolKind> getInvertibleProtocolKind() const;
53055334

5335+
/// Returns a dictionary that maps Obj-C protocol requirement selectors to a
5336+
/// list of function decls.
5337+
ObjCRequirementMap getObjCRequiremenMap() const;
5338+
53065339
private:
53075340
void computeKnownProtocolKind() const;
53085341

include/swift/AST/TypeCheckRequests.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4828,6 +4828,23 @@ class LocalTypeDeclsRequest
48284828
bool isCached() const { return true; }
48294829
};
48304830

4831+
class ObjCRequirementMapRequest
4832+
: public SimpleRequest<ObjCRequirementMapRequest,
4833+
ObjCRequirementMap(const ProtocolDecl *proto),
4834+
RequestFlags::Cached> {
4835+
public:
4836+
using SimpleRequest::SimpleRequest;
4837+
4838+
private:
4839+
friend SimpleRequest;
4840+
4841+
ObjCRequirementMap evaluate(Evaluator &evaluator,
4842+
const ProtocolDecl *proto) const;
4843+
4844+
public:
4845+
bool isCached() const { return true; }
4846+
};
4847+
48314848
#define SWIFT_TYPEID_ZONE TypeChecker
48324849
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"
48334850
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,3 +559,7 @@ SWIFT_REQUEST(TypeChecker, UniqueUnderlyingTypeSubstitutionsRequest,
559559
SWIFT_REQUEST(TypeChecker, LocalTypeDeclsRequest,
560560
ArrayRef<TypeDecl *>(SourceFile *),
561561
Cached, NoLocationInfo)
562+
SWIFT_REQUEST(TypeChecker, ObjCRequirementMapRequest,
563+
ObjCRequirementMap(const ProtocolDecl *proto),
564+
Cached, NoLocationInfo)
565+

lib/AST/Decl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6467,6 +6467,15 @@ ProtocolDecl::getInvertibleProtocolKind() const {
64676467
return std::nullopt;
64686468
}
64696469

6470+
ObjCRequirementMap ProtocolDecl::getObjCRequiremenMap() const {
6471+
ObjCRequirementMap defaultMap;
6472+
if (!isObjC())
6473+
return defaultMap;
6474+
6475+
return evaluateOrDefault(getASTContext().evaluator,
6476+
ObjCRequirementMapRequest{this}, defaultMap);
6477+
}
6478+
64706479
ArrayRef<ProtocolDecl *> ProtocolDecl::getInheritedProtocols() const {
64716480
auto *mutThis = const_cast<ProtocolDecl *>(this);
64726481
return evaluateOrDefault(getASTContext().evaluator,

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,6 @@ bool ClangImporter::addHeaderDependencies(
455455
ModuleDependencyID moduleID,
456456
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
457457
ModuleDependenciesCache &cache) {
458-
auto &ctx = Impl.SwiftContext;
459458
auto optionalTargetModule = cache.findDependency(moduleID);
460459
assert(optionalTargetModule.has_value());
461460
auto targetModule = *(optionalTargetModule.value());

lib/Sema/AssociatedTypeInference.cpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,23 +1172,6 @@ AssociatedTypeInference::AssociatedTypeInference(
11721172
: ctx(ctx), conformance(conformance), proto(conformance->getProtocol()),
11731173
dc(conformance->getDeclContext()), adoptee(conformance->getType()) {}
11741174

1175-
static bool associatedTypesAreSameEquivalenceClass(AssociatedTypeDecl *a,
1176-
AssociatedTypeDecl *b) {
1177-
if (a == b)
1178-
return true;
1179-
1180-
// TODO: Do a proper equivalence check here by looking for some relationship
1181-
// between a and b's protocols. In practice today, it's unlikely that
1182-
// two same-named associated types can currently be independent, since we
1183-
// don't have anything like `@implements(P.foo)` to rename witnesses (and
1184-
// we still fall back to name lookup for witnesses in more cases than we
1185-
// should).
1186-
if (a->getName() == b->getName())
1187-
return true;
1188-
1189-
return false;
1190-
}
1191-
11921175
namespace {
11931176

11941177
/// Try to avoid situations where resolving the type of a witness calls back
@@ -1428,8 +1411,6 @@ static InferenceCandidateKind checkInferenceCandidate(
14281411
NormalProtocolConformance *conformance,
14291412
ValueDecl *witness,
14301413
Type selfTy) {
1431-
auto &ctx = selfTy->getASTContext();
1432-
14331414
// The unbound form of `Self.A`.
14341415
auto selfAssocTy = DependentMemberType::get(selfTy, result->first->getName());
14351416
auto genericSig = witness->getInnermostDeclContext()
@@ -1855,16 +1836,6 @@ AssociatedTypeInference::inferTypeWitnessesViaValueWitnesses(
18551836
return result;
18561837
}
18571838

1858-
/// Map error types back to their original types.
1859-
static Type mapErrorTypeToOriginal(Type type) {
1860-
if (auto errorType = type->getAs<ErrorType>()) {
1861-
if (auto originalType = errorType->getOriginalType())
1862-
return originalType.transform(mapErrorTypeToOriginal);
1863-
}
1864-
1865-
return type;
1866-
}
1867-
18681839
/// Desugar protocol type aliases, since they can cause request cycles in
18691840
/// type resolution if printed in a module interface and parsed back in.
18701841
static Type getWithoutProtocolTypeAliases(Type type) {
@@ -1937,8 +1908,6 @@ static Type getWitnessTypeForMatching(NormalProtocolConformance *conformance,
19371908
LookUpConformanceInModule(module));
19381909
}
19391910

1940-
auto &ctx = conformance->getDeclContext()->getASTContext();
1941-
19421911
auto proto = conformance->getProtocol();
19431912
auto selfTy = proto->getSelfInterfaceType();
19441913

@@ -2906,8 +2875,6 @@ AssociatedTypeDecl *AssociatedTypeInference::inferAbstractTypeWitnesses(
29062875
// not resolve otherwise.
29072876
llvm::SmallVector<AbstractTypeWitness, 2> abstractTypeWitnesses;
29082877

2909-
auto selfTypeInContext = dc->getSelfTypeInContext();
2910-
29112878
TypeWitnessSystem system(unresolvedAssocTypes);
29122879
collectAbstractTypeWitnesses(system, unresolvedAssocTypes);
29132880

lib/Sema/CSDiagnostics.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8202,7 +8202,6 @@ bool UnableToInferClosureReturnType::diagnoseAsError() {
82028202

82038203
bool UnableToInferGenericPackElementType::diagnoseAsError() {
82048204
auto *locator = getLocator();
8205-
auto path = locator->getPath();
82068205

82078206
auto packElementElt = locator->getLastElementAs<LocatorPathElt::PackElement>();
82088207
assert(packElementElt && "Expected path to end with a pack element locator");

0 commit comments

Comments
 (0)