Skip to content

Commit 1a07230

Browse files
authored
Merge pull request #8642 from brentdax/error-type-debugging-fix
2 parents ca5c65f + 1795471 commit 1a07230

File tree

6 files changed

+32
-15
lines changed

6 files changed

+32
-15
lines changed

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,8 +1302,10 @@ namespace {
13021302
// We may, alternatively, want to use a type variable in that case,
13031303
// and possibly infer the type of the variable that way.
13041304
CS.getTypeChecker().validateDecl(E->getDecl());
1305-
if (E->getDecl()->isInvalid())
1305+
if (E->getDecl()->isInvalid()) {
1306+
CS.setType(E, E->getDecl()->getInterfaceType());
13061307
return nullptr;
1308+
}
13071309

13081310
auto locator = CS.getConstraintLocator(E);
13091311

lib/Sema/CSSolver.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,11 +1772,12 @@ void ConstraintSystem::shrink(Expr *expr) {
17721772
///
17731773
/// \param collection The type of the collection container.
17741774
///
1775-
/// \returns ErrorType on failure, properly constructed type otherwise.
1775+
/// \returns Null type, ErrorType or UnresolvedType on failure,
1776+
/// properly constructed type otherwise.
17761777
Type extractElementType(Type collection) {
17771778
auto &ctx = CS.getASTContext();
1778-
if (collection.isNull() || collection->hasError())
1779-
return ErrorType::get(ctx);
1779+
if (!collection || collection->hasError())
1780+
return collection;
17801781

17811782
auto base = collection.getPointer();
17821783
auto isInvalidType = [](Type type) -> bool {
@@ -1788,19 +1789,19 @@ void ConstraintSystem::shrink(Expr *expr) {
17881789
if (auto array = dyn_cast<ArraySliceType>(base)) {
17891790
auto elementType = array->getBaseType();
17901791
// If base type is invalid let's return error type.
1791-
return isInvalidType(elementType) ? ErrorType::get(ctx) : elementType;
1792+
return elementType;
17921793
}
17931794

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

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

18051806
params.push_back(type);
18061807
}
@@ -1812,7 +1813,7 @@ void ConstraintSystem::shrink(Expr *expr) {
18121813
return TupleType::get(params, ctx);
18131814
}
18141815

1815-
return ErrorType::get(ctx);
1816+
return Type();
18161817
}
18171818

18181819
bool isSuitableCollection(TypeRepr *collectionTypeRepr) {
@@ -1893,7 +1894,9 @@ void ConstraintSystem::shrink(Expr *expr) {
18931894
auto elementType = extractElementType(contextualType);
18941895
// If we couldn't deduce element type for the collection, let's
18951896
// not attempt to solve it.
1896-
if (elementType->hasError())
1897+
if (!elementType ||
1898+
elementType->hasError() ||
1899+
elementType->hasUnresolvedType())
18971900
return;
18981901

18991902
contextualType = elementType;

lib/Sema/TypeCheckCaptures.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,9 @@ class FindCapturedVars : public ASTWalker {
583583

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

589590
return true;
590591
}

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ void CleanupIllFormedExpressionRAII::doIt(Expr *expr, ASTContext &Context) {
14721472
// If the type of this expression has a type variable or is invalid,
14731473
// overwrite it with ErrorType.
14741474
Type type = expr->getType();
1475-
if (!type || type->hasTypeVariable())
1475+
if (type && type->hasTypeVariable())
14761476
expr->setType(ErrorType::get(context));
14771477

14781478
return { true, expr };
@@ -2119,6 +2119,10 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
21192119
contextualPurpose,
21202120
flags,
21212121
&listener);
2122+
2123+
if (hadError && !initializer->getType()) {
2124+
initializer->setType(ErrorType::get(Context));
2125+
}
21222126

21232127
if (hadError && !pattern->hasType()) {
21242128
pattern->setType(ErrorType::get(Context));
@@ -2709,8 +2713,9 @@ bool TypeChecker::isSubstitutableFor(Type type, ArchetypeType *archetype,
27092713
}
27102714

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

27162721
// Load lvalues.

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4119,7 +4119,7 @@ void ConformanceChecker::resolveTypeWitnesses() {
41194119
if (known != typeWitnesses.end())
41204120
substitutions[archetype] = known->first;
41214121
else
4122-
substitutions[archetype] = ErrorType::get(TC.Context);
4122+
substitutions[archetype] = ErrorType::get(archetype);
41234123
}
41244124
}
41254125
}

lib/Sema/TypeCheckStmt.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
881881
/*isStmtCondition*/false)) {
882882
pattern = newPattern;
883883
// Coerce the pattern to the subject's type.
884-
if (TC.coercePatternToType(pattern, DC, subjectType,
884+
if (!subjectType || TC.coercePatternToType(pattern, DC, subjectType,
885885
TR_InExpression)) {
886886
hadError = true;
887887

@@ -1069,6 +1069,12 @@ void TypeChecker::checkIgnoredExpr(Expr *E) {
10691069
}
10701070
return;
10711071
}
1072+
1073+
// Skip checking if there is no type, which presumably means there was a
1074+
// type error.
1075+
if (!E->getType()) {
1076+
return;
1077+
}
10721078

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

0 commit comments

Comments
 (0)