Skip to content

Commit c780ab8

Browse files
committed
Sema: Remove TypeMatchResult
1 parent 06b0d47 commit c780ab8

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
@@ -995,7 +995,7 @@ TypeChecker::applyResultBuilderBodyTransform(FuncDecl *func, Type builderType) {
995995
func, builderType, resultContextType, resultConstraintKind,
996996
/*contextualType=*/Type(),
997997
cs.getConstraintLocator(func->getBody()))) {
998-
if (result->isFailure())
998+
if (*result == ConstraintSystem::SolutionKind::Error)
999999
return nullptr;
10001000
}
10011001

@@ -1106,7 +1106,7 @@ TypeChecker::applyResultBuilderBodyTransform(FuncDecl *func, Type builderType) {
11061106
return nullptr;
11071107
}
11081108

1109-
std::optional<ConstraintSystem::TypeMatchResult>
1109+
std::optional<ConstraintSystem::SolutionKind>
11101110
ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
11111111
Type bodyResultType,
11121112
ConstraintKind bodyResultConstraintKind,
@@ -1121,7 +1121,7 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
11211121
if (InvalidResultBuilderBodies.count(fn)) {
11221122
(void)recordFix(IgnoreInvalidResultBuilderBody::create(
11231123
*this, getConstraintLocator(fn.getAbstractClosureExpr())));
1124-
return getTypeMatchSuccess();
1124+
return SolutionKind::Solved;
11251125
}
11261126

11271127
// We have already pre-checked the result builder body. Technically, we
@@ -1137,9 +1137,9 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
11371137
if (recordFix(IgnoreResultBuilderWithReturnStmts::create(
11381138
*this, builderType,
11391139
getConstraintLocator(fn.getAbstractClosureExpr()))))
1140-
return getTypeMatchFailure(locator);
1140+
return SolutionKind::Error;
11411141

1142-
return getTypeMatchSuccess();
1142+
return SolutionKind::Solved;
11431143
}
11441144

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

11621162
// If we aren't supposed to attempt fixes, fail.
11631163
if (!shouldAttemptFixes()) {
1164-
return getTypeMatchFailure(locator);
1164+
return SolutionKind::Error;
11651165
}
11661166

11671167
// If we're solving for code completion and the body contains the code
11681168
// completion location, skipping it won't get us to a useful solution so
11691169
// just bail.
11701170
if (isForCodeCompletion() &&
11711171
containsIDEInspectionTarget(fn.getBody())) {
1172-
return getTypeMatchFailure(locator);
1172+
return SolutionKind::Error;
11731173
}
11741174

11751175
// Record the first unhandled construct as a fix.
11761176
if (recordFix(
11771177
SkipUnhandledConstructInResultBuilder::create(
11781178
*this, unsupported, builder, getConstraintLocator(locator)),
11791179
/*impact=*/100)) {
1180-
return getTypeMatchFailure(locator);
1180+
return SolutionKind::Error;
11811181
}
11821182

11831183
if (auto *closure =
11841184
getAsExpr<ClosureExpr>(fn.getAbstractClosureExpr())) {
11851185
recordTypeVariablesAsHoles(getClosureType(closure));
11861186
}
11871187

1188-
return getTypeMatchSuccess();
1188+
return SolutionKind::Solved;
11891189
}
11901190

11911191
transformedBody = std::make_pair(transform.getBuilderSelf(), body);
@@ -1216,9 +1216,9 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
12161216
recordResultBuilderTransform(fn, std::move(transformInfo));
12171217

12181218
if (generateConstraints(fn, transformInfo.transformedBody))
1219-
return getTypeMatchFailure(locator);
1219+
return SolutionKind::Error;
12201220

1221-
return getTypeMatchSuccess();
1221+
return SolutionKind::Solved;
12221222
}
12231223

12241224
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)