Skip to content

Commit 1b75bd0

Browse files
committed
[CSBindings] Perform more checking before accepting default types
Some literal protocols default to unbound types, we need to make sure that it's okay to include them to avoid duplicate solutions.
1 parent ea5eb89 commit 1b75bd0

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ void ConstraintSystem::PotentialBindings::inferTransitiveBindings(
9292
}
9393
}
9494

95+
static bool
96+
isUnviableDefaultType(Type defaultType,
97+
llvm::SmallPtrSetImpl<CanType> &existingTypes) {
98+
auto canType = defaultType->getCanonicalType();
99+
100+
if (!defaultType->hasUnboundGenericType())
101+
return !existingTypes.insert(canType).second;
102+
103+
// For generic literal types, check whether we already have a
104+
// specialization of this generic within our list.
105+
// FIXME: This assumes that, e.g., the default literal
106+
// int/float/char/string types are never generic.
107+
auto nominal = defaultType->getAnyNominal();
108+
if (!nominal)
109+
return true;
110+
111+
if (llvm::any_of(existingTypes, [&nominal](CanType existingType) {
112+
// FIXME: Check parents?
113+
return nominal == existingType->getAnyNominal();
114+
}))
115+
return true;
116+
117+
existingTypes.insert(canType);
118+
return false;
119+
}
120+
95121
void ConstraintSystem::PotentialBindings::inferDefaultTypes(
96122
ConstraintSystem &cs, llvm::SmallPtrSetImpl<CanType> &existingTypes) {
97123
// If we have any literal constraints, check whether there is already a
@@ -165,7 +191,7 @@ void ConstraintSystem::PotentialBindings::inferDefaultTypes(
165191
if (!defaultType)
166192
continue;
167193

168-
if (!existingTypes.insert(defaultType->getCanonicalType()).second)
194+
if (isUnviableDefaultType(defaultType, existingTypes))
169195
continue;
170196

171197
// We need to figure out whether this is a direct conformance

0 commit comments

Comments
 (0)