Skip to content

[Experiment] GSB: Disable conditional requirement inference in protocols #40466

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
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
15 changes: 13 additions & 2 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,9 @@ struct GenericSignatureBuilder::Implementation {
/// requirements.
bool RebuildingWithoutRedundantConformances = false;

/// Whether we are building a protocol requirement signature.
bool BuildingProtocolRequirementSignature = false;

/// A mapping of redundant explicit requirements to the best root requirement
/// that implies them. Built by computeRedundantRequirements().
using RedundantRequirementMap =
Expand Down Expand Up @@ -2313,6 +2316,11 @@ void GenericSignatureBuilder::addConditionalRequirements(
if (Impl->RebuildingWithoutRedundantConformances)
return;

// We do not perform requirement inference, including conditional requirement
// inference, inside protocols.
if (Impl->BuildingProtocolRequirementSignature)
return;

// Abstract conformances don't have associated decl-contexts/modules, but also
// don't have conditional requirements.
if (conformance.isConcrete()) {
Expand Down Expand Up @@ -3399,10 +3407,12 @@ void EquivalenceClass::modified(GenericSignatureBuilder &builder) {
}

GenericSignatureBuilder::GenericSignatureBuilder(
ASTContext &ctx)
ASTContext &ctx,
bool requirementSignature)
: Context(ctx), Diags(Context.Diags), Impl(new Implementation) {
if (auto *Stats = Context.Stats)
++Stats->getFrontendCounters().NumGenericSignatureBuilders;
Impl->BuildingProtocolRequirementSignature = requirementSignature;
}

GenericSignatureBuilder::GenericSignatureBuilder(
Expand Down Expand Up @@ -8833,7 +8843,8 @@ RequirementSignatureRequest::evaluate(Evaluator &evaluator,
}

auto buildViaGSB = [&]() {
GenericSignatureBuilder builder(proto->getASTContext());
GenericSignatureBuilder builder(proto->getASTContext(),
/*requirementSignature=*/true);

// Add all of the generic parameters.
for (auto gp : *proto->getGenericParams())
Expand Down
3 changes: 2 additions & 1 deletion lib/AST/GenericSignatureBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,8 @@ class GenericSignatureBuilder {

public:
/// Construct a new generic signature builder.
explicit GenericSignatureBuilder(ASTContext &ctx);
explicit GenericSignatureBuilder(ASTContext &ctx,
bool requirementSignature=false);
GenericSignatureBuilder(GenericSignatureBuilder &&);
~GenericSignatureBuilder();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures 2>&1 | %FileCheck %s

// CHECK-LABEL: conditional_requirement_inference_in_protocol.(file).Good@
// CHECK-LABEL: Requirement signature: <Self where Self.T == Array<Self.U>, Self.U : Equatable>

protocol Good {
associatedtype T : Equatable // expected-warning {{redundant conformance constraint 'Self.T' : 'Equatable'}}
associatedtype U : Equatable where T == Array<U> // expected-note {{conformance constraint 'Self.T' : 'Equatable' implied here}}
}

// CHECK-LABEL: conditional_requirement_inference_in_protocol.(file).Bad@
// CHECK-LABEL: Requirement signature: <Self where Self.T == Array<Self.U>>

protocol Bad {
associatedtype T : Equatable // expected-warning {{redundant conformance constraint 'Self.T' : 'Equatable'}}
associatedtype U where T == Array<U> // expected-note {{conformance constraint 'Self.T' : 'Equatable' implied here}}
}