Skip to content

Commit 43a2086

Browse files
committed
Sema: Mark type witness candidates nonviable when contextual requirements are incompatible
1 parent 4bb4efc commit 43a2086

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3795,6 +3795,20 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
37953795
if (typeAliasDecl->getDeclaredInterfaceType()->is<UnboundGenericType>())
37963796
continue;
37973797

3798+
// A candidate is nonviable if its context has requirements not
3799+
// satisfied by those of the confomance context.
3800+
{
3801+
const auto candidateCtxSig = candidate.Member->getDeclContext()
3802+
->getGenericSignatureOfContext();
3803+
const auto conformanceCtxSig = DC->getGenericSignatureOfContext();
3804+
3805+
if(candidateCtxSig && conformanceCtxSig &&
3806+
!candidateCtxSig->requirementsNotSatisfiedBy(
3807+
conformanceCtxSig).empty()) {
3808+
continue;
3809+
}
3810+
}
3811+
37983812
// Check this type against the protocol requirements.
37993813
if (auto checkResult =
38003814
checkTypeWitness(DC, Proto, assocType, candidate.MemberType)) {

test/Generics/conditional_conformances.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ func sr_10992_bar(_ fn: (SR_10992_P) -> Void) {
433433

434434
// SR-7516
435435

436-
protocol SR_7516_P1 { associatedtype X = Void } // expected-note {{protocol requires nested type 'X'; do you want to add it?}}
436+
protocol SR_7516_P1 { associatedtype X = Void } // expected-note 4{{protocol requires nested type 'X'; do you want to add it?}}
437437
protocol SR_7516_P2 { associatedtype X = Bool }
438438
protocol SR_7516_P3 { associatedtype X = Void }
439439
protocol SR_7516_P4 { associatedtype X } // expected-note {{protocol requires nested type 'X'; do you want to add it?}}
@@ -514,3 +514,19 @@ extension SR_7516_S5: SR_7516_P1 {
514514
// DEFAULT_TYPE_WITNESS-NEXT: internal typealias X = Void
515515
}
516516
extension SR_7516_S5: SR_7516_P4 where T: Equatable {}
517+
518+
struct SR_7516_S6<T> {}
519+
extension SR_7516_S6: SR_7516_P2 where T == Void {}
520+
extension SR_7516_S6: SR_7516_P1 where T == Never { // expected-error {{type 'SR_7516_S6<T>' does not conform to protocol 'SR_7516_P1'}}
521+
}
522+
523+
struct SR_7516_S7<T> {}
524+
extension SR_7516_S7: SR_7516_P1 where T == Void {} // expected-error {{type 'SR_7516_S7<T>' does not conform to protocol 'SR_7516_P1'}}
525+
extension SR_7516_S7: SR_7516_P2 where T == Never {
526+
typealias X = Bool
527+
}
528+
529+
struct SR_7516_S8<T>: SR_7516_P1 {} // expected-error {{type 'SR_7516_S8<T>' does not conform to protocol 'SR_7516_P1'}}
530+
extension SR_7516_S8: SR_7516_P2 where T == Never {
531+
typealias X = Bool
532+
}

0 commit comments

Comments
 (0)