Skip to content

Commit 3a72221

Browse files
authored
Merge pull request #79507 from slavapestov/assert-constraint-types
Sema: Assert that certain type kinds do not appear in constraints
2 parents 9782eae + cd77027 commit 3a72221

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

lib/IDE/CodeCompletionResultType.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,8 @@ static TypeRelation calculateTypeRelation(Type Ty, Type ExpectedTy,
395395

396396
// Equality/Conversion of GenericTypeParameterType won't account for
397397
// requirements – ignore them
398-
if (!Ty->hasTypeParameter() && !ExpectedTy->hasTypeParameter()) {
398+
if (!Ty->hasTypeParameter() && !ExpectedTy->hasTypeParameter() &&
399+
!Ty->hasUnboundGenericType() && !ExpectedTy->hasUnboundGenericType()) {
399400
if (Ty->isEqual(ExpectedTy))
400401
return TypeRelation::Convertible;
401402
bool isAny = false;

lib/Sema/Constraint.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ Constraint::Constraint(ConstraintKind kind, ArrayRef<Constraint *> constraints,
4747
getTypeVariablesBuffer().begin());
4848
}
4949

50+
static bool isAdmissibleType(Type type) {
51+
return !type->hasUnboundGenericType() && !type->hasTypeParameter();
52+
}
53+
5054
Constraint::Constraint(ConstraintKind Kind, Type First, Type Second,
5155
ConstraintLocator *locator,
5256
SmallPtrSetImpl<TypeVariableType *> &typeVars)
@@ -55,6 +59,9 @@ Constraint::Constraint(ConstraintKind Kind, Type First, Type Second,
5559
IsFavored(false), IsIsolated(false),
5660
NumTypeVariables(typeVars.size()), Types{First, Second, Type()},
5761
Locator(locator) {
62+
ASSERT(isAdmissibleType(First));
63+
ASSERT(isAdmissibleType(Second));
64+
5865
switch (Kind) {
5966
case ConstraintKind::Bind:
6067
case ConstraintKind::Equal:
@@ -84,11 +91,9 @@ Constraint::Constraint(ConstraintKind Kind, Type First, Type Second,
8491
case ConstraintKind::SameShape:
8592
case ConstraintKind::MaterializePackExpansion:
8693
case ConstraintKind::LValueObject:
87-
assert(!First.isNull());
88-
assert(!Second.isNull());
8994
break;
9095
case ConstraintKind::DynamicCallableApplicableFunction:
91-
assert(First->is<FunctionType>()
96+
ASSERT(First->is<FunctionType>()
9297
&& "The left-hand side type should be a function type");
9398
break;
9499

@@ -99,8 +104,6 @@ Constraint::Constraint(ConstraintKind Kind, Type First, Type Second,
99104

100105
case ConstraintKind::Defaultable:
101106
case ConstraintKind::FallbackType:
102-
assert(!First.isNull());
103-
assert(!Second.isNull());
104107
break;
105108

106109
case ConstraintKind::BindOverload:
@@ -136,6 +139,10 @@ Constraint::Constraint(ConstraintKind Kind, Type First, Type Second, Type Third,
136139
IsFavored(false), IsIsolated(false),
137140
NumTypeVariables(typeVars.size()), Types{First, Second, Third},
138141
Locator(locator) {
142+
ASSERT(isAdmissibleType(First));
143+
ASSERT(isAdmissibleType(Second));
144+
ASSERT(isAdmissibleType(Third));
145+
139146
switch (Kind) {
140147
case ConstraintKind::Bind:
141148
case ConstraintKind::Equal:
@@ -180,9 +187,6 @@ Constraint::Constraint(ConstraintKind Kind, Type First, Type Second, Type Third,
180187

181188
case ConstraintKind::KeyPath:
182189
case ConstraintKind::KeyPathApplication:
183-
assert(!First.isNull());
184-
assert(!Second.isNull());
185-
assert(!Third.isNull());
186190
break;
187191
}
188192

@@ -256,8 +260,8 @@ Constraint::Constraint(ConstraintKind kind,
256260
RememberChoice(false), IsFavored(false), IsIsolated(false),
257261
NumTypeVariables(typeVars.size()), Types{first, second, Type()},
258262
Locator(locator) {
259-
assert(!first.isNull());
260-
assert(!second.isNull());
263+
ASSERT(isAdmissibleType(first));
264+
ASSERT(isAdmissibleType(second));
261265
std::copy(typeVars.begin(), typeVars.end(), getTypeVariablesBuffer().begin());
262266
}
263267

@@ -269,8 +273,8 @@ Constraint::Constraint(ConstraintKind kind, ConstraintFix *fix, Type first,
269273
IsFavored(false), IsIsolated(false),
270274
NumTypeVariables(typeVars.size()), Types{first, second, Type()},
271275
Locator(locator) {
272-
assert(!first.isNull());
273-
assert(!second.isNull());
276+
ASSERT(isAdmissibleType(first));
277+
ASSERT(isAdmissibleType(second));
274278
std::copy(typeVars.begin(), typeVars.end(), getTypeVariablesBuffer().begin());
275279
if (fix)
276280
*getTrailingObjects<ConstraintFix *>() = fix;
@@ -297,8 +301,8 @@ Constraint::Constraint(FunctionType *appliedFn, Type calleeType,
297301
HasRestriction(false), IsActive(false), IsDisabled(false),
298302
IsDisabledForPerformance(false), RememberChoice(false), IsFavored(false),
299303
IsIsolated(false), NumTypeVariables(typeVars.size()), Locator(locator) {
300-
assert(appliedFn);
301-
assert(calleeType);
304+
ASSERT(isAdmissibleType(appliedFn));
305+
ASSERT(isAdmissibleType(calleeType));
302306
assert(trailingClosureMatching >= 0 && trailingClosureMatching <= 2);
303307
assert(useDC);
304308

lib/Sema/TypeOfReference.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ Type ConstraintSystem::openUnboundGenericType(GenericTypeDecl *decl,
7272
if (found == subs.end())
7373
continue;
7474

75+
// When a nominal type is declared in generic local context (which is
76+
// not actually valid anyway), the context substitution map will map
77+
// the outer generic parameters to themselves. Skip such entries to
78+
// avoid introducing constraints that contain type parameters into
79+
// the solver.
80+
if (found->second->hasTypeParameter())
81+
continue;
82+
7583
addConstraint(ConstraintKind::Bind, pair.second, found->second,
7684
locator);
7785
}

0 commit comments

Comments
 (0)