Skip to content

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

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 7 commits into from
May 18, 2017
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
4 changes: 3 additions & 1 deletion lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1352,8 +1352,10 @@ namespace {
// We may, alternatively, want to use a type variable in that case,
// and possibly infer the type of the variable that way.
CS.getTypeChecker().validateDecl(E->getDecl());
if (E->getDecl()->isInvalid())
if (E->getDecl()->isInvalid()) {
CS.setType(E, E->getDecl()->getInterfaceType());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fixme notes spawning and solving a type variable here would potentially provide better diagnostics instead of no diagnostics.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the code correctly, this will only happen if the variable's declaration is erroneous, and the idea is that we might be able to guess the type here so we can add it to the diagnosis there. But that seems really difficult to accomplish—by this point, the error at the declaration site should already have been diagnosed. Even if we did decide to do it, I think that's probably better done as a separate patch; I'm just trying to maintain the status quo in this PR.

return nullptr;
}

auto locator = CS.getConstraintLocator(E);

Expand Down
19 changes: 11 additions & 8 deletions lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1763,11 +1763,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 @@ -1779,19 +1780,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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be asserted as an invariant @slavapestov?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would it be an invariant? An array slice type can end up with an error type inside of it

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 @@ -1803,7 +1804,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 @@ -1884,7 +1885,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
5 changes: 3 additions & 2 deletions lib/Sema/TypeCheckCaptures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,9 @@ class FindCapturedVars : public ASTWalker {

// Assigning an object doesn't require type metadata.
if (auto assignment = dyn_cast<AssignExpr>(E))
return !assignment->getSrc()->getType()
->hasRetainablePointerRepresentation();
return assignment->getSrc()->getType() &&
!assignment->getSrc()->getType()
->hasRetainablePointerRepresentation();

return true;
}
Expand Down
9 changes: 7 additions & 2 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1452,7 +1452,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 Expand Up @@ -2099,6 +2099,10 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
contextualPurpose,
flags,
&listener);

if (hadError && !initializer->getType()) {
initializer->setType(ErrorType::get(Context));
}

if (hadError && !pattern->hasType()) {
pattern->setType(ErrorType::get(Context));
Expand Down Expand Up @@ -2689,8 +2693,9 @@ bool TypeChecker::isSubstitutableFor(Type type, ArchetypeType *archetype,
}

Expr *TypeChecker::coerceToMaterializable(Expr *expr) {
// If expr has no type, just assume it's the right expr.
// If the type is already materializable, then we're already done.
if (expr->getType()->isMaterializable())
if (!expr->getType() || expr->getType()->isMaterializable())
return expr;

// Load lvalues.
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4100,7 +4100,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
8 changes: 7 additions & 1 deletion lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
/*isStmtCondition*/false)) {
pattern = newPattern;
// Coerce the pattern to the subject's type.
if (TC.coercePatternToType(pattern, DC, subjectType,
if (!subjectType || TC.coercePatternToType(pattern, DC, subjectType,
TR_InExpression)) {
hadError = true;

Expand Down Expand Up @@ -1069,6 +1069,12 @@ void TypeChecker::checkIgnoredExpr(Expr *E) {
}
return;
}

// Skip checking if there is no type, which presumably means there was a
// type error.
if (!E->getType()) {
return;
}

// Complain about l-values that are neither loaded nor stored.
if (E->getType()->isLValueType()) {
Expand Down