Skip to content

Commit ec60c88

Browse files
committed
Sema: Use requirement signature to check type witnesses
1 parent 18082b1 commit ec60c88

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3286,15 +3286,21 @@ ResolveWitnessResult ConformanceChecker::resolveWitnessViaDefault(
32863286
///
32873287
/// \returns an empty result on success, or a description of the error.
32883288
static CheckTypeWitnessResult checkTypeWitness(TypeChecker &tc, DeclContext *dc,
3289+
ProtocolDecl *proto,
32893290
AssociatedTypeDecl *assocType,
32903291
Type type) {
3291-
if (auto superclass = assocType->getSuperclass()) {
3292+
auto *moduleDecl = dc->getParentModule();
3293+
auto *reqtSig = assocType->getProtocol()->getRequirementSignature();
3294+
auto *depTy = DependentMemberType::get(proto->getSelfInterfaceType(),
3295+
assocType);
3296+
3297+
if (auto superclass = reqtSig->getSuperclassBound(depTy, *moduleDecl)) {
32923298
if (!superclass->isExactSuperclassOf(type))
32933299
return superclass->getAnyNominal();
32943300
}
32953301

32963302
// Check protocol conformances.
3297-
for (auto reqProto : assocType->getConformingProtocols()) {
3303+
for (auto reqProto : reqtSig->getConformsTo(depTy, *moduleDecl)) {
32983304
if (!tc.conformsToProtocol(type, reqProto, dc, None))
32993305
return reqProto;
33003306

@@ -3323,6 +3329,11 @@ static CheckTypeWitnessResult checkTypeWitness(TypeChecker &tc, DeclContext *dc,
33233329
/// Attempt to resolve a type witness via member name lookup.
33243330
ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
33253331
AssociatedTypeDecl *assocType) {
3332+
if (!Proto->isRequirementSignatureComputed()) {
3333+
Conformance->setInvalid();
3334+
return ResolveWitnessResult::Missing;
3335+
}
3336+
33263337
// Look for a member type with the same name as the associated type.
33273338
auto candidates = TC.lookupMemberType(DC, Adoptee, assocType->getName(),
33283339
NameLookupFlags::ProtocolMembers);
@@ -3342,7 +3353,7 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
33423353
continue;
33433354

33443355
// Check this type against the protocol requirements.
3345-
if (auto checkResult = checkTypeWitness(TC, DC, assocType,
3356+
if (auto checkResult = checkTypeWitness(TC, DC, Proto, assocType,
33463357
candidate.second)) {
33473358
auto reqProto = checkResult.getProtocolOrClass();
33483359
nonViable.push_back({candidate.first, reqProto});
@@ -3603,7 +3614,7 @@ ConformanceChecker::inferTypeWitnessesViaValueWitnesses(
36033614
if (!canInferFromOtherAssociatedType) {
36043615
// Check that the type witness meets the
36053616
// requirements on the associated type.
3606-
if (auto failed = checkTypeWitness(TC, DC, result.first,
3617+
if (auto failed = checkTypeWitness(TC, DC, Proto, result.first,
36073618
result.second)) {
36083619
witnessResult.NonViable.push_back(
36093620
std::make_tuple(result.first,result.second,failed));
@@ -4196,7 +4207,7 @@ void ConformanceChecker::resolveTypeWitnesses() {
41964207
if (!defaultType)
41974208
return Type();
41984209

4199-
if (auto failed = checkTypeWitness(TC, DC, assocType, defaultType)) {
4210+
if (auto failed = checkTypeWitness(TC, DC, Proto, assocType, defaultType)) {
42004211
// Record the failure, if we haven't seen one already.
42014212
if (!failedDefaultedAssocType) {
42024213
failedDefaultedAssocType = assocType;
@@ -4231,7 +4242,7 @@ void ConformanceChecker::resolveTypeWitnesses() {
42314242
return Type();
42324243

42334244
// Make sure that the derived type is sane.
4234-
if (checkTypeWitness(TC, DC, assocType, derivedType)) {
4245+
if (checkTypeWitness(TC, DC, Proto, assocType, derivedType)) {
42354246
diagnoseOrDefer(assocType, true,
42364247
[derivedType](NormalProtocolConformance *conformance) {
42374248
// FIXME: give more detail here?

validation-test/compiler_crashers/28748-genericenv-nullptr-too-much-circularity.swift renamed to validation-test/compiler_crashers_fixed/28748-genericenv-nullptr-too-much-circularity.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

88
// REQUIRES: asserts
9-
// RUN: not --crash %target-swift-frontend %s -emit-ir
9+
// RUN: not %target-swift-frontend %s -emit-ir
1010
protocol A:a{{}typealias e:a}{}class a:A{typealias e:b

0 commit comments

Comments
 (0)