Skip to content

[5.7] RequirementMachine: Teach concrete contraction about nested associated types #58813

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
319 changes: 167 additions & 152 deletions lib/AST/RequirementMachine/ConcreteContraction.cpp

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion lib/AST/RequirementMachine/RequirementMachineRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ RequirementSignatureRequestRQM::evaluate(Evaluator &evaluator,
rewriteCtx.finishComputingRequirementSignatures(proto);
};

SmallVector<RequirementError, 4> errors;

// Collect user-written requirements from the protocols in this connected
// component.
llvm::DenseMap<const ProtocolDecl *,
Expand All @@ -297,6 +299,20 @@ RequirementSignatureRequestRQM::evaluate(Evaluator &evaluator,
requirements.push_back(req);
for (auto req : proto->getTypeAliasRequirements())
requirements.push_back({req, SourceLoc(), /*inferred=*/false});

// Preprocess requirements to eliminate conformances on type parameters
// which are made concrete.
if (ctx.LangOpts.EnableRequirementMachineConcreteContraction) {
SmallVector<StructuralRequirement, 4> contractedRequirements;

bool debug = rewriteCtx.getDebugOptions()
.contains(DebugFlags::ConcreteContraction);

if (performConcreteContraction(requirements, contractedRequirements,
errors, debug)) {
std::swap(contractedRequirements, requirements);
}
}
}

if (rewriteCtx.getDebugOptions().contains(DebugFlags::Timers)) {
Expand Down Expand Up @@ -409,7 +425,6 @@ RequirementSignatureRequestRQM::evaluate(Evaluator &evaluator,
// Diagnose redundant requirements and conflicting requirements.
if (ctx.LangOpts.RequirementMachineProtocolSignatures ==
RequirementMachineMode::Enabled) {
SmallVector<RequirementError, 4> errors;
machine->computeRequirementDiagnostics(errors, proto->getLoc());
diagnoseRequirementErrors(ctx, errors,
AllowConcreteTypePolicy::NestedAssocTypes);
Expand Down
15 changes: 15 additions & 0 deletions lib/AST/RequirementMachine/RuleBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ void RuleBuilder::initWithProtocolSignatureRequirements(
addPermanentProtocolRules(proto);

auto reqs = proto->getRequirementSignature();

// If completion failed, we'll have a totally empty requirement signature,
// but to maintain invariants around what constitutes a valid rewrite term
// between getTypeForTerm() and isValidTypeInContext(), we need to add rules
// for inherited protocols.
if (reqs.getErrors().contains(GenericSignatureErrorFlags::CompletionFailed)) {
for (auto *inheritedProto : Context.getInheritedProtocols(proto)) {
Requirement req(RequirementKind::Conformance,
proto->getSelfInterfaceType(),
inheritedProto->getDeclaredInterfaceType());

addRequirement(req.getCanonical(), proto);
}
}

for (auto req : reqs.getRequirements())
addRequirement(req.getCanonical(), proto);
for (auto alias : reqs.getTypeAliases())
Expand Down
29 changes: 26 additions & 3 deletions test/Generics/concrete_conformances_in_protocol.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures 2>&1 | %FileCheck %s

// rdar://problem/88135912
// XFAIL: *

protocol P {
associatedtype T
}
Expand All @@ -15,6 +13,7 @@ struct S : P {

protocol R0 {
associatedtype A where A : P, A == S
// expected-warning@-1 {{redundant conformance constraint 'S' : 'P'}}
}

////
Expand All @@ -27,13 +26,15 @@ struct G<T> : P {}
protocol R1 {
associatedtype A
associatedtype B where B : P, B == G<A>
// expected-warning@-1 {{redundant conformance constraint 'G<Self.A>' : 'P'}}
}

// CHECK-LABEL: concrete_conformances_in_protocol.(file).R2@
// CHECK-LABEL: Requirement signature: <Self where Self.[R2]A == G<Self.[R2]B>>

protocol R2 {
associatedtype A where A : P, A == G<B>
// expected-warning@-1 {{redundant conformance constraint 'G<Self.B>' : 'P'}}
associatedtype B
}

Expand All @@ -51,13 +52,15 @@ struct GG<T : P> : PP {}
protocol RR3 {
associatedtype A : P
associatedtype B where B : PP, B == GG<A>
// expected-warning@-1 {{redundant conformance constraint 'GG<Self.A>' : 'PP'}}
}

// CHECK-LABEL: concrete_conformances_in_protocol.(file).RR4@
// CHECK-LABEL: Requirement signature: <Self where Self.[RR4]A == GG<Self.[RR4]B>, Self.[RR4]B : P>

protocol RR4 {
associatedtype A where A : PP, A == GG<B>
// expected-warning@-1 {{redundant conformance constraint 'GG<Self.B>' : 'PP'}}
associatedtype B : P
}

Expand All @@ -67,12 +70,32 @@ protocol RR4 {
protocol RR5 {
associatedtype A : PP
associatedtype B where B : PP, B == GG<A.T>
// expected-warning@-1 {{redundant conformance constraint 'GG<Self.A.T>' : 'PP'}}
}

// CHECK-LABEL: concrete_conformances_in_protocol.(file).RR6@
// CHECK-LABEL: Requirement signature: <Self where Self.[RR6]A == GG<Self.[RR6]B.[PP]T>, Self.[RR6]B : PP>

protocol RR6 {
associatedtype A where A : PP, A == GG<B.T>
// expected-warning@-1 {{redundant conformance constraint 'GG<Self.B.T>' : 'PP'}}
associatedtype B : PP
}

protocol P1 {
associatedtype T : P1
}

struct GGG<U : P1> : P1 {
typealias T = GGG<GGG<U>>
}

// CHECK-LABEL: concrete_conformances_in_protocol.(file).P2@
// CHECK-LABEL: Requirement signature: <Self where Self.[P2]T == GGG<Self.[P2]U>, Self.[P2]U : P1>

protocol P2 {
associatedtype T : P1 where T == GGG<U>
// expected-warning@-1 {{redundant conformance constraint 'GGG<Self.U>' : 'P1'}}
associatedtype U : P1
}

2 changes: 1 addition & 1 deletion test/Generics/concrete_same_type_versus_anyobject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension G2 where U == S, U : AnyObject {}
// CHECK: ExtensionDecl line={{.*}} base=G2
// CHECK-NEXT: Generic signature: <U where U == C>
extension G2 where U == C, U : AnyObject {}
// expected-warning@-1 {{redundant constraint 'C' : 'AnyObject'}}
// expected-warning@-1 {{redundant constraint 'U' : 'AnyObject'}}

// CHECK: ExtensionDecl line={{.*}} base=G2
// CHECK-NEXT: Generic signature: <U where U : C>
Expand Down
18 changes: 11 additions & 7 deletions test/Generics/conditional_requirement_inference_in_protocol.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=verify -requirement-machine-inferred-signatures=verify
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures -requirement-machine-protocol-signatures=verify -requirement-machine-inferred-signatures=verify 2>&1 | %FileCheck %s
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on 2>&1 | %FileCheck %s

// FIXME: The redundant conformance warnings here should not be emitted, since
// these requirements participate in conditional requirement inference.

// CHECK-LABEL: conditional_requirement_inference_in_protocol.(file).Good@
// CHECK-LABEL: Requirement signature: <Self where Self.[Good]T == [Self.[Good]U], Self.[Good]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}}
associatedtype T : Equatable // expected-warning {{redundant conformance constraint 'Array<Self.U>' : 'Equatable'}}
associatedtype U : Equatable where T == Array<U>
// expected-warning@-1 {{redundant conformance constraint 'Self.U' : 'Equatable'}}
}

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

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}}
associatedtype T : Equatable // expected-warning {{redundant conformance constraint 'Array<Self.U>' : 'Equatable'}}
associatedtype U where T == Array<U>
}
16 changes: 6 additions & 10 deletions test/Generics/derived_via_concrete_in_protocol.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=off
// RUN: %target-swift-frontend -debug-generic-signatures -typecheck %s -requirement-machine-protocol-signatures=off 2>&1 | %FileCheck %s

// FIXME: Implement concrete contraction for the Requirement Machine's
// RequirementSignatureRequest to get this to pass without turning off
// -requirement-machine-protocol-signatures.
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=on
// RUN: %target-swift-frontend -debug-generic-signatures -typecheck %s -requirement-machine-protocol-signatures=on 2>&1 | %FileCheck %s

protocol P24 {
associatedtype C: P20
Expand All @@ -29,8 +25,8 @@ class X3 { }
// CHECK-NEXT: Requirement signature: <Self where Self.[P25a]A == X24<Self.[P25a]B>, Self.[P25a]B : P20>
// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.[P25a]A == X24<τ_0_0.[P25a]B>, τ_0_0.[P25a]B : P20>
protocol P25a {
associatedtype A: P24 // expected-warning{{redundant conformance constraint 'Self.A' : 'P24'}}
associatedtype B: P20 where A == X24<B> // expected-note{{conformance constraint 'Self.A' : 'P24' implied here}}
associatedtype A: P24 // expected-warning{{redundant conformance constraint 'X24<Self.B>' : 'P24'}}
associatedtype B: P20 where A == X24<B>
}

// CHECK-LABEL: .P25b@
Expand All @@ -45,9 +41,9 @@ protocol P25b {
// CHECK-NEXT: Requirement signature: <Self where Self.[P27a]A == X26<Self.[P27a]B>, Self.[P27a]B : X3>
// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.[P27a]A == X26<τ_0_0.[P27a]B>, τ_0_0.[P27a]B : X3>
protocol P27a {
associatedtype A: P26 // expected-warning{{redundant conformance constraint 'Self.A' : 'P26'}}
associatedtype A: P26 // expected-warning{{redundant conformance constraint 'X26<Self.B>' : 'P26'}}

associatedtype B: X3 where A == X26<B> // expected-note{{conformance constraint 'Self.A' : 'P26' implied here}}
associatedtype B: X3 where A == X26<B>
}

// CHECK-LABEL: .P27b@
Expand Down
11 changes: 6 additions & 5 deletions test/Generics/non_confluent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ struct S<U : P1> : P1 {
typealias T = S<S<U>>
}

protocol P3 {
protocol P3Base {
associatedtype T : P1
associatedtype U : P1
}

protocol P3 : P3Base where T == S<U> {
// expected-error@-1 {{cannot build rewrite system for protocol; rule length limit exceeded}}
// expected-note@-2 {{failed rewrite rule is [P3:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[concrete: S<S<S<S<S<S<S<S<S<S<S<S<S<S<[P3:U]>>>>>>>>>>>>>>] => [P3:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T].[P1:T]}}

associatedtype T : P1 where T == S<U>
// expected-error@-1 {{type 'Self.U' does not conform to protocol 'P1'}}
associatedtype U : P1
}
20 changes: 20 additions & 0 deletions test/Generics/rdar91594361.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures -requirement-machine-inferred-signatures=on 2>&1 | %FileCheck %s

protocol P1 {
associatedtype T
}

protocol P2 {
associatedtype T : P3
}

protocol P3 {}

struct G<T : P3> : P2 {}

extension P1 where T : P2 {
// CHECK-LABEL: .foo@
// CHECK-NEXT: <Self, X where Self : P1, X : P3, Self.[P1]T == G<X>>
func foo<X>(_: X) where T == G<X> {}
}

4 changes: 2 additions & 2 deletions test/Generics/requirement_inference.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=verify -requirement-machine-inferred-signatures=off
// RUN: not %target-swift-frontend -typecheck %s -debug-generic-signatures -requirement-machine-protocol-signatures=verify -requirement-machine-inferred-signatures=on 2>&1 | %FileCheck %s
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=verify
// RUN: not %target-swift-frontend -typecheck %s -debug-generic-signatures -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on 2>&1 | %FileCheck %s

protocol P1 {
func p1()
Expand Down
4 changes: 2 additions & 2 deletions test/Generics/requirement_machine_diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,18 +295,18 @@ func sameTypeConflicts() {
fatalError()
}

// expected-error@+1{{no type for 'T.Bar.Foo' can satisfy both 'T.Bar.Foo == X' and 'T.Bar.Foo == Z'}}
func fail4<T: Barrable>(_ t: T) -> (Y, Z)
where
T.Bar == Y,
T.Bar.Foo == Z {
// expected-error@-1{{generic signature requires types 'Y.Foo' (aka 'X') and 'Z' to be the same}}
fatalError()
}

// expected-error@+1{{no type for 'T.Bar.Foo' can satisfy both 'T.Bar.Foo == X' and 'T.Bar.Foo == Z'}}
func fail5<T: Barrable>(_ t: T) -> (Y, Z)
where
T.Bar.Foo == Z,
// expected-error@-1{{generic signature requires types 'Y.Foo' (aka 'X') and 'Z' to be the same}}
T.Bar == Y {
fatalError()
}
Expand Down
5 changes: 1 addition & 4 deletions test/attr/accessibility_where_clause.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift -requirement-machine-inferred-signatures=off
// RUN: %target-typecheck-verify-swift -requirement-machine-inferred-signatures=on

public class OuterClass {
class InnerClass {}
Expand All @@ -17,9 +17,6 @@ public protocol PublicProto2 {
// FIXME: Once we no longer do that, come up with another strategy
// to make the above diagnose.

// FIXME: Get this working with the Requirement Machine, or decide that it should
// be unsupported: rdar://90469477

extension PublicProto2 where Self.T : OuterClass, Self.U == Self.T.InnerClass {
public func cannotBePublic() {}
// expected-error@-1 {{cannot declare a public instance method in an extension with internal requirements}}
Expand Down
4 changes: 1 addition & 3 deletions validation-test/compiler_crashers_2_fixed/rdar56398071.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// RUN: %target-swift-frontend -primary-file %s -emit-ir -requirement-machine-protocol-signatures=off

// TODO: Get this to pass with -requirement-machine-protocol-signatures=on.
// RUN: %target-swift-frontend -primary-file %s -emit-ir -requirement-machine-protocol-signatures=on

public protocol WrappedSignedInteger: SignedInteger where Stride == Int {
typealias WrappedInteger = Int
Expand Down
4 changes: 1 addition & 3 deletions validation-test/compiler_crashers_2_fixed/sr11639.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// RUN: %target-swift-frontend -emit-ir -primary-file %s -debug-generic-signatures -requirement-machine-protocol-signatures=off 2>&1 | %FileCheck %s

// FIXME: Get this working with -requirement-machine-protocol-signatures=on again
// RUN: %target-swift-frontend -emit-ir -primary-file %s -debug-generic-signatures -requirement-machine-protocol-signatures=on 2>&1 | %FileCheck %s

public protocol FooProtocol {
associatedtype Bar
Expand Down