Skip to content

Commit 7500c6e

Browse files
committed
[Diagnostics] Switch from vector to set to avoid storing duplicate solution types
This allows callees of `getPossibleTypesOfExpressionWithoutApplying` to get only unique types from the re-run of the solver.
1 parent 65119c8 commit 7500c6e

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,7 +2811,8 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
28112811
auto exprType = recheckedExpr ? CS.getType(recheckedExpr) : Type();
28122812

28132813
// If it failed and diagnosed something, then we're done.
2814-
if (!exprType) return true;
2814+
if (!exprType)
2815+
return CS.TC.Diags.hadAnyError();
28152816

28162817
// If we contextually had an inout type, and got a non-lvalue result, then
28172818
// we fail with a mutability error.
@@ -5274,7 +5275,7 @@ bool FailureDiagnosis::diagnoseTrailingClosureErrors(ApplyExpr *callExpr) {
52745275
}
52755276
};
52765277

5277-
SmallVector<Type, 4> possibleTypes;
5278+
SmallPtrSet<TypeBase *, 4> possibleTypes;
52785279
auto currentType = CS.getType(fnExpr);
52795280

52805281
// If current type has type variables or unresolved types
@@ -5294,10 +5295,10 @@ bool FailureDiagnosis::diagnoseTrailingClosureErrors(ApplyExpr *callExpr) {
52945295
return diagnoseContextualConversionError(callExpr, contextualType,
52955296
CS.getContextualTypePurpose());
52965297
} else {
5297-
possibleTypes.push_back(currentType);
5298+
possibleTypes.insert(currentType.getPointer());
52985299
}
52995300

5300-
for (auto type : possibleTypes) {
5301+
for (Type type : possibleTypes) {
53015302
auto *fnType = type->getAs<AnyFunctionType>();
53025303
if (!fnType)
53035304
continue;
@@ -5388,7 +5389,7 @@ bool FailureDiagnosis::diagnoseCallContextualConversionErrors(
53885389
auto *DC = CS.DC;
53895390

53905391
auto typeCheckExpr = [](TypeChecker &TC, Expr *expr, DeclContext *DC,
5391-
SmallVectorImpl<Type> &types,
5392+
SmallPtrSetImpl<TypeBase *> &types,
53925393
Type contextualType = Type()) {
53935394
CalleeListener listener(contextualType);
53945395
TC.getPossibleTypesOfExpressionWithoutApplying(
@@ -5398,7 +5399,7 @@ bool FailureDiagnosis::diagnoseCallContextualConversionErrors(
53985399
// First let's type-check expression without contextual type, and
53995400
// see if that's going to produce a type, if so, let's type-check
54005401
// again, this time using given contextual type.
5401-
SmallVector<Type, 4> withoutContextual;
5402+
SmallPtrSet<TypeBase *, 4> withoutContextual;
54025403
typeCheckExpr(TC, callExpr, DC, withoutContextual);
54035404

54045405
// If there are no types returned, it means that problem was
@@ -5407,12 +5408,13 @@ bool FailureDiagnosis::diagnoseCallContextualConversionErrors(
54075408
if (withoutContextual.empty())
54085409
return false;
54095410

5410-
SmallVector<Type, 4> withContextual;
5411+
SmallPtrSet<TypeBase *, 4> withContextual;
54115412
typeCheckExpr(TC, callExpr, DC, withContextual, contextualType);
54125413
// If type-checking with contextual type didn't produce any results
54135414
// it means that we have a contextual mismatch.
5414-
if (withContextual.empty())
5415+
if (withContextual.empty()) {
54155416
return diagnoseContextualConversionError(callExpr, contextualType, CTP);
5417+
}
54165418

54175419
// If call produces a single type when type-checked with contextual
54185420
// expression, it means that the problem is elsewhere, any other
@@ -5431,15 +5433,15 @@ static bool shouldTypeCheckFunctionExpr(TypeChecker &TC, DeclContext *DC,
54315433
if (!isa<UnresolvedDotExpr>(fnExpr))
54325434
return true;
54335435

5434-
SmallVector<Type, 4> fnTypes;
5436+
SmallPtrSet<TypeBase *, 4> fnTypes;
54355437
TC.getPossibleTypesOfExpressionWithoutApplying(fnExpr, DC, fnTypes,
54365438
FreeTypeVariableBinding::UnresolvedType);
54375439

54385440
if (fnTypes.size() == 1) {
54395441
// Some member types depend on the arguments to produce a result type,
54405442
// type-checking such expressions without associated arguments is
54415443
// going to produce unrelated diagnostics.
5442-
if (auto fn = fnTypes[0]->getAs<AnyFunctionType>()) {
5444+
if (auto fn = (*fnTypes.begin())->getAs<AnyFunctionType>()) {
54435445
auto resultType = fn->getResult();
54445446
if (resultType->hasUnresolvedType() || resultType->hasTypeVariable())
54455447
return false;
@@ -5494,7 +5496,7 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
54945496
isa<UnresolvedDotExpr>(callExpr->getFn())) {
54955497
fnExpr = callExpr->getFn();
54965498

5497-
SmallVector<Type, 4> types;
5499+
SmallPtrSet<TypeBase *, 4> types;
54985500
CS.TC.getPossibleTypesOfExpressionWithoutApplying(fnExpr, CS.DC, types);
54995501

55005502
auto isFunctionType = [getFuncType](Type type) -> bool {

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,7 +1994,7 @@ getTypeOfExpressionWithoutApplying(Expr *&expr, DeclContext *dc,
19941994
}
19951995

19961996
void TypeChecker::getPossibleTypesOfExpressionWithoutApplying(
1997-
Expr *&expr, DeclContext *dc, SmallVectorImpl<Type> &types,
1997+
Expr *&expr, DeclContext *dc, SmallPtrSetImpl<TypeBase *> &types,
19981998
FreeTypeVariableBinding allowFreeTypeVariables,
19991999
ExprTypeCheckListener *listener) {
20002000
PrettyStackTraceExpr stackTrace(Context, "type-checking", expr);
@@ -2028,7 +2028,7 @@ void TypeChecker::getPossibleTypesOfExpressionWithoutApplying(
20282028
for (auto &solution : viable) {
20292029
auto exprType = solution.simplifyType(cs.getType(expr));
20302030
assert(exprType && !exprType->hasTypeVariable());
2031-
types.push_back(exprType);
2031+
types.insert(exprType.getPointer());
20322032
}
20332033
}
20342034

lib/Sema/TypeChecker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1743,7 +1743,7 @@ class TypeChecker final : public LazyResolver {
17431743
ExprTypeCheckListener *listener = nullptr);
17441744

17451745
void getPossibleTypesOfExpressionWithoutApplying(
1746-
Expr *&expr, DeclContext *dc, SmallVectorImpl<Type> &types,
1746+
Expr *&expr, DeclContext *dc, SmallPtrSetImpl<TypeBase *> &types,
17471747
FreeTypeVariableBinding allowFreeTypeVariables =
17481748
FreeTypeVariableBinding::Disallow,
17491749
ExprTypeCheckListener *listener = nullptr);

0 commit comments

Comments
 (0)