Skip to content

Commit fee26b6

Browse files
authored
Merge pull request #42092 from slavapestov/rqm-even-more-comments
RequirementMachine: More comments and a regression test for a long-ago fixed crasher
2 parents ab1a989 + 8ea6338 commit fee26b6

File tree

6 files changed

+98
-6
lines changed

6 files changed

+98
-6
lines changed

lib/AST/RequirementMachine/GenericSignatureQueries.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,25 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
//
13-
// Implements the various operations on interface types in GenericSignature.
14-
// Use those methods instead of calling into the RequirementMachine directly.
13+
// The various generic signature query operations on GenericSignature will
14+
// lazily construct a requirement machine for the generic signature from the
15+
// RewriteContext, then call the methods in this file.
16+
//
17+
// If you're working elsewhere in the compiler, use the methods on
18+
// GenericSignature instead of calling into the RequirementMachine directly.
19+
//
20+
// Each query is generally implemented in the same manner:
21+
//
22+
// - First, convert the subject type parameter into a Term.
23+
// - Simplify the Term to obtain a canonical Term.
24+
// - Perform a property map lookup on the Term.
25+
// - Return the appropriate piece of information from the property map.
26+
//
27+
// A few are slightly different; for example, getCanonicalTypeInContext() takes
28+
// an arbitrary type, not just a type parameter, and recursively transforms the
29+
// type parameters it contains, if any.
30+
//
31+
// Also, getConformanceAccessPath() is another one-off operation.
1532
//
1633
//===----------------------------------------------------------------------===//
1734

lib/AST/RequirementMachine/HomotopyReduction.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
// 2) Small enough that no further rules can be deleted without changing the
2020
// resulting confluent rewrite system.
2121
//
22-
// Redundant rules that are not part of the minimal set are redundant are
23-
// detected by analyzing the set of rewrite loops computed by the completion
24-
// procedure. See RewriteLoop.cpp for a discussion of rewrite loops.
22+
// The main entry point here is RewriteSystem::minimizeRewriteSystem().
23+
//
24+
// Redundant rules are detected by analyzing the set of rewrite loops computed
25+
// by the completion procedure. See RewriteLoop.cpp for a discussion of rewrite
26+
// loops.
2527
//
2628
// If a rewrite rule appears exactly once in a loop and without context, the
2729
// loop witnesses a redundancy; the rewrite rule is equivalent to traveling

lib/AST/RequirementMachine/InterfaceType.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,28 @@
1313
// This file implements routines for converting Swift AST interface types to
1414
// rewrite system terms.
1515
//
16+
// A type parameter in Swift is a GenericTypeParamType wrapped in zero or
17+
// more DependentMemberTypes. DependentMemberTypes come in two flavors,
18+
// "unresolved" and "resolved". Unresolved DependentMemberTypes store an
19+
// identifier. Resolved DependentMemberTypes store an associated type
20+
// declaration.
21+
//
22+
// In the rewrite system, unresolved DependentMemberTypes map to name symbols;
23+
// resolved DependentMemberTypes map to associated type symbols.
24+
//
25+
// The mapping of the root generic parameter depends on the specific usage:
26+
//
27+
// - If the type is understood to be the subject type of a requirement in a
28+
// protocol, the root generic parameter, which must equal τ_0_0, maps to a
29+
// protocol symbol for the protocol in question.
30+
//
31+
// - If the type is part of a top-level generic signature, the root generic
32+
// parameter maps to the corresponding generic parameter symbol.
33+
//
34+
// - If the type was derived from a superclass or concrete type symbol, the
35+
// root generic parameter, which must equal τ_0_N for some N, maps to the
36+
// Nth substitution stored in the superclass or concrete type symbol.
37+
//
1638
// The rewrite system's reduction order differs from the canonical type order
1739
// used by Swift's ABI and name mangling. What this means in practice is that
1840
// converting a canonical type to a term does not necessarily produce a
@@ -36,6 +58,43 @@
3658
// their module names, if the unqualified names are the same). In the reduction
3759
// order, we want P1 < P2 to also hold if P1 inherits from P2.
3860
//
61+
// The following diagram shows the relationship between the two directions of
62+
// the type to term mapping:
63+
//
64+
// ---------------------
65+
// / Non-canonical Type /
66+
// ---------------------
67+
// |
68+
// v
69+
// +------------------+
70+
// | getTermForType() |
71+
// +------------------+
72+
// |
73+
// v
74+
// ---------------------
75+
// / Non-canonical Term /
76+
// ---------------------
77+
// |
78+
// v
79+
// +------------+
80+
// | simplify() |
81+
// +------------+
82+
// |
83+
// v
84+
// -----------------
85+
// / Canonical Term /
86+
// -----------------
87+
// |
88+
// v
89+
// +------------------+
90+
// | getTypeForTerm() |
91+
// +------------------+
92+
// |
93+
// v
94+
// -----------------
95+
// / Canonical Type /
96+
// -----------------
97+
//
3998
//===----------------------------------------------------------------------===//
4099

41100
#include "swift/AST/Decl.h"

lib/AST/RequirementMachine/RequirementBuilder.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
// This file implements the final step in generic signature minimization,
1414
// building requirements from a set of minimal, canonical rewrite rules.
1515
//
16+
// The main entry point is RequirementMachine::buildRequirementsFromRules(),
17+
// called from the RequirementSignatureRequest, AbstractGenericSignatureRequest
18+
// and InferredGenericSignatureRequest requests defined in
19+
// RequirementMachineRequests.cpp.
20+
//
1621
//===----------------------------------------------------------------------===//
1722

1823
#include "RequirementMachine.h"

lib/AST/RequirementMachine/RewriteContext.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@
151151
using namespace swift;
152152
using namespace rewriting;
153153

154-
/// Build a DebugOptions by parsing a comma-separated list of debug flags.
154+
/// Parse a comma-separated list from the -debug-requirement-machine= frontend
155+
/// flag.
155156
static DebugOptions parseDebugFlags(StringRef debugFlags) {
156157
DebugOptions result;
157158

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %target-swift-frontend -emit-ir %s
2+
3+
protocol DateGroupedCollection: Collection {
4+
typealias DictionaryType = [String: Int]
5+
6+
typealias Index = DictionaryType.Index
7+
typealias Element = DictionaryType.Element
8+
}

0 commit comments

Comments
 (0)