Skip to content

Sema: Don't construct naked ErrorTypes in valid code #8341

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

Closed
Closed
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
19 changes: 11 additions & 8 deletions lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1727,11 +1727,12 @@ void ConstraintSystem::shrink(Expr *expr) {
///
/// \param collection The type of the collection container.
///
/// \returns ErrorType on failure, properly constructed type otherwise.
/// \returns Null type, ErrorType or UnresolvedType on failure,
/// properly constructed type otherwise.
Type extractElementType(Type collection) {
auto &ctx = CS.getASTContext();
if (collection.isNull() || collection->hasError())
return ErrorType::get(ctx);
if (!collection || collection->hasError())
return collection;

auto base = collection.getPointer();
auto isInvalidType = [](Type type) -> bool {
Expand All @@ -1743,19 +1744,19 @@ void ConstraintSystem::shrink(Expr *expr) {
if (auto array = dyn_cast<ArraySliceType>(base)) {
auto elementType = array->getBaseType();
// If base type is invalid let's return error type.
return isInvalidType(elementType) ? ErrorType::get(ctx) : elementType;
return elementType;
}

// Map or Set or any other associated collection type.
if (auto boundGeneric = dyn_cast<BoundGenericType>(base)) {
if (boundGeneric->hasUnresolvedType())
return ErrorType::get(ctx);
return boundGeneric;

llvm::SmallVector<TupleTypeElt, 2> params;
for (auto &type : boundGeneric->getGenericArgs()) {
// One of the generic arguments in invalid or unresolved.
if (isInvalidType(type))
return ErrorType::get(ctx);
return type;

params.push_back(type);
}
Expand All @@ -1767,7 +1768,7 @@ void ConstraintSystem::shrink(Expr *expr) {
return TupleType::get(params, ctx);
}

return ErrorType::get(ctx);
return Type();
}

bool isSuitableCollection(TypeRepr *collectionTypeRepr) {
Expand Down Expand Up @@ -1848,7 +1849,9 @@ void ConstraintSystem::shrink(Expr *expr) {
auto elementType = extractElementType(contextualType);
// If we couldn't deduce element type for the collection, let's
// not attempt to solve it.
if (elementType->hasError())
if (!elementType ||
elementType->hasError() ||
elementType->hasUnresolvedType())
return;

contextualType = elementType;
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,7 @@ void CleanupIllFormedExpressionRAII::doIt(Expr *expr, ASTContext &Context) {
// If the type of this expression has a type variable or is invalid,
// overwrite it with ErrorType.
Type type = expr->getType();
if (!type || type->hasTypeVariable())
if (type && type->hasTypeVariable())
expr->setType(ErrorType::get(context));

return { true, expr };
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4082,7 +4082,7 @@ void ConformanceChecker::resolveTypeWitnesses() {
if (known != typeWitnesses.end())
substitutions[archetype] = known->first;
else
substitutions[archetype] = ErrorType::get(TC.Context);
substitutions[archetype] = ErrorType::get(archetype);
}
}
}
Expand Down