Skip to content
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
41 changes: 32 additions & 9 deletions lib/AST/RequirementMachine/GeneratingConformances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,11 @@ namespace {
/// Utility class to encapsulate some shared state.
class GeneratingConformances {
const RewriteSystem &System;

RewriteContext &Context;

DebugOptions Debug;

// All conformance rules, sorted by (isExplicit(), getLHS()), with non-explicit
// rules with longer left hand sides coming first.
//
Expand Down Expand Up @@ -209,6 +212,7 @@ class GeneratingConformances {
llvm::DenseSet<unsigned> &redundantConformances)
: System(system),
Context(system.getRewriteContext()),
Debug(system.getDebugOptions()),
RedundantConformances(redundantConformances) {}

void collectConformanceRules();
Expand Down Expand Up @@ -481,7 +485,7 @@ void GeneratingConformances::computeCandidateConformancePaths() {
if (result.empty())
continue;

if (System.getDebugOptions().contains(DebugFlags::GeneratingConformances)) {
if (Debug.contains(DebugFlags::GeneratingConformances)) {
llvm::dbgs() << "Candidate homotopy generator: ";
loop.dump(llvm::dbgs(), System);
llvm::dbgs() << "\n";
Expand All @@ -497,7 +501,7 @@ void GeneratingConformances::computeCandidateConformancePaths() {
if (inEmptyContext.empty())
continue;

if (System.getDebugOptions().contains(DebugFlags::GeneratingConformances)) {
if (Debug.contains(DebugFlags::GeneratingConformances)) {
llvm::dbgs() << "* Protocol " << proto->getName() << ":\n";
llvm::dbgs() << "** Conformance rules not in context:\n";
for (unsigned ruleID : inEmptyContext) {
Expand Down Expand Up @@ -755,7 +759,7 @@ void GeneratingConformances::verifyGeneratingConformanceEquations() const {
llvm::errs() << "Mismatched conformance:\n";
llvm::errs() << "Base rule: " << rule << "\n";
llvm::errs() << "Final rule: " << otherRule << "\n\n";
System.dump(llvm::errs());
dumpGeneratingConformanceEquations(llvm::errs());
abort();
}

Expand All @@ -772,7 +776,7 @@ void GeneratingConformances::verifyGeneratingConformanceEquations() const {
pair.first, pair.second);
llvm::errs() << "\n";
llvm::errs() << "Term: " << rule << "\n";
System.dump(llvm::errs());
dumpGeneratingConformanceEquations(llvm::errs());
abort();
}

Expand All @@ -789,7 +793,7 @@ void GeneratingConformances::verifyGeneratingConformanceEquations() const {
llvm::errs() << "Invalid conformance path:\n";
llvm::errs() << "Expected: " << baseTerm << "\n";
llvm::errs() << "Got: " << otherTerm << "\n\n";
System.dump(llvm::errs());
dumpGeneratingConformanceEquations(llvm::errs());
abort();
}
}
Expand All @@ -811,7 +815,12 @@ void GeneratingConformances::computeGeneratingConformances(
bool derivedViaConcrete = false;
for (const auto &path : paths) {
if (path.empty())
break;
continue;

// If the rule is itself a concrete conformance, it is not
// derived-via-concrete via itself.
if (path.size() == 1 && path.front() == ruleID)
continue;

if (System.getRule(path.back()).getLHS().back().getKind() ==
Symbol::Kind::ConcreteConformance) {
Expand All @@ -824,6 +833,12 @@ void GeneratingConformances::computeGeneratingConformances(
// considered in the second pass.
if (!derivedViaConcrete)
continue;

if (Debug.contains(DebugFlags::GeneratingConformances)) {
llvm::dbgs() << "Derived-via-concrete: ";
dumpGeneratingConformanceEquation(llvm::dbgs(), ruleID, paths);
llvm::dbgs() << "\n";
}
} else {
// Ignore rules already determined to be redundant by the first pass.
if (RedundantConformances.count(ruleID) > 0)
Expand All @@ -843,6 +858,14 @@ void GeneratingConformances::computeGeneratingConformances(

if (isValidConformancePath(visited, path,
/*allowConcrete=*/true)) {
if (Debug.contains(DebugFlags::GeneratingConformances)) {
llvm::dbgs() << "Redundant rule in ";
llvm::dbgs() << (firstPass ? "first" : "second");
llvm::dbgs() << " pass: ";
llvm::dbgs() << System.getRule(ruleID).getLHS();
llvm::dbgs() << "\n";
}

RedundantConformances.insert(ruleID);
break;
}
Expand All @@ -869,7 +892,7 @@ void GeneratingConformances::verifyGeneratingConformances() const {
/*allowConcrete=*/true)) {
llvm::errs() << "Redundant conformance is not recoverable:\n";
llvm::errs() << rule << "\n\n";
System.dump(llvm::errs());
dumpGeneratingConformanceEquations(llvm::errs());
abort();
}

Expand All @@ -879,14 +902,14 @@ void GeneratingConformances::verifyGeneratingConformances() const {
if (rule.isRedundant()) {
llvm::errs() << "Generating conformance is redundant: ";
llvm::errs() << rule << "\n\n";
System.dump(llvm::errs());
dumpGeneratingConformanceEquations(llvm::errs());
abort();
}

if (rule.getLHS().containsUnresolvedSymbols()) {
llvm::errs() << "Generating conformance contains unresolved symbols: ";
llvm::errs() << rule << "\n\n";
System.dump(llvm::errs());
dumpGeneratingConformanceEquations(llvm::errs());
abort();
}
}
Expand Down
5 changes: 5 additions & 0 deletions lib/AST/RequirementMachine/RewriteContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,9 @@ void RewriteContext::getProtocolComponentRec(

/// Lazily construct a requirement machine for the given protocol's strongly
/// connected component (SCC) in the protocol dependency graph.
///
/// This can only be called once, to prevent multiple requirement machines
/// for being built with the same component.
ArrayRef<const ProtocolDecl *> RewriteContext::getProtocolComponent(
const ProtocolDecl *proto) {
auto found = Protos.find(proto);
Expand All @@ -650,6 +653,8 @@ ArrayRef<const ProtocolDecl *> RewriteContext::getProtocolComponent(
abort();
}

component.InProgress = true;

return component.Protos;
}

Expand Down
14 changes: 14 additions & 0 deletions test/Generics/inherited_concrete_conformance_in_protocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures -requirement-machine-protocol-signatures=on 2>&1 | %FileCheck %s

protocol Sequence {}

protocol Collection : Sequence {}

struct MyCollection : Collection {}

// CHECK-LABEL: inherited_concrete_conformance_in_protocol.(file).P@
// CHECK-LABEL: Requirement signature: <Self where Self.T == MyCollection>

protocol P {
associatedtype T : Collection where T == MyCollection
}