Skip to content

Commit

Permalink
Allow unavailable conformances to non-Sendaable protocols during cont…
Browse files Browse the repository at this point in the history
…raction

Refusing to acknowledge unavailable conformances at this point in the
requirement machine doesn't allow us to make use of unavailable
conformances from unavailable contexts, something which code currently
depends on. Limit the new filtering behavior to `Sendable` conformances
where we need them, as a narrow fix for this regression. We'll revisit
the approach here later.

Fixes rdar://94154905.
  • Loading branch information
DougGregor committed Jun 2, 2022
1 parent 6267513 commit 904a2ff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/AST/RequirementMachine/ConcreteContraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,16 @@ Optional<Type> ConcreteContraction::substTypeParameterRec(
auto *proto = assocType->getProtocol();
auto *module = proto->getParentModule();

bool allowUnavailable =
!proto->isSpecificProtocol(KnownProtocolKind::Sendable);
// The 'Sendable' protocol does not declare any associated types, so the
// 'allowMissing' value here is actually irrelevant.
auto conformance = ((*substBaseType)->isTypeParameter()
? ProtocolConformanceRef(proto)
: module->lookupConformance(
*substBaseType, proto,
/*allowMissing=*/false,
/*allowUnavailable=*/false));
allowUnavailable));

// The base type doesn't conform, in which case the requirement remains
// unsubstituted.
Expand Down Expand Up @@ -391,9 +393,11 @@ ConcreteContraction::substRequirement(const Requirement &req) const {
if (ConcreteTypes.count(stripBoundDependentMemberTypes(firstType)) > 0)
allowMissing = true;

bool allowUnavailable =
!proto->isSpecificProtocol(KnownProtocolKind::Sendable);
if (!substFirstType->isTypeParameter() &&
!module->lookupConformance(substFirstType, proto,
allowMissing, /*allowUnavailable=*/false)) {
allowMissing, allowUnavailable)) {
// Handle the case of <T where T : P, T : C> where C is a class and
// C does not conform to P by leaving the conformance requirement
// unsubstituted.
Expand Down
4 changes: 3 additions & 1 deletion test/Generics/superclass_constraint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,7 @@ extension Animal: Pony { }

public struct AnimalWrapper<Friend: Animal> { }

// CHECK: Generic signature: <Friend where Friend : Animal, Friend : Pony>
// FIXME: Generic signature: <Friend where Friend : Animal, Friend : Pony>
// Generic signature: <Friend where Friend : Animal>
extension AnimalWrapper: Pony where Friend: Pony { }
// expected-warning@-1{{redundant conformance constraint 'Animal' : 'Pony'}}
17 changes: 17 additions & 0 deletions test/decl/protocol/unavailable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %target-typecheck-verify-swift

protocol P { }

struct X { }

@available(*, unavailable)
extension X: P { }

struct Y<T: P> { }

@available(*, unavailable)
extension Y {
// Okay, because the unavailable conformance is used within an
// unavailable context.
init() where T == X { }
}

0 comments on commit 904a2ff

Please sign in to comment.