Skip to content

Commit 554619f

Browse files
committed
[AST] Make GenericEnvironment's reverse mapping deterministic.
GenericEnvironment walked its input mapping in DenseMap order while populating a reverse mapping from archetypes to interface types. However, this mapping is not unique, because two generic parameters can end up mapping to the same archetype. In such cases, which generic parameter we mapped to was nondeterministic. Make this deterministic by preferring to map back to the earlier generic parameter.
1 parent 637e542 commit 554619f

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

lib/AST/GenericEnvironment.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,24 @@ 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
}
5669

5770
// Make sure this generic environment gets destroyed.

0 commit comments

Comments
 (0)