Skip to content

RequirementMachine: Tighten up type witness recursion hack #39673

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
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
14 changes: 13 additions & 1 deletion lib/AST/RequirementMachine/PropertyMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,19 @@ void PropertyMap::concretizeNestedTypesFromConcreteParent(

MutableTerm constraintType;

if (concreteType == typeWitness &&
auto simplify = [&](CanType t) -> CanType {
return CanType(t.transformRec([&](Type t) -> Optional<Type> {
if (!t->isTypeParameter())
return None;

auto term = getRelativeTermForType(t->getCanonicalType(),
substitutions, Context);
System.simplify(term);
return Context.getTypeForTerm(term, { }, Protos);
}));
};

if (simplify(concreteType) == simplify(typeWitness) &&
requirementKind == RequirementKind::SameType) {
// FIXME: ConcreteTypeInDomainMap should support substitutions so
// that we can remove this.
Expand Down
10 changes: 6 additions & 4 deletions lib/AST/RequirementMachine/PropertyMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class PropertyBag {
/// Out-of-line methods are documented in PropertyMap.cpp.
class PropertyMap {
RewriteContext &Context;
RewriteSystem &System;
std::vector<PropertyBag *> Entries;
Trie<PropertyBag *, MatchKind::Longest> Trie;

Expand All @@ -156,10 +157,11 @@ class PropertyMap {
PropertyMap &operator=(PropertyMap &&) = delete;

public:
explicit PropertyMap(RewriteContext &ctx,
const ProtocolGraph &protos)
: Context(ctx), Protos(protos) {
Debug = ctx.getDebugOptions();
explicit PropertyMap(RewriteSystem &system)
: Context(system.getRewriteContext()),
System(system),
Protos(system.getProtocols()) {
Debug = Context.getDebugOptions();
}

~PropertyMap();
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/RequirementMachine/RequirementMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ void RequirementMachine::dump(llvm::raw_ostream &out) const {
}

RequirementMachine::RequirementMachine(RewriteContext &ctx)
: Context(ctx), System(ctx), Map(ctx, System.getProtocols()) {
: Context(ctx), System(ctx), Map(System) {
auto &langOpts = ctx.getASTContext().LangOpts;
Dump = langOpts.DumpRequirementMachine;
RequirementMachineStepLimit = langOpts.RequirementMachineStepLimit;
Expand Down
33 changes: 33 additions & 0 deletions test/Generics/rdar83894546.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: %target-typecheck-verify-swift

public protocol P1 {
associatedtype A: P1 where A.A == A
}

public protocol P2 {
associatedtype B: P1
associatedtype C: P2
}

public struct G<B : P1> : P2 {
public typealias C = G<B.A>
}

// C == G<B> together with the definition of G<B>.C implies an infinite series
// of rules:
// - C.C == G<B.A>
// - C.C.C == G<B.A.A>
// - C.C.C.C == G<B.A.A.A>
// - ...
//
// This would normally prevent the completion procedure from terminating,
// however A.A == A in protocol P1 means that the right hand sides simplify
// as follows:
//
// - C.C == G<B.A>
// - C.C.C == B<G.A>
// - C.C.C.C == G<B.A>
// - ...
//
// Therefore, the single rule C.C == C.C.C suffices to "tie off" the recursion.
public protocol P3: P2 where C == G<B> {}