Skip to content

Commit ab7f8da

Browse files
committed
Sema: Remove TypeMatchResult
1 parent 115b537 commit ab7f8da

File tree

6 files changed

+279
-330
lines changed

6 files changed

+279
-330
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 25 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4700,31 +4700,6 @@ class ConstraintSystem {
47004700
Error
47014701
};
47024702

4703-
class TypeMatchResult {
4704-
SolutionKind Kind;
4705-
4706-
public:
4707-
inline bool isSuccess() const { return Kind == SolutionKind::Solved; }
4708-
inline bool isFailure() const { return Kind == SolutionKind::Error; }
4709-
inline bool isAmbiguous() const { return Kind == SolutionKind::Unsolved; }
4710-
4711-
static TypeMatchResult success() {
4712-
return {SolutionKind::Solved};
4713-
}
4714-
4715-
static TypeMatchResult failure() {
4716-
return {SolutionKind::Error};
4717-
}
4718-
4719-
static TypeMatchResult ambiguous() {
4720-
return {SolutionKind::Unsolved};
4721-
}
4722-
4723-
operator SolutionKind() { return Kind; }
4724-
private:
4725-
TypeMatchResult(SolutionKind result) : Kind(result) {}
4726-
};
4727-
47284703
/// Attempt to repair typing failures and record fixes if needed.
47294704
/// \return true if at least some of the failures has been repaired
47304705
/// successfully, which allows type matcher to continue.
@@ -4733,12 +4708,12 @@ class ConstraintSystem {
47334708
SmallVectorImpl<RestrictionOrFix> &conversionsOrFixes,
47344709
ConstraintLocatorBuilder locator);
47354710

4736-
TypeMatchResult
4711+
SolutionKind
47374712
matchPackTypes(PackType *pack1, PackType *pack2,
47384713
ConstraintKind kind, TypeMatchOptions flags,
47394714
ConstraintLocatorBuilder locator);
47404715

4741-
TypeMatchResult
4716+
SolutionKind
47424717
matchPackExpansionTypes(PackExpansionType *expansion1,
47434718
PackExpansionType *expansion2,
47444719
ConstraintKind kind, TypeMatchOptions flags,
@@ -4747,13 +4722,13 @@ class ConstraintSystem {
47474722
/// Subroutine of \c matchTypes(), which matches up two tuple types.
47484723
///
47494724
/// \returns the result of performing the tuple-to-tuple conversion.
4750-
TypeMatchResult matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
4751-
ConstraintKind kind, TypeMatchOptions flags,
4752-
ConstraintLocatorBuilder locator);
4725+
SolutionKind matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
4726+
ConstraintKind kind, TypeMatchOptions flags,
4727+
ConstraintLocatorBuilder locator);
47534728

47544729
/// Subroutine of \c matchTypes(), which matches up two function
47554730
/// types.
4756-
TypeMatchResult matchFunctionTypes(FunctionType *func1, FunctionType *func2,
4731+
SolutionKind matchFunctionTypes(FunctionType *func1, FunctionType *func2,
47574732
ConstraintKind kind, TypeMatchOptions flags,
47584733
ConstraintLocatorBuilder locator);
47594734

@@ -4764,14 +4739,14 @@ class ConstraintSystem {
47644739

47654740
/// Subroutine of \c matchTypes(), which matches up a value to a
47664741
/// superclass.
4767-
TypeMatchResult matchSuperclassTypes(Type type1, Type type2,
4768-
TypeMatchOptions flags,
4769-
ConstraintLocatorBuilder locator);
4742+
SolutionKind matchSuperclassTypes(Type type1, Type type2,
4743+
TypeMatchOptions flags,
4744+
ConstraintLocatorBuilder locator);
47704745

47714746
/// Subroutine of \c matchTypes(), which matches up two types that
47724747
/// refer to the same declaration via their generic arguments.
4773-
TypeMatchResult matchDeepEqualityTypes(Type type1, Type type2,
4774-
ConstraintLocatorBuilder locator);
4748+
SolutionKind matchDeepEqualityTypes(Type type1, Type type2,
4749+
ConstraintLocatorBuilder locator);
47754750

47764751
/// Subroutine of \c matchTypes(), which matches up a value to an
47774752
/// existential type.
@@ -4780,23 +4755,23 @@ class ConstraintSystem {
47804755
/// Usually this uses Subtype, but when matching the instance type of a
47814756
/// metatype with the instance type of an existential metatype, since we
47824757
/// want an actual conformance check.
4783-
TypeMatchResult matchExistentialTypes(Type type1, Type type2,
4784-
ConstraintKind kind,
4785-
TypeMatchOptions flags,
4786-
ConstraintLocatorBuilder locator);
4758+
SolutionKind matchExistentialTypes(Type type1, Type type2,
4759+
ConstraintKind kind,
4760+
TypeMatchOptions flags,
4761+
ConstraintLocatorBuilder locator);
47874762

47884763
/// Subroutine of \c matchTypes(), used to bind a type to a
47894764
/// type variable.
4790-
TypeMatchResult matchTypesBindTypeVar(
4765+
SolutionKind matchTypesBindTypeVar(
47914766
TypeVariableType *typeVar, Type type, ConstraintKind kind,
47924767
TypeMatchOptions flags, ConstraintLocatorBuilder locator,
4793-
llvm::function_ref<TypeMatchResult()> formUnsolvedResult);
4768+
llvm::function_ref<SolutionKind()> formUnsolvedResult);
47944769

47954770
/// Matches two function result types for a function application. This is
47964771
/// usually a bind, but also handles e.g IUO unwraps.
4797-
TypeMatchResult matchFunctionResultTypes(Type expectedResult, Type fnResult,
4798-
TypeMatchOptions flags,
4799-
ConstraintLocatorBuilder locator);
4772+
SolutionKind matchFunctionResultTypes(Type expectedResult, Type fnResult,
4773+
TypeMatchOptions flags,
4774+
ConstraintLocatorBuilder locator);
48004775

48014776
public: // FIXME: public due to statics in CSSimplify.cpp
48024777
/// Attempt to match up types \c type1 and \c type2, which in effect
@@ -4816,21 +4791,9 @@ class ConstraintSystem {
48164791
/// the specific types being matched.
48174792
///
48184793
/// \returns the result of attempting to solve this constraint.
4819-
TypeMatchResult matchTypes(Type type1, Type type2, ConstraintKind kind,
4820-
TypeMatchOptions flags,
4821-
ConstraintLocatorBuilder locator);
4822-
4823-
TypeMatchResult getTypeMatchSuccess() {
4824-
return TypeMatchResult::success();
4825-
}
4826-
4827-
TypeMatchResult getTypeMatchFailure(ConstraintLocatorBuilder locator) {
4828-
return TypeMatchResult::failure();
4829-
}
4830-
4831-
TypeMatchResult getTypeMatchAmbiguous() {
4832-
return TypeMatchResult::ambiguous();
4833-
}
4794+
SolutionKind matchTypes(Type type1, Type type2, ConstraintKind kind,
4795+
TypeMatchOptions flags,
4796+
ConstraintLocatorBuilder locator);
48344797

48354798
public:
48364799
// Build a disjunction that attempts both T? and T for a particular
@@ -5244,7 +5207,7 @@ class ConstraintSystem {
52445207
///
52455208
/// \returns \c None when the result builder cannot be applied at all,
52465209
/// otherwise the result of applying the result builder.
5247-
std::optional<TypeMatchResult>
5210+
std::optional<SolutionKind>
52485211
matchResultBuilder(AnyFunctionRef fn, Type builderType, Type bodyResultType,
52495212
ConstraintKind bodyResultConstraintKind,
52505213
Type contextualType, ConstraintLocatorBuilder locator);
@@ -5259,7 +5222,7 @@ class ConstraintSystem {
52595222

52605223
/// Matches a wrapped or projected value parameter type to its backing
52615224
/// property wrapper type by applying the property wrapper.
5262-
TypeMatchResult applyPropertyWrapperToParameter(
5225+
SolutionKind applyPropertyWrapperToParameter(
52635226
Type wrapperType, Type paramType, ParamDecl *param, Identifier argLabel,
52645227
ConstraintKind matchKind, ConstraintLocator *locator,
52655228
ConstraintLocator *calleeLocator);

lib/Sema/BuilderTransform.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ TypeChecker::applyResultBuilderBodyTransform(FuncDecl *func, Type builderType) {
983983
func, builderType, resultContextType, resultConstraintKind,
984984
/*contextualType=*/Type(),
985985
cs.getConstraintLocator(func->getBody()))) {
986-
if (result->isFailure())
986+
if (*result == ConstraintSystem::SolutionKind::Error)
987987
return nullptr;
988988
}
989989

@@ -1094,7 +1094,7 @@ TypeChecker::applyResultBuilderBodyTransform(FuncDecl *func, Type builderType) {
10941094
return nullptr;
10951095
}
10961096

1097-
std::optional<ConstraintSystem::TypeMatchResult>
1097+
std::optional<ConstraintSystem::SolutionKind>
10981098
ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
10991099
Type bodyResultType,
11001100
ConstraintKind bodyResultConstraintKind,
@@ -1109,7 +1109,7 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
11091109
if (InvalidResultBuilderBodies.count(fn)) {
11101110
(void)recordFix(IgnoreInvalidResultBuilderBody::create(
11111111
*this, getConstraintLocator(fn.getAbstractClosureExpr())));
1112-
return getTypeMatchSuccess();
1112+
return SolutionKind::Solved;
11131113
}
11141114

11151115
// We have already pre-checked the result builder body. Technically, we
@@ -1125,9 +1125,9 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
11251125
if (recordFix(IgnoreResultBuilderWithReturnStmts::create(
11261126
*this, builderType,
11271127
getConstraintLocator(fn.getAbstractClosureExpr()))))
1128-
return getTypeMatchFailure(locator);
1128+
return SolutionKind::Error;
11291129

1130-
return getTypeMatchSuccess();
1130+
return SolutionKind::Solved;
11311131
}
11321132

11331133
// If the body has a return statement, suppress the transform but
@@ -1149,31 +1149,31 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
11491149

11501150
// If we aren't supposed to attempt fixes, fail.
11511151
if (!shouldAttemptFixes()) {
1152-
return getTypeMatchFailure(locator);
1152+
return SolutionKind::Error;
11531153
}
11541154

11551155
// If we're solving for code completion and the body contains the code
11561156
// completion location, skipping it won't get us to a useful solution so
11571157
// just bail.
11581158
if (isForCodeCompletion() &&
11591159
containsIDEInspectionTarget(fn.getBody())) {
1160-
return getTypeMatchFailure(locator);
1160+
return SolutionKind::Error;
11611161
}
11621162

11631163
// Record the first unhandled construct as a fix.
11641164
if (recordFix(
11651165
SkipUnhandledConstructInResultBuilder::create(
11661166
*this, unsupported, builder, getConstraintLocator(locator)),
11671167
/*impact=*/100)) {
1168-
return getTypeMatchFailure(locator);
1168+
return SolutionKind::Error;
11691169
}
11701170

11711171
if (auto *closure =
11721172
getAsExpr<ClosureExpr>(fn.getAbstractClosureExpr())) {
11731173
recordTypeVariablesAsHoles(getClosureType(closure));
11741174
}
11751175

1176-
return getTypeMatchSuccess();
1176+
return SolutionKind::Solved;
11771177
}
11781178

11791179
transformedBody = std::make_pair(transform.getBuilderSelf(), body);
@@ -1204,9 +1204,9 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
12041204
recordResultBuilderTransform(fn, std::move(transformInfo));
12051205

12061206
if (generateConstraints(fn, transformInfo.transformedBody))
1207-
return getTypeMatchFailure(locator);
1207+
return SolutionKind::Error;
12081208

1209-
return getTypeMatchSuccess();
1209+
return SolutionKind::Solved;
12101210
}
12111211

12121212
void ConstraintSystem::recordResultBuilderTransform(AnyFunctionRef fn,

lib/Sema/CSBindings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2941,7 +2941,7 @@ bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {
29412941
auto result =
29422942
cs.matchTypes(TypeVar, type, ConstraintKind::Bind, options, srcLocator);
29432943

2944-
if (result.isFailure()) {
2944+
if (result == ConstraintSystem::SolutionKind::Error) {
29452945
if (cs.isDebugMode()) {
29462946
PrintOptions PO;
29472947
PO.PrintTypesForDebugging = true;

lib/Sema/CSFix.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,7 @@ ExpandArrayIntoVarargs::attempt(ConstraintSystem &cs, Type argType,
17931793
auto result = cs.matchTypes(elementType, paramType, ConstraintKind::Subtype,
17941794
options, builder);
17951795

1796-
if (result.isFailure())
1796+
if (result == ConstraintSystem::SolutionKind::Error)
17971797
return nullptr;
17981798

17991799
return new (cs.getAllocator())
@@ -2089,7 +2089,7 @@ UnwrapOptionalBaseKeyPathApplication::attempt(ConstraintSystem &cs, Type baseTy,
20892089
auto result =
20902090
cs.matchTypes(nonOptionalTy, rootTy, ConstraintKind::Subtype,
20912091
ConstraintSystem::TypeMatchFlags::TMF_ApplyingFix, locator);
2092-
if (result.isFailure())
2092+
if (result == ConstraintSystem::SolutionKind::Error)
20932093
return nullptr;
20942094

20952095
return new (cs.getAllocator())

lib/Sema/CSGen.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5046,23 +5046,23 @@ void ConstraintSystem::removePropertyWrapper(Expr *anchor) {
50465046
}
50475047
}
50485048

5049-
ConstraintSystem::TypeMatchResult
5049+
ConstraintSystem::SolutionKind
50505050
ConstraintSystem::applyPropertyWrapperToParameter(
50515051
Type wrapperType, Type paramType, ParamDecl *param, Identifier argLabel,
50525052
ConstraintKind matchKind, ConstraintLocator *locator,
50535053
ConstraintLocator *calleeLocator) {
50545054
Expr *anchor = getAsExpr(calleeLocator->getAnchor());
50555055

5056-
auto recordPropertyWrapperFix = [&](ConstraintFix *fix) -> TypeMatchResult {
5056+
auto recordPropertyWrapperFix = [&](ConstraintFix *fix) -> SolutionKind {
50575057
if (!shouldAttemptFixes())
5058-
return getTypeMatchFailure(locator);
5058+
return SolutionKind::Error;
50595059

50605060
recordAnyTypeVarAsPotentialHole(paramType);
50615061

50625062
if (recordFix(fix))
5063-
return getTypeMatchFailure(locator);
5063+
return SolutionKind::Error;
50645064

5065-
return getTypeMatchSuccess();
5065+
return SolutionKind::Solved;
50665066
};
50675067

50685068
// Incorrect use of projected value argument
@@ -5102,10 +5102,10 @@ ConstraintSystem::applyPropertyWrapperToParameter(
51025102

51035103
applyPropertyWrapper(anchor, { wrapperType, PropertyWrapperInitKind::WrappedValue });
51045104
} else {
5105-
return getTypeMatchFailure(locator);
5105+
return SolutionKind::Error;
51065106
}
51075107

5108-
return getTypeMatchSuccess();
5108+
return SolutionKind::Solved;
51095109
}
51105110

51115111
void ConstraintSystem::optimizeConstraints(Expr *e) {

0 commit comments

Comments
 (0)