Skip to content

Sema: Try not to let protocol type aliases leak into inferred type witnesses #71621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions lib/Sema/AssociatedTypeInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,6 @@ class AssociatedTypeInference {

/// Infer associated type witnesses for the given associated type.
InferredAssociatedTypesByWitnesses inferTypeWitnessesViaAssociatedType(
const llvm::SetVector<AssociatedTypeDecl *> &allUnresolved,
AssociatedTypeDecl *assocType);

/// Infer associated type witnesses for all relevant value requirements.
Expand Down Expand Up @@ -1790,8 +1789,7 @@ AssociatedTypeInference::inferTypeWitnessesViaValueWitnesses(
if (assocTypes.count(assocType) == 0)
continue;

auto reqInferred = inferTypeWitnessesViaAssociatedType(assocTypes,
assocType);
auto reqInferred = inferTypeWitnessesViaAssociatedType(assocType);
if (!reqInferred.empty())
result.push_back({req, std::move(reqInferred)});

Expand Down Expand Up @@ -1853,6 +1851,19 @@ static Type mapErrorTypeToOriginal(Type type) {
return type;
}

/// Desugar protocol type aliases, since they can cause request cycles in
/// type resolution if printed in a module interface and parsed back in.
static Type getWithoutProtocolTypeAliases(Type type) {
return type.transformRec([](TypeBase *t) -> llvm::Optional<Type> {
if (auto *aliasTy = dyn_cast<TypeAliasType>(t)) {
if (aliasTy->getDecl()->getDeclContext()->getExtendedProtocolDecl())
return getWithoutProtocolTypeAliases(aliasTy->getSinglyDesugaredType());
}

return llvm::None;
});
}

/// Produce the type when matching a witness.
///
/// If the witness is a member of the type itself or a superclass, we
Expand Down Expand Up @@ -1886,6 +1897,9 @@ static Type getWitnessTypeForMatching(NormalProtocolConformance *conformance,
conformance->getDeclContext()->mapTypeIntoContext(conformance->getType());
TypeSubstitutionMap substitutions = model->getMemberSubstitutions(witness);
Type type = witness->getInterfaceType()->getReferenceStorageReferent();

type = getWithoutProtocolTypeAliases(type);

LLVM_DEBUG(llvm::dbgs() << "Witness interface type is " << type << "\n";);

if (substitutions.empty())
Expand Down Expand Up @@ -1998,7 +2012,6 @@ static Type removeSelfParam(ValueDecl *value, Type type) {

InferredAssociatedTypesByWitnesses
AssociatedTypeInference::inferTypeWitnessesViaAssociatedType(
const llvm::SetVector<AssociatedTypeDecl *> &allUnresolved,
AssociatedTypeDecl *assocType) {
InferredAssociatedTypesByWitnesses result;

Expand Down
38 changes: 38 additions & 0 deletions test/ModuleInterface/protocol_extension_type_witness.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s -module-name protocol_extension_type_witness -enable-experimental-associated-type-inference
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface) -module-name protocol_extension_type_witness
// RUN: %FileCheck %s < %t.swiftinterface

// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s -module-name protocol_extension_type_witness -disable-experimental-associated-type-inference
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface) -module-name protocol_extension_type_witness
// RUN: %FileCheck %s < %t.swiftinterface

public protocol P {
associatedtype A
associatedtype B
associatedtype C
associatedtype D

func b(_: B)
func c(_: C)
func d(_: D)
}

extension P {
public typealias _Default_A = B
public typealias Alias = D

public func c(_: Alias) {}
}

public struct S<B>: P {
public func b(_: B) {}
public func d(_: String) {}
}

// CHECK-LABEL: public struct S<B> : protocol_extension_type_witness.P {
// CHECK-NEXT: public func b(_: B)
// CHECK-NEXT: public func d(_: Swift.String)
// CHECK-NEXT: public typealias A = B
// CHECK-NEXT: public typealias C = Swift.String
// CHECK-NEXT: public typealias D = Swift.String
// CHECK-NEXT: }
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %target-typecheck-verify-swift -enable-experimental-associated-type-inference
// RUN: not %target-typecheck-verify-swift -disable-experimental-associated-type-inference

// Reduced from the distributed actors implementation. This didn't type check in 5.10
// but works now.

protocol DistributedActorSystem<SerializationRequirement> {
associatedtype SerializationRequirement
}

protocol DistributedActor where SerializationRequirement == ActorSystem.SerializationRequirement {
associatedtype ActorSystem: DistributedActorSystem
associatedtype SerializationRequirement
}

class Worker<ActorSystem>: DistributedActor where ActorSystem: DistributedActorSystem, ActorSystem.SerializationRequirement == Int {}

struct FakeActorSystem: DistributedActorSystem {
typealias SerializationRequirement = Int
}

print(Worker<FakeActorSystem>.SerializationRequirement.self)