Skip to content

Sema: Assert that certain type kinds do not appear in constraints #79507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/IDE/CodeCompletionResultType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@ static TypeRelation calculateTypeRelation(Type Ty, Type ExpectedTy,

// Equality/Conversion of GenericTypeParameterType won't account for
// requirements – ignore them
if (!Ty->hasTypeParameter() && !ExpectedTy->hasTypeParameter()) {
if (!Ty->hasTypeParameter() && !ExpectedTy->hasTypeParameter() &&
!Ty->hasUnboundGenericType() && !ExpectedTy->hasUnboundGenericType()) {
if (Ty->isEqual(ExpectedTy))
return TypeRelation::Convertible;
bool isAny = false;
Expand Down
32 changes: 18 additions & 14 deletions lib/Sema/Constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Constraint::Constraint(ConstraintKind kind, ArrayRef<Constraint *> constraints,
getTypeVariablesBuffer().begin());
}

static bool isAdmissibleType(Type type) {
return !type->hasUnboundGenericType() && !type->hasTypeParameter();
}

Constraint::Constraint(ConstraintKind Kind, Type First, Type Second,
ConstraintLocator *locator,
SmallPtrSetImpl<TypeVariableType *> &typeVars)
Expand All @@ -55,6 +59,9 @@ Constraint::Constraint(ConstraintKind Kind, Type First, Type Second,
IsFavored(false), IsIsolated(false),
NumTypeVariables(typeVars.size()), Types{First, Second, Type()},
Locator(locator) {
ASSERT(isAdmissibleType(First));
ASSERT(isAdmissibleType(Second));

switch (Kind) {
case ConstraintKind::Bind:
case ConstraintKind::Equal:
Expand Down Expand Up @@ -84,11 +91,9 @@ Constraint::Constraint(ConstraintKind Kind, Type First, Type Second,
case ConstraintKind::SameShape:
case ConstraintKind::MaterializePackExpansion:
case ConstraintKind::LValueObject:
assert(!First.isNull());
assert(!Second.isNull());
break;
case ConstraintKind::DynamicCallableApplicableFunction:
assert(First->is<FunctionType>()
ASSERT(First->is<FunctionType>()
&& "The left-hand side type should be a function type");
break;

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

case ConstraintKind::Defaultable:
case ConstraintKind::FallbackType:
assert(!First.isNull());
assert(!Second.isNull());
break;

case ConstraintKind::BindOverload:
Expand Down Expand Up @@ -136,6 +139,10 @@ Constraint::Constraint(ConstraintKind Kind, Type First, Type Second, Type Third,
IsFavored(false), IsIsolated(false),
NumTypeVariables(typeVars.size()), Types{First, Second, Third},
Locator(locator) {
ASSERT(isAdmissibleType(First));
ASSERT(isAdmissibleType(Second));
ASSERT(isAdmissibleType(Third));

switch (Kind) {
case ConstraintKind::Bind:
case ConstraintKind::Equal:
Expand Down Expand Up @@ -180,9 +187,6 @@ Constraint::Constraint(ConstraintKind Kind, Type First, Type Second, Type Third,

case ConstraintKind::KeyPath:
case ConstraintKind::KeyPathApplication:
assert(!First.isNull());
assert(!Second.isNull());
assert(!Third.isNull());
break;
}

Expand Down Expand Up @@ -256,8 +260,8 @@ Constraint::Constraint(ConstraintKind kind,
RememberChoice(false), IsFavored(false), IsIsolated(false),
NumTypeVariables(typeVars.size()), Types{first, second, Type()},
Locator(locator) {
assert(!first.isNull());
assert(!second.isNull());
ASSERT(isAdmissibleType(first));
ASSERT(isAdmissibleType(second));
std::copy(typeVars.begin(), typeVars.end(), getTypeVariablesBuffer().begin());
}

Expand All @@ -269,8 +273,8 @@ Constraint::Constraint(ConstraintKind kind, ConstraintFix *fix, Type first,
IsFavored(false), IsIsolated(false),
NumTypeVariables(typeVars.size()), Types{first, second, Type()},
Locator(locator) {
assert(!first.isNull());
assert(!second.isNull());
ASSERT(isAdmissibleType(first));
ASSERT(isAdmissibleType(second));
std::copy(typeVars.begin(), typeVars.end(), getTypeVariablesBuffer().begin());
if (fix)
*getTrailingObjects<ConstraintFix *>() = fix;
Expand All @@ -297,8 +301,8 @@ Constraint::Constraint(FunctionType *appliedFn, Type calleeType,
HasRestriction(false), IsActive(false), IsDisabled(false),
IsDisabledForPerformance(false), RememberChoice(false), IsFavored(false),
IsIsolated(false), NumTypeVariables(typeVars.size()), Locator(locator) {
assert(appliedFn);
assert(calleeType);
ASSERT(isAdmissibleType(appliedFn));
ASSERT(isAdmissibleType(calleeType));
assert(trailingClosureMatching >= 0 && trailingClosureMatching <= 2);
assert(useDC);

Expand Down
8 changes: 8 additions & 0 deletions lib/Sema/TypeOfReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ Type ConstraintSystem::openUnboundGenericType(GenericTypeDecl *decl,
if (found == subs.end())
continue;

// When a nominal type is declared in generic local context (which is
// not actually valid anyway), the context substitution map will map
// the outer generic parameters to themselves. Skip such entries to
// avoid introducing constraints that contain type parameters into
// the solver.
if (found->second->hasTypeParameter())
continue;

addConstraint(ConstraintKind::Bind, pair.second, found->second,
locator);
}
Expand Down