Skip to content

Commit 7358bf9

Browse files
authored
Merge pull request #5966 from DougGregor/generic-environment-cleanups
2 parents 262cf50 + 554619f commit 7358bf9

File tree

8 files changed

+43
-11
lines changed

8 files changed

+43
-11
lines changed

include/swift/AST/Types.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3926,13 +3926,26 @@ class ArchetypeType final : public SubstitutableType,
39263926
/// \brief Check if the archetype contains a nested type with the given name.
39273927
bool hasNestedType(Identifier Name) const;
39283928

3929+
/// \brief Retrieve the known nested types of this archetype.
3930+
///
3931+
/// Useful only for debugging dumps; all other queries should attempt to
3932+
/// find a particular nested type by name, directly, or look at the
3933+
/// protocols to which this archetype conforms.
3934+
ArrayRef<std::pair<Identifier, NestedType>>
3935+
getKnownNestedTypes(bool resolveTypes = true) const {
3936+
return getAllNestedTypes(/*resolveTypes=*/false);
3937+
}
3938+
39293939
/// \brief Retrieve the nested types of this archetype.
39303940
///
39313941
/// \param resolveTypes Whether to eagerly resolve the nested types
39323942
/// (defaults to \c true). Otherwise, the nested types might be
39333943
/// null.
3944+
///
3945+
/// FIXME: This operation should go away, because it breaks recursive
3946+
/// protocol constraints.
39343947
ArrayRef<std::pair<Identifier, NestedType>>
3935-
getNestedTypes(bool resolveTypes = true) const;
3948+
getAllNestedTypes(bool resolveTypes = true) const;
39363949

39373950
/// \brief Set the nested types to a copy of the given array of
39383951
/// archetypes, which will first be sorted in place.

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2824,7 +2824,7 @@ namespace {
28242824
printRec("opened_existential", openedExistential);
28252825

28262826
Indent += 2;
2827-
for (auto nestedType : T->getNestedTypes(/*resolveTypes=*/false)) {
2827+
for (auto nestedType : T->getKnownNestedTypes()) {
28282828
OS << "\n";
28292829
OS.indent(Indent) << "(";
28302830
PrintWithColorRAII(OS, TypeFieldColor) << "nested_type";

lib/AST/ASTVerifier.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,10 @@ struct ASTNodeBase {};
462462
}
463463

464464
// Make sure that none of the nested types are dependent.
465-
for (const auto &nested : archetype->getNestedTypes()) {
465+
for (const auto &nested : archetype->getKnownNestedTypes()) {
466+
if (!nested.second)
467+
continue;
468+
466469
if (auto nestedType = nested.second.getAsConcreteType()) {
467470
if (nestedType->hasTypeParameter()) {
468471
Out << "Nested type " << nested.first.str()

lib/AST/ArchetypeBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ ArchetypeBuilder::PotentialArchetype::getType(ArchetypeBuilder &builder) {
757757
arch->setNestedTypes(builder.getASTContext(), FlatNestedTypes);
758758

759759
// Force the resolution of the nested types.
760-
(void)arch->getNestedTypes();
760+
(void)arch->getAllNestedTypes();
761761

762762
builder.getASTContext().unregisterLazyArchetype(arch);
763763
}

lib/AST/GenericEnvironment.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,28 @@ GenericEnvironment::GenericEnvironment(
4747

4848
// If we mapped the generic parameter to an archetype, add it to the
4949
// reverse mapping.
50-
if (auto *archetypeTy = entry.second->getAs<ArchetypeType>())
51-
ArchetypeToInterfaceMap[archetypeTy] = entry.first;
50+
if (auto *archetypeTy = entry.second->getAs<ArchetypeType>()) {
51+
// If we've already recorded an interface type for this archetype, then
52+
// multiple generic parameters map to the same archetype. If the
53+
// existing entry comes from a later generic parameter, replace it with
54+
// the earlier generic parameter. This gives us a deterministic reverse
55+
// mapping.
56+
auto knownInterfaceType = ArchetypeToInterfaceMap.find(archetypeTy);
57+
if (knownInterfaceType != ArchetypeToInterfaceMap.end()) {
58+
auto otherGP
59+
= knownInterfaceType->second->castTo<GenericTypeParamType>();
60+
if (std::make_pair(canParamTy->getDepth(), canParamTy->getIndex())
61+
< std::make_pair(otherGP->getDepth(), otherGP->getIndex()))
62+
knownInterfaceType->second = entry.first;
63+
continue;
64+
}
5265

53-
// FIXME: If multiple generic parameters map to the same archetype,
54-
// the reverse mapping order is not deterministic.
66+
ArchetypeToInterfaceMap[archetypeTy] = entry.first;
67+
}
5568
}
69+
70+
// Make sure this generic environment gets destroyed.
71+
signature->getASTContext().addDestructorCleanup(*this);
5672
}
5773

5874
void *GenericEnvironment::operator new(size_t bytes, const ASTContext &ctx) {

lib/AST/Type.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2587,7 +2587,7 @@ bool ArchetypeType::hasNestedType(Identifier Name) const {
25872587
}
25882588

25892589
ArrayRef<std::pair<Identifier, ArchetypeType::NestedType>>
2590-
ArchetypeType::getNestedTypes(bool resolveTypes) const {
2590+
ArchetypeType::getAllNestedTypes(bool resolveTypes) const {
25912591
if (resolveTypes) {
25922592
for (auto &nested : NestedTypes) {
25932593
if (!nested.second)

lib/IRGen/GenType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ static void profileArchetypeConstraints(
10771077
}
10781078

10791079
// Recursively profile nested archetypes.
1080-
for (auto nested : arch->getNestedTypes()) {
1080+
for (auto nested : arch->getAllNestedTypes()) {
10811081
profileArchetypeConstraints(nested.second.getValue(), ID, seen);
10821082
}
10831083
}

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2978,7 +2978,7 @@ void Serializer::writeType(Type ty) {
29782978
SmallVector<IdentifierID, 4> nestedTypeNames;
29792979
SmallVector<TypeID, 4> nestedTypes;
29802980
SmallVector<bool, 4> areArchetypes;
2981-
for (auto next : archetypeTy->getNestedTypes()) {
2981+
for (auto next : archetypeTy->getAllNestedTypes()) {
29822982
nestedTypeNames.push_back(addIdentifierRef(next.first));
29832983
nestedTypes.push_back(addTypeRef(next.second.getValue()));
29842984
areArchetypes.push_back(!next.second.isConcreteType());

0 commit comments

Comments
 (0)