Skip to content

Commit bca2d6f

Browse files
committed
[CS] Strengthen a couple of CSApply assertions
Enforce that we don't ever encounter solutions that contain holes or error types.
1 parent 6024327 commit bca2d6f

File tree

1 file changed

+24
-32
lines changed

1 file changed

+24
-32
lines changed

lib/Sema/CSApply.cpp

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3268,8 +3268,6 @@ namespace {
32683268
auto type = simplifyType(openedType);
32693269
cs.setType(expr, type);
32703270

3271-
assert(!type->is<UnresolvedType>());
3272-
32733271
Type conformingType = type;
32743272
if (auto baseType = conformingType->getOptionalObjectType()) {
32753273
// The type may be optional due to a failable initializer in the
@@ -3461,10 +3459,6 @@ namespace {
34613459
// an ambiguity tolerant mode used for diagnostic generation. Just leave
34623460
// this as an unresolved member reference.
34633461
Type resultTy = simplifyType(cs.getType(expr));
3464-
if (resultTy->hasUnresolvedType()) {
3465-
cs.setType(expr, resultTy);
3466-
return expr;
3467-
}
34683462

34693463
// Find the selected member and base type.
34703464
auto memberLocator = cs.getConstraintLocator(
@@ -4935,7 +4929,6 @@ namespace {
49354929
Expr *visitEditorPlaceholderExpr(EditorPlaceholderExpr *E) {
49364930
simplifyExprType(E);
49374931
auto valueType = cs.getType(E);
4938-
assert(!valueType->hasUnresolvedType());
49394932

49404933
// Synthesize a call to _undefined() of appropriate type.
49414934
FuncDecl *undefinedDecl = ctx.getUndefined();
@@ -5264,13 +5257,6 @@ namespace {
52645257
auto componentTy = baseTy;
52655258
for (unsigned i : indices(E->getComponents())) {
52665259
auto &origComponent = E->getMutableComponents()[i];
5267-
5268-
// If there were unresolved types, we may end up with a null base for
5269-
// following components.
5270-
if (!componentTy) {
5271-
resolvedComponents.push_back(origComponent);
5272-
continue;
5273-
}
52745260

52755261
auto kind = origComponent.getKind();
52765262
auto componentLocator =
@@ -5325,7 +5311,6 @@ namespace {
53255311
case KeyPathExpr::Component::Kind::OptionalChain: {
53265312
didOptionalChain = true;
53275313
// Chaining always forces the element to be an rvalue.
5328-
assert(!componentTy->hasUnresolvedType());
53295314
auto objectTy =
53305315
componentTy->getWithoutSpecifierType()->getOptionalObjectType();
53315316
assert(objectTy);
@@ -5377,8 +5362,7 @@ namespace {
53775362
}
53785363

53795364
// Wrap a non-optional result if there was chaining involved.
5380-
assert(!componentTy->hasUnresolvedType());
5381-
if (didOptionalChain && componentTy &&
5365+
if (didOptionalChain &&
53825366
!componentTy->getWithoutSpecifierType()->isEqual(leafTy)) {
53835367
auto component = KeyPathExpr::Component::forOptionalWrap(leafTy);
53845368
resolvedComponents.push_back(component);
@@ -5504,7 +5488,6 @@ namespace {
55045488

55055489
// Unwrap the last component type, preserving @lvalue-ness.
55065490
auto optionalTy = components.back().getComponentType();
5507-
assert(!optionalTy->hasUnresolvedType());
55085491
Type objectTy;
55095492
if (auto lvalue = optionalTy->getAs<LValueType>()) {
55105493
objectTy = lvalue->getObjectType()->getOptionalObjectType();
@@ -7218,6 +7201,9 @@ Expr *ConstraintSystem::addImplicitLoadExpr(Expr *expr) {
72187201

72197202
Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
72207203
ConstraintLocatorBuilder locator) {
7204+
ASSERT(toType && !toType->hasError() && !toType->hasUnresolvedType() &&
7205+
!toType->hasTypeVariableOrPlaceholder());
7206+
72217207
// Diagnose conversions to invalid function types that couldn't be performed
72227208
// beforehand because of placeholders.
72237209
if (auto *fnTy = toType->getAs<FunctionType>()) {
@@ -7245,8 +7231,6 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
72457231
if (knownRestriction != solution.ConstraintRestrictions.end()) {
72467232
switch (knownRestriction->second) {
72477233
case ConversionRestrictionKind::DeepEquality: {
7248-
assert(!toType->hasUnresolvedType());
7249-
72507234
// HACK: Fix problem related to Swift 4 mode (with assertions),
72517235
// since Swift 4 mode allows passing arguments with extra parens
72527236
// to parameters which don't expect them, it should be supported
@@ -8119,9 +8103,6 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
81198103
}
81208104
}
81218105

8122-
assert(!fromType->hasUnresolvedType());
8123-
assert(!toType->hasUnresolvedType());
8124-
81258106
ABORT([&](auto &out) {
81268107
out << "Unhandled coercion:\n";
81278108
fromType->dump(out);
@@ -8224,13 +8205,6 @@ Expr *ExprRewriter::convertLiteralInPlace(
82248205
Identifier literalType, DeclName literalFuncName,
82258206
ProtocolDecl *builtinProtocol, DeclName builtinLiteralFuncName,
82268207
Diag<> brokenProtocolDiag, Diag<> brokenBuiltinProtocolDiag) {
8227-
// If coercing a literal to an unresolved type, we don't try to look up the
8228-
// witness members, just do it.
8229-
if (type->is<UnresolvedType>() || type->is<ErrorType>()) {
8230-
cs.setType(literal, type);
8231-
return literal;
8232-
}
8233-
82348208
// Check whether this literal type conforms to the builtin protocol. If so,
82358209
// initialize via the builtin protocol.
82368210
if (builtinProtocol) {
@@ -8669,8 +8643,6 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
86698643

86708644
// FIXME: Handle unwrapping everywhere else.
86718645

8672-
assert(!cs.getType(fn)->is<UnresolvedType>());
8673-
86748646
// We have a type constructor.
86758647
auto metaTy = cs.getType(fn)->castTo<AnyMetatypeType>();
86768648
auto ty = metaTy->getInstanceType();
@@ -10036,6 +10008,26 @@ ConstraintSystem::applySolution(Solution &solution,
1003610008
}
1003710009
}
1003810010

10011+
// If the score indicates the solution is valid, ensure we don't have any
10012+
// unresolved types.
10013+
{
10014+
auto isValidType = [&](Type ty) {
10015+
return !ty->hasUnresolvedType() && !ty->hasError() &&
10016+
!ty->hasTypeVariableOrPlaceholder();
10017+
};
10018+
for (auto &[_, type] : solution.typeBindings) {
10019+
ASSERT(isValidType(type) && "type binding has invalid type");
10020+
}
10021+
for (auto &[_, type] : solution.nodeTypes) {
10022+
ASSERT(isValidType(solution.simplifyType(type)) &&
10023+
"node type has invalid type");
10024+
}
10025+
for (auto &[_, type] : solution.keyPathComponentTypes) {
10026+
ASSERT(isValidType(solution.simplifyType(type)) &&
10027+
"key path type has invalid type");
10028+
}
10029+
}
10030+
1003910031
ExprRewriter rewriter(*this, solution, target, shouldSuppressDiagnostics());
1004010032
ExprWalker walker(rewriter);
1004110033
auto resultTarget = walker.rewriteTarget(target);

0 commit comments

Comments
 (0)