Skip to content

Commit 5171b84

Browse files
committed
[CS] Replace UnresolvedType with ErrorType in simplifyType/resolveType
This means we now either produce a bare ErrorType, or an ErrorType with a generic parameter original type for a generic parameter hole. We ought to further consolidate this logic by sinking the generic parameter original type replacement into `simplifyType` itself, but I'm leaving that for a future patch since it affects completion results and I want to try keep this close to NFC.
1 parent 21141f4 commit 5171b84

11 files changed

+36
-34
lines changed

lib/AST/DiagnosticEngine.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -983,11 +983,10 @@ static void formatDiagnosticArgument(StringRef Modifier,
983983
needsQualification = typeSpellingIsAmbiguous(type, Args, printOptions);
984984
}
985985

986-
// If a type has an unresolved type, print it with syntax sugar removed for
986+
// If a type has a bare error type, print it with syntax sugar removed for
987987
// clarity. For example, print `Array<_>` instead of `[_]`.
988-
if (type->hasUnresolvedType()) {
988+
if (type->hasBareError())
989989
type = type->getWithoutSyntaxSugar();
990-
}
991990

992991
if (needsQualification &&
993992
isa<OpaqueTypeArchetypeType>(type.getPointer()) &&

lib/IDE/ArgumentCompletion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
201201
}
202202
}
203203
if (ExpectedCallType &&
204-
(ExpectedCallType->hasUnresolvedType() ||
204+
(ExpectedCallType->hasError() ||
205205
ExpectedCallType->hasUnboundGenericType())) {
206206
ExpectedCallType = Type();
207207
}

lib/IDE/CompletionLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ Type CompletionLookup::getTypeOfMember(const ValueDecl *VD, Type ExprType) {
693693

694694
// For a GenericFunctionType, we only want to substitute the
695695
// param/result types, as otherwise we might end up with a bad generic
696-
// signature if there are UnresolvedTypes present in the base type. Note
696+
// signature if there are ErrorTypes present in the base type. Note
697697
// we pass in DesugarMemberTypes so that we see the actual concrete type
698698
// witnesses instead of type alias types.
699699
if (auto *GFT = T->getAs<GenericFunctionType>()) {

lib/IDE/ConformingMethodList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void ConformingMethodListCallbacks::readyForTypeChecking(SourceFile *SrcFile) {
123123
Type T = Res.Ty;
124124
WithSolutionSpecificVarTypesRAII VarType(Res.SolutionSpecificVarTypes);
125125

126-
if (!T || T->is<ErrorType>() || T->is<UnresolvedType>())
126+
if (!T || T->is<ErrorType>())
127127
return;
128128

129129
T = T->getRValueType();

lib/IDE/PostfixCompletion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ void PostfixCompletionCallback::collectResults(
420420
// tuple. But that doesn’t really make sense so we shouldn't be suggesting
421421
// any operators based on `Void`.
422422
if (IncludeOperators && !Result.BaseIsStaticMetaType &&
423-
!Result.BaseTy->isVoid() &&
423+
!Result.BaseTy->isVoid() && !Result.BaseTy->hasError() &&
424424
!ProcessedBaseTypes.contains(Result.BaseTy)) {
425425
addOperatorResults(Result.BaseTy, Operators, DC, Lookup);
426426
}

lib/IDE/SelectedOverloadInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ swift::ide::getSelectedOverloadInfo(const Solution &S,
5656
OverloadChoiceKind::KeyPathApplication) {
5757
auto Params = Result.ValueTy->getAs<AnyFunctionType>()->getParams();
5858
if (Params.size() == 1 &&
59-
Params[0].getPlainType()->is<UnresolvedType>()) {
59+
Params[0].getPlainType()->is<ErrorType>()) {
6060
auto *KPDecl = CS.getASTContext().getKeyPathDecl();
6161
Type KPTy =
6262
KPDecl->mapTypeIntoContext(KPDecl->getDeclaredInterfaceType());

lib/IDE/TypeCheckCompletionCallback.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Type swift::ide::getTypeForCompletion(const constraints::Solution &S,
2727
// Use the contextual type, unless it is still unresolved, in which case fall
2828
// back to getting the type from the expression.
2929
if (auto ContextualType = S.getContextualType(Node)) {
30-
if (!ContextualType->hasUnresolvedType() &&
30+
if (!ContextualType->hasError() &&
3131
!ContextualType->hasUnboundGenericType()) {
3232
return ContextualType;
3333
}
@@ -46,7 +46,7 @@ Type swift::ide::getTypeForCompletion(const constraints::Solution &S,
4646
Result = S.getResolvedType(Node);
4747
}
4848

49-
if (Result && Result->is<UnresolvedType>()) {
49+
if (Result && Result->is<ErrorType>()) {
5050
Result = Type();
5151
}
5252
return Result;

lib/IDE/TypeContextInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void ContextInfoCallbacks::readyForTypeChecking(SourceFile *SrcFile) {
129129
SmallVector<TypeContextInfoItem, 2> results;
130130

131131
for (auto T : TypeCheckCallback.getTypes()) {
132-
if (T->is<ErrorType>() || T->is<UnresolvedType>())
132+
if (T->is<ErrorType>())
133133
continue;
134134

135135
T = T->getRValueType();

lib/Sema/CSApply.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,16 @@ Solution::computeSubstitutions(NullablePtr<ValueDecl> decl,
105105
if (openedTypes == OpenedTypes.end())
106106
return SubstitutionMap();
107107

108+
auto &ctx = getConstraintSystem().getASTContext();
109+
108110
SmallVector<Type, 4> replacementTypes;
109111
for (const auto &opened : openedTypes->second) {
110112
auto type = getFixedType(opened.second);
111113
if (opened.first->isParameterPack()) {
112114
if (type->is<PlaceholderType>()) {
113-
auto &ctx = type->getASTContext();
114-
type =
115-
PackType::get(ctx, {PackExpansionType::get(ctx.TheUnresolvedType,
116-
ctx.TheUnresolvedType)});
115+
type = PackType::get(
116+
ctx,
117+
{PackExpansionType::get(ErrorType::get(ctx), ErrorType::get(ctx))});
117118
} else if (!type->is<PackType>())
118119
type = PackType::getSingletonPackExpansion(type);
119120
}
@@ -126,8 +127,11 @@ Solution::computeSubstitutions(NullablePtr<ValueDecl> decl,
126127
auto replacement = original.subst(IFS);
127128
assert(!replacement->is<GenericTypeParamType>());
128129

129-
if (replacement->hasError() ||
130-
isOpenedAnyObject(replacement) ||
130+
if (replacement->hasError()) {
131+
return ProtocolConformanceRef::forAbstract(ErrorType::get(replacement),
132+
protoType);
133+
}
134+
if (isOpenedAnyObject(replacement) ||
131135
replacement->is<GenericTypeParamType>()) {
132136
return ProtocolConformanceRef::forAbstract(replacement, protoType);
133137
}
@@ -7218,7 +7222,7 @@ Expr *ConstraintSystem::addImplicitLoadExpr(Expr *expr) {
72187222

72197223
Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
72207224
ConstraintLocatorBuilder locator) {
7221-
ASSERT(toType && !toType->hasError() && !toType->hasUnresolvedType() &&
7225+
ASSERT(toType && !toType->hasError() &&
72227226
!toType->hasTypeVariableOrPlaceholder());
72237227

72247228
// Diagnose conversions to invalid function types that couldn't be performed
@@ -10032,8 +10036,7 @@ ConstraintSystem::applySolution(Solution &solution,
1003210036
// unresolved types.
1003310037
{
1003410038
auto isValidType = [&](Type ty) {
10035-
return !ty->hasUnresolvedType() && !ty->hasError() &&
10036-
!ty->hasTypeVariableOrPlaceholder();
10039+
return !ty->hasError() && !ty->hasTypeVariableOrPlaceholder();
1003710040
};
1003810041
for (auto &[_, type] : solution.typeBindings) {
1003910042
ASSERT(isValidType(type) && "type binding has invalid type");

lib/Sema/CSDiagnostics.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Type FailureDiagnostic::resolveType(Type rawType, bool reconstituteSugar,
100100
if (auto *typeVar = type->getAs<TypeVariableType>()) {
101101
auto resolvedType = S.simplifyType(typeVar);
102102

103-
if (!resolvedType->hasUnresolvedType())
103+
if (!resolvedType->hasError())
104104
return resolvedType;
105105

106106
// If type variable was simplified to an unresolved pack expansion
@@ -117,7 +117,7 @@ Type FailureDiagnostic::resolveType(Type rawType, bool reconstituteSugar,
117117
}
118118

119119
Type GP = typeVar->getImpl().getGenericParameter();
120-
return resolvedType->is<UnresolvedType>() && GP
120+
return resolvedType->is<ErrorType>() && GP
121121
? ErrorType::get(GP)
122122
: resolvedType;
123123
}
@@ -128,7 +128,7 @@ Type FailureDiagnostic::resolveType(Type rawType, bool reconstituteSugar,
128128
}
129129

130130
if (type->isPlaceholder())
131-
return Type(type->getASTContext().TheUnresolvedType);
131+
return ErrorType::get(type->getASTContext());
132132

133133
return std::nullopt;
134134
});
@@ -183,7 +183,7 @@ StringRef FailureDiagnostic::getEditorPlaceholder(
183183
llvm::SmallVectorImpl<char> &scratch) const {
184184
llvm::raw_svector_ostream OS(scratch);
185185
OS << "<#";
186-
if (!ty || ty->is<UnresolvedType>()) {
186+
if (!ty || ty->isBareErrorType()) {
187187
OS << description;
188188
} else {
189189
OS << "T##";
@@ -5681,7 +5681,7 @@ bool MissingArgumentsFailure::diagnoseMissingResultBuilderElement() const {
56815681
auto fixIt = getEditorPlaceholder("result", paramType, scratch);
56825682
auto fixItLoc = call->getStartLoc();
56835683

5684-
if (paramType->is<UnresolvedType>()) {
5684+
if (paramType->isBareErrorType()) {
56855685
emitDiagnostic(diag::result_builder_missing_element,
56865686
resultBuilder->getName())
56875687
.fixItInsertAfter(fixItLoc, fixIt);
@@ -5804,7 +5804,7 @@ bool MissingArgumentsFailure::isMisplacedMissingArgument(
58045804
auto argType = solution.simplifyType(solution.getType(unaryArg));
58055805
auto paramType = fnType->getParams()[1].getPlainType();
58065806

5807-
if (isExpr<ClosureExpr>(unaryArg) && argType->is<UnresolvedType>()) {
5807+
if (isExpr<ClosureExpr>(unaryArg) && argType->is<ErrorType>()) {
58085808
auto unwrappedParamTy = paramType->lookThroughAllOptionalTypes();
58095809
if (unwrappedParamTy->is<FunctionType>() || unwrappedParamTy->isAny())
58105810
return true;
@@ -5958,7 +5958,7 @@ bool ClosureParamDestructuringFailure::diagnoseAsError() {
59585958
};
59595959

59605960
auto isValidType = [](Type resultType) -> bool {
5961-
return resultType && !resultType->hasUnresolvedType() &&
5961+
return resultType && !resultType->hasError() &&
59625962
!resultType->hasTypeVariable();
59635963
};
59645964

@@ -6619,7 +6619,7 @@ bool CollectionElementContextualFailure::diagnoseAsError() {
66196619
// statement it has to be diagnosed as pattern match if there are
66206620
// holes present in the contextual type.
66216621
if (purpose == ContextualTypePurpose::CTP_ForEachSequence &&
6622-
contextualType->hasUnresolvedType()) {
6622+
contextualType->hasError()) {
66236623
auto diagnostic = emitDiagnostic(
66246624
(contextualType->is<TupleType>() && !eltType->is<TupleType>())
66256625
? diag::cannot_match_expr_tuple_pattern_with_nontuple_value
@@ -7490,7 +7490,7 @@ bool ArgumentMismatchFailure::diagnoseAsError() {
74907490
if (argType->isKeyPath() && !paramType->isKnownKeyPathType()) {
74917491
auto keyPathTy = argType->castTo<BoundGenericType>();
74927492
auto rootTy = keyPathTy->getGenericArgs()[0];
7493-
if (rootTy->is<UnresolvedType>()) {
7493+
if (rootTy->isBareErrorType()) {
74947494
emitDiagnostic(diag::cannot_convert_unresolved_key_path_argument_value,
74957495
paramType);
74967496
return true;
@@ -7627,7 +7627,7 @@ bool ArgumentMismatchFailure::diagnosePatternMatchingMismatch() const {
76277627
auto rhsType = getType(rhsExpr);
76287628

76297629
auto diagnostic =
7630-
lhsType->is<UnresolvedType>()
7630+
lhsType->isBareErrorType()
76317631
? emitDiagnostic(
76327632
diag::cannot_match_unresolved_expr_pattern_with_value, rhsType)
76337633
: emitDiagnostic(diag::cannot_match_expr_pattern_with_value, lhsType,
@@ -8286,7 +8286,7 @@ bool UnableToInferClosureParameterType::diagnoseAsError() {
82868286
if (parentExpr) {
82878287
// Missing or invalid member reference in call.
82888288
if (auto *AE = dyn_cast<ApplyExpr>(parentExpr)) {
8289-
if (getType(AE->getFn())->is<UnresolvedType>())
8289+
if (getType(AE->getFn())->is<ErrorType>())
82908290
return false;
82918291
}
82928292

0 commit comments

Comments
 (0)