Skip to content

Commit 7a451a6

Browse files
authored
Merge pull request swiftlang#5704 from DougGregor/eliminate-witness-markers
2 parents 0f75036 + 15db826 commit 7a451a6

27 files changed

+191
-253
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 16 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -31,71 +31,6 @@ class ProtocolType;
3131
class Substitution;
3232
class SubstitutionMap;
3333

34-
/// Iterator that walks the generic parameter types declared in a generic
35-
/// signature and their dependent members.
36-
class GenericSignatureWitnessIterator {
37-
ArrayRef<Requirement> p;
38-
39-
void checkValid() const {
40-
assert(!p.empty() &&
41-
p.front().getKind() == RequirementKind::WitnessMarker);
42-
}
43-
44-
bool shouldSkip() const {
45-
return (!p.empty() &&
46-
p.front().getKind() != RequirementKind::WitnessMarker);
47-
}
48-
49-
public:
50-
GenericSignatureWitnessIterator() = default;
51-
GenericSignatureWitnessIterator(ArrayRef<Requirement> requirements)
52-
: p(requirements) {
53-
while (shouldSkip()) { p = p.slice(1); }
54-
}
55-
56-
GenericSignatureWitnessIterator &operator++() {
57-
checkValid();
58-
do { p = p.slice(1); } while (shouldSkip());
59-
return *this;
60-
}
61-
62-
GenericSignatureWitnessIterator operator++(int) {
63-
auto copy = *this;
64-
++(*this);
65-
return copy;
66-
}
67-
68-
Type operator*() const {
69-
checkValid();
70-
return p.front().getFirstType();
71-
}
72-
73-
Type operator->() const {
74-
checkValid();
75-
return p.front().getFirstType();
76-
}
77-
78-
bool operator==(const GenericSignatureWitnessIterator &o) {
79-
return p.data() == o.p.data() && p.size() == o.p.size();
80-
}
81-
82-
bool operator!=(const GenericSignatureWitnessIterator &o) {
83-
return p.data() != o.p.data() || p.size() != o.p.size();
84-
}
85-
86-
static GenericSignatureWitnessIterator emptyRange() {
87-
return GenericSignatureWitnessIterator();
88-
}
89-
90-
// Allow the witness iterator to be used with a ranged for.
91-
GenericSignatureWitnessIterator begin() const {
92-
return *this;
93-
}
94-
GenericSignatureWitnessIterator end() const {
95-
return GenericSignatureWitnessIterator({p.end(), p.end()});
96-
}
97-
};
98-
9934
/// Describes the generic signature of a particular declaration, including
10035
/// both the generic type parameters and the requirements placed on those
10136
/// generic parameters.
@@ -207,12 +142,22 @@ class alignas(1 << TypeAlignInBits) GenericSignature final
207142
const SubstitutionMap &subMap,
208143
SmallVectorImpl<Substitution> &result) const;
209144

210-
/// Return a range that iterates through first all of the generic parameters
211-
/// of the signature, followed by all of their recursive member types exposed
212-
/// through protocol requirements.
213-
GenericSignatureWitnessIterator getAllDependentTypes() const {
214-
return GenericSignatureWitnessIterator(getRequirements());
215-
}
145+
/// Return a range that iterates through all of the types that require
146+
/// substitution, which includes the generic parameter types as well as
147+
/// other dependent types that require additional conformances.
148+
SmallVector<Type, 4> getAllDependentTypes() const;
149+
150+
/// Enumerate all of the dependent types in the type signature that will
151+
/// occur in substitution lists (in order), along with the set of
152+
/// conformance requirements placed on that dependent type.
153+
///
154+
/// \param fn Callback function that will receive each (type, requirements)
155+
/// pair, in the order they occur within a list of substitutions. If this
156+
/// returns \c true, the enumeration will be aborted.
157+
///
158+
/// \returns true if any call to \c fn returned \c true, otherwise \c false.
159+
bool enumeratePairedRequirements(
160+
llvm::function_ref<bool(Type, ArrayRef<Requirement>)> fn) const;
216161

217162
/// Determines whether this GenericSignature is canonical.
218163
bool isCanonical() const;

include/swift/AST/Requirement.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ enum class RequirementKind : unsigned {
3535
/// A same-type requirement T == U, where T and U are types that shall be
3636
/// equivalent.
3737
SameType,
38-
/// A marker that indicates where the witness for the given (first)
39-
/// type should be located.
40-
///
41-
/// FIXME: This is a crutch used to help us eliminate various walks over
42-
/// "all archetypes".
43-
WitnessMarker
4438

4539
// Note: there is code that packs this enum in a 2-bit bitfield. Audit users
4640
// when adding enumerators.
@@ -56,10 +50,8 @@ class Requirement {
5650
/// Create a conformance or same-type requirement.
5751
Requirement(RequirementKind kind, Type first, Type second)
5852
: FirstTypeAndKind(first, kind), SecondType(second) {
59-
if (kind != RequirementKind::WitnessMarker) {
60-
assert(first);
61-
assert(second);
62-
}
53+
assert(first);
54+
assert(second);
6355
}
6456

6557
/// \brief Determine the kind of requirement.

include/swift/Serialization/ModuleFormat.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const uint16_t VERSION_MAJOR = 0;
5454
/// in source control, you should also update the comment to briefly
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
57-
const uint16_t VERSION_MINOR = 282; // Last change: @_inlineable
57+
const uint16_t VERSION_MINOR = 283; // Last change: witness markers removed
5858

5959
using DeclID = PointerEmbeddedInt<unsigned, 31>;
6060
using DeclIDField = BCFixed<31>;
@@ -236,9 +236,8 @@ static inline OperatorKind getStableFixity(DeclKind kind) {
236236
// VERSION_MAJOR.
237237
enum GenericRequirementKind : uint8_t {
238238
Conformance = 0,
239-
SameType,
240-
WitnessMarker,
241-
Superclass
239+
SameType = 1,
240+
Superclass = 2,
242241
};
243242
using GenericRequirementKindField = BCFixed<2>;
244243

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3542,8 +3542,7 @@ void GenericSignature::Profile(llvm::FoldingSetNodeID &ID,
35423542

35433543
for (auto &reqt : requirements) {
35443544
ID.AddPointer(reqt.getFirstType().getPointer());
3545-
if (reqt.getKind() != RequirementKind::WitnessMarker)
3546-
ID.AddPointer(reqt.getSecondType().getPointer());
3545+
ID.AddPointer(reqt.getSecondType().getPointer());
35473546
ID.AddInteger(unsigned(reqt.getKind()));
35483547
}
35493548
}
@@ -4056,8 +4055,7 @@ CanGenericSignature ASTContext::getSingleGenericParameterSignature() const {
40564055
return theSig;
40574056

40584057
auto param = GenericTypeParamType::get(0, 0, *this);
4059-
auto witnessReqt = Requirement(RequirementKind::WitnessMarker, param, Type());
4060-
auto sig = GenericSignature::get(param, witnessReqt);;
4058+
auto sig = GenericSignature::get(param, { });
40614059
auto canonicalSig = CanGenericSignature(sig);
40624060
Impl.SingleGenericParameterSignature = canonicalSig;
40634061
return canonicalSig;

lib/AST/ASTPrinter.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,6 @@ struct SynthesizedExtensionAnalyzer::Implementation {
233233
assert(Ext->getGenericSignature() && "No generic signature.");
234234
for (auto Req : Ext->getGenericSignature()->getRequirements()) {
235235
auto Kind = Req.getKind();
236-
if (Kind == RequirementKind::WitnessMarker)
237-
continue;
238236

239237
Type First = Req.getFirstType().subst(
240238
M, subMap, SubstOptions());
@@ -251,9 +249,6 @@ struct SynthesizedExtensionAnalyzer::Implementation {
251249
Second = Second->getCanonicalType();
252250

253251
switch (Kind) {
254-
case RequirementKind::WitnessMarker:
255-
break;
256-
257252
case RequirementKind::Conformance:
258253
case RequirementKind::Superclass:
259254
if (!canPossiblyConvertTo(First, Second, *DC))
@@ -1208,7 +1203,6 @@ static unsigned getDepthOfRequirement(const Requirement &req) {
12081203
switch (req.getKind()) {
12091204
case RequirementKind::Conformance:
12101205
case RequirementKind::Superclass:
1211-
case RequirementKind::WitnessMarker:
12121206
return getDepthOfType(req.getFirstType());
12131207

12141208
case RequirementKind::SameType: {
@@ -1337,9 +1331,6 @@ void PrintAST::printSingleDepthOfGenericSignature(
13371331
// Print the requirements.
13381332
bool isFirstReq = true;
13391333
for (const auto &req : requirements) {
1340-
if (req.getKind() == RequirementKind::WitnessMarker)
1341-
continue;
1342-
13431334
auto first = req.getFirstType();
13441335
auto second = req.getSecondType();
13451336

@@ -1387,9 +1378,6 @@ void PrintAST::printRequirement(const Requirement &req) {
13871378
case RequirementKind::SameType:
13881379
Printer << " == ";
13891380
break;
1390-
1391-
case RequirementKind::WitnessMarker:
1392-
llvm_unreachable("Handled above");
13931381
}
13941382
printType(req.getSecondType());
13951383
}
@@ -4119,9 +4107,6 @@ void GenericSignature::dump() const {
41194107

41204108
void Requirement::dump() const {
41214109
switch (getKind()) {
4122-
case RequirementKind::WitnessMarker:
4123-
llvm::errs() << "witness_marker: ";
4124-
break;
41254110
case RequirementKind::Conformance:
41264111
llvm::errs() << "conforms_to: ";
41274112
break;

lib/AST/ArchetypeBuilder.cpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,9 +1517,6 @@ void ArchetypeBuilder::addRequirement(const Requirement &req,
15171517
case RequirementKind::SameType:
15181518
addSameTypeRequirement(req.getFirstType(), req.getSecondType(), source);
15191519
return;
1520-
1521-
case RequirementKind::WitnessMarker:
1522-
return;
15231520
}
15241521

15251522
llvm_unreachable("Unhandled requirement?");
@@ -1569,9 +1566,6 @@ class ArchetypeBuilder::InferRequirementsWalker : public TypeWalker {
15691566
RequirementSource source(RequirementSource::Inferred, Loc);
15701567
for (const auto &req : genericSig->getRequirements()) {
15711568
switch (req.getKind()) {
1572-
case RequirementKind::WitnessMarker:
1573-
break;
1574-
15751569
case RequirementKind::SameType: {
15761570
auto firstType = req.getFirstType().subst(
15771571
&Builder.getModule(),
@@ -1891,10 +1885,6 @@ void ArchetypeBuilder::enumerateRequirements(llvm::function_ref<
18911885
continue;
18921886
}
18931887

1894-
// Add the witness marker.
1895-
f(RequirementKind::WitnessMarker, archetype, Type(),
1896-
RequirementSource(RequirementSource::Explicit, SourceLoc()));
1897-
18981888
// If we have a superclass, produce a superclass requirement
18991889
if (Type superclass = archetype->getSuperclass()) {
19001890
f(RequirementKind::Superclass, archetype, superclass,
@@ -1957,10 +1947,6 @@ void ArchetypeBuilder::dump(llvm::raw_ostream &out) {
19571947
source.dump(out, &Context.SourceMgr);
19581948
out << "]";
19591949
break;
1960-
1961-
case RequirementKind::WitnessMarker:
1962-
out << "\n " << archetype->getDebugName() << " witness marker";
1963-
break;
19641950
}
19651951
});
19661952
out << "\n";
@@ -2046,15 +2032,6 @@ Type ArchetypeBuilder::substDependentType(Type type) {
20462032
static void collectRequirements(ArchetypeBuilder &builder,
20472033
ArrayRef<GenericTypeParamType *> params,
20482034
SmallVectorImpl<Requirement> &requirements) {
2049-
// Don't emit WitnessMarker requirements for secondary types with
2050-
// no requirements.
2051-
auto dropRedundantWitnessMarker = [&]() {
2052-
if (!requirements.empty() &&
2053-
requirements.back().getKind() == RequirementKind::WitnessMarker &&
2054-
!requirements.back().getFirstType()->is<GenericTypeParamType>())
2055-
requirements.pop_back();
2056-
};
2057-
20582035
builder.enumerateRequirements([&](RequirementKind kind,
20592036
ArchetypeBuilder::PotentialArchetype *archetype,
20602037
llvm::PointerUnion<Type, ArchetypeBuilder::PotentialArchetype *> type,
@@ -2078,12 +2055,6 @@ static void collectRequirements(ArchetypeBuilder &builder,
20782055
if (depTy->hasError())
20792056
return;
20802057

2081-
if (kind == RequirementKind::WitnessMarker) {
2082-
dropRedundantWitnessMarker();
2083-
requirements.push_back(Requirement(kind, depTy, Type()));
2084-
return;
2085-
}
2086-
20872058
Type repTy;
20882059
if (auto concreteTy = type.dyn_cast<Type>()) {
20892060
// Maybe we were equated to a concrete type...
@@ -2099,8 +2070,6 @@ static void collectRequirements(ArchetypeBuilder &builder,
20992070

21002071
requirements.push_back(Requirement(kind, depTy, repTy));
21012072
});
2102-
2103-
dropRedundantWitnessMarker();
21042073
}
21052074

21062075
GenericSignature *ArchetypeBuilder::getGenericSignature() {

lib/AST/Builtins.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,8 @@ getBuiltinGenericFunction(Identifier Id,
196196
GenericParamTypes.push_back(gp->getDeclaredType()
197197
->castTo<GenericTypeParamType>());
198198
}
199-
// Create witness markers for all of the generic param types.
200-
SmallVector<Requirement, 2> requirements;
201-
for (auto param : GenericParamTypes) {
202-
requirements.push_back(Requirement(RequirementKind::WitnessMarker,
203-
param, Type()));
204-
}
205199
GenericSignature *Sig =
206-
GenericSignature::get(GenericParamTypes, requirements);
200+
GenericSignature::get(GenericParamTypes, { });
207201
GenericEnvironment *Env =
208202
GenericEnvironment::get(Context, GenericParamTypes,
209203
InterfaceToArchetypeMap);

0 commit comments

Comments
 (0)