Skip to content

Commit cdbbe17

Browse files
committed
Sema: Remove TypeMatchResult
1 parent faa3376 commit cdbbe17

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
@@ -4591,31 +4591,6 @@ class ConstraintSystem {
45914591
Error
45924592
};
45934593

4594-
class TypeMatchResult {
4595-
SolutionKind Kind;
4596-
4597-
public:
4598-
inline bool isSuccess() const { return Kind == SolutionKind::Solved; }
4599-
inline bool isFailure() const { return Kind == SolutionKind::Error; }
4600-
inline bool isAmbiguous() const { return Kind == SolutionKind::Unsolved; }
4601-
4602-
static TypeMatchResult success() {
4603-
return {SolutionKind::Solved};
4604-
}
4605-
4606-
static TypeMatchResult failure() {
4607-
return {SolutionKind::Error};
4608-
}
4609-
4610-
static TypeMatchResult ambiguous() {
4611-
return {SolutionKind::Unsolved};
4612-
}
4613-
4614-
operator SolutionKind() { return Kind; }
4615-
private:
4616-
TypeMatchResult(SolutionKind result) : Kind(result) {}
4617-
};
4618-
46194594
/// Attempt to repair typing failures and record fixes if needed.
46204595
/// \return true if at least some of the failures has been repaired
46214596
/// successfully, which allows type matcher to continue.
@@ -4624,12 +4599,12 @@ class ConstraintSystem {
46244599
SmallVectorImpl<RestrictionOrFix> &conversionsOrFixes,
46254600
ConstraintLocatorBuilder locator);
46264601

4627-
TypeMatchResult
4602+
SolutionKind
46284603
matchPackTypes(PackType *pack1, PackType *pack2,
46294604
ConstraintKind kind, TypeMatchOptions flags,
46304605
ConstraintLocatorBuilder locator);
46314606

4632-
TypeMatchResult
4607+
SolutionKind
46334608
matchPackExpansionTypes(PackExpansionType *expansion1,
46344609
PackExpansionType *expansion2,
46354610
ConstraintKind kind, TypeMatchOptions flags,
@@ -4638,13 +4613,13 @@ class ConstraintSystem {
46384613
/// Subroutine of \c matchTypes(), which matches up two tuple types.
46394614
///
46404615
/// \returns the result of performing the tuple-to-tuple conversion.
4641-
TypeMatchResult matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
4642-
ConstraintKind kind, TypeMatchOptions flags,
4643-
ConstraintLocatorBuilder locator);
4616+
SolutionKind matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
4617+
ConstraintKind kind, TypeMatchOptions flags,
4618+
ConstraintLocatorBuilder locator);
46444619

46454620
/// Subroutine of \c matchTypes(), which matches up two function
46464621
/// types.
4647-
TypeMatchResult matchFunctionTypes(FunctionType *func1, FunctionType *func2,
4622+
SolutionKind matchFunctionTypes(FunctionType *func1, FunctionType *func2,
46484623
ConstraintKind kind, TypeMatchOptions flags,
46494624
ConstraintLocatorBuilder locator);
46504625

@@ -4655,14 +4630,14 @@ class ConstraintSystem {
46554630

46564631
/// Subroutine of \c matchTypes(), which matches up a value to a
46574632
/// superclass.
4658-
TypeMatchResult matchSuperclassTypes(Type type1, Type type2,
4659-
TypeMatchOptions flags,
4660-
ConstraintLocatorBuilder locator);
4633+
SolutionKind matchSuperclassTypes(Type type1, Type type2,
4634+
TypeMatchOptions flags,
4635+
ConstraintLocatorBuilder locator);
46614636

46624637
/// Subroutine of \c matchTypes(), which matches up two types that
46634638
/// refer to the same declaration via their generic arguments.
4664-
TypeMatchResult matchDeepEqualityTypes(Type type1, Type type2,
4665-
ConstraintLocatorBuilder locator);
4639+
SolutionKind matchDeepEqualityTypes(Type type1, Type type2,
4640+
ConstraintLocatorBuilder locator);
46664641

46674642
/// Subroutine of \c matchTypes(), which matches up a value to an
46684643
/// existential type.
@@ -4671,23 +4646,23 @@ class ConstraintSystem {
46714646
/// Usually this uses Subtype, but when matching the instance type of a
46724647
/// metatype with the instance type of an existential metatype, since we
46734648
/// want an actual conformance check.
4674-
TypeMatchResult matchExistentialTypes(Type type1, Type type2,
4675-
ConstraintKind kind,
4676-
TypeMatchOptions flags,
4677-
ConstraintLocatorBuilder locator);
4649+
SolutionKind matchExistentialTypes(Type type1, Type type2,
4650+
ConstraintKind kind,
4651+
TypeMatchOptions flags,
4652+
ConstraintLocatorBuilder locator);
46784653

46794654
/// Subroutine of \c matchTypes(), used to bind a type to a
46804655
/// type variable.
4681-
TypeMatchResult matchTypesBindTypeVar(
4656+
SolutionKind matchTypesBindTypeVar(
46824657
TypeVariableType *typeVar, Type type, ConstraintKind kind,
46834658
TypeMatchOptions flags, ConstraintLocatorBuilder locator,
4684-
llvm::function_ref<TypeMatchResult()> formUnsolvedResult);
4659+
llvm::function_ref<SolutionKind()> formUnsolvedResult);
46854660

46864661
/// Matches two function result types for a function application. This is
46874662
/// usually a bind, but also handles e.g IUO unwraps.
4688-
TypeMatchResult matchFunctionResultTypes(Type expectedResult, Type fnResult,
4689-
TypeMatchOptions flags,
4690-
ConstraintLocatorBuilder locator);
4663+
SolutionKind matchFunctionResultTypes(Type expectedResult, Type fnResult,
4664+
TypeMatchOptions flags,
4665+
ConstraintLocatorBuilder locator);
46914666

46924667
public: // FIXME: public due to statics in CSSimplify.cpp
46934668
/// Attempt to match up types \c type1 and \c type2, which in effect
@@ -4707,21 +4682,9 @@ class ConstraintSystem {
47074682
/// the specific types being matched.
47084683
///
47094684
/// \returns the result of attempting to solve this constraint.
4710-
TypeMatchResult matchTypes(Type type1, Type type2, ConstraintKind kind,
4711-
TypeMatchOptions flags,
4712-
ConstraintLocatorBuilder locator);
4713-
4714-
TypeMatchResult getTypeMatchSuccess() {
4715-
return TypeMatchResult::success();
4716-
}
4717-
4718-
TypeMatchResult getTypeMatchFailure(ConstraintLocatorBuilder locator) {
4719-
return TypeMatchResult::failure();
4720-
}
4721-
4722-
TypeMatchResult getTypeMatchAmbiguous() {
4723-
return TypeMatchResult::ambiguous();
4724-
}
4685+
SolutionKind matchTypes(Type type1, Type type2, ConstraintKind kind,
4686+
TypeMatchOptions flags,
4687+
ConstraintLocatorBuilder locator);
47254688

47264689
public:
47274690
// Build a disjunction that attempts both T? and T for a particular
@@ -5123,7 +5086,7 @@ class ConstraintSystem {
51235086
///
51245087
/// \returns \c None when the result builder cannot be applied at all,
51255088
/// otherwise the result of applying the result builder.
5126-
std::optional<TypeMatchResult>
5089+
std::optional<SolutionKind>
51275090
matchResultBuilder(AnyFunctionRef fn, Type builderType, Type bodyResultType,
51285091
ConstraintKind bodyResultConstraintKind,
51295092
Type contextualType, ConstraintLocatorBuilder locator);
@@ -5138,7 +5101,7 @@ class ConstraintSystem {
51385101

51395102
/// Matches a wrapped or projected value parameter type to its backing
51405103
/// property wrapper type by applying the property wrapper.
5141-
TypeMatchResult applyPropertyWrapperToParameter(
5104+
SolutionKind applyPropertyWrapperToParameter(
51425105
Type wrapperType, Type paramType, ParamDecl *param, Identifier argLabel,
51435106
ConstraintKind matchKind, ConstraintLocator *locator,
51445107
ConstraintLocator *calleeLocator);

lib/Sema/BuilderTransform.cpp

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

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

1096-
std::optional<ConstraintSystem::TypeMatchResult>
1096+
std::optional<ConstraintSystem::SolutionKind>
10971097
ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
10981098
Type bodyResultType,
10991099
ConstraintKind bodyResultConstraintKind,
@@ -1108,7 +1108,7 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
11081108
if (InvalidResultBuilderBodies.count(fn)) {
11091109
(void)recordFix(IgnoreInvalidResultBuilderBody::create(
11101110
*this, getConstraintLocator(fn.getAbstractClosureExpr())));
1111-
return getTypeMatchSuccess();
1111+
return SolutionKind::Solved;
11121112
}
11131113

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

1129-
return getTypeMatchSuccess();
1129+
return SolutionKind::Solved;
11301130
}
11311131

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

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

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

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

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

1175-
return getTypeMatchSuccess();
1175+
return SolutionKind::Solved;
11761176
}
11771177

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

12051205
if (generateConstraints(fn, transformInfo.transformedBody))
1206-
return getTypeMatchFailure(locator);
1206+
return SolutionKind::Error;
12071207

1208-
return getTypeMatchSuccess();
1208+
return SolutionKind::Solved;
12091209
}
12101210

12111211
void ConstraintSystem::recordResultBuilderTransform(AnyFunctionRef fn,

lib/Sema/CSBindings.cpp

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

2897-
if (result.isFailure()) {
2897+
if (result == ConstraintSystem::SolutionKind::Error) {
28982898
if (cs.isDebugMode()) {
28992899
PrintOptions PO;
29002900
PO.PrintTypesForDebugging = true;

lib/Sema/CSFix.cpp

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

1793-
if (result.isFailure())
1793+
if (result == ConstraintSystem::SolutionKind::Error)
17941794
return nullptr;
17951795

17961796
return new (cs.getAllocator())
@@ -2086,7 +2086,7 @@ UnwrapOptionalBaseKeyPathApplication::attempt(ConstraintSystem &cs, Type baseTy,
20862086
auto result =
20872087
cs.matchTypes(nonOptionalTy, rootTy, ConstraintKind::Subtype,
20882088
ConstraintSystem::TypeMatchFlags::TMF_ApplyingFix, locator);
2089-
if (result.isFailure())
2089+
if (result == ConstraintSystem::SolutionKind::Error)
20902090
return nullptr;
20912091

20922092
return new (cs.getAllocator())

lib/Sema/CSGen.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4221,23 +4221,23 @@ void ConstraintSystem::removePropertyWrapper(Expr *anchor) {
42214221
}
42224222
}
42234223

4224-
ConstraintSystem::TypeMatchResult
4224+
ConstraintSystem::SolutionKind
42254225
ConstraintSystem::applyPropertyWrapperToParameter(
42264226
Type wrapperType, Type paramType, ParamDecl *param, Identifier argLabel,
42274227
ConstraintKind matchKind, ConstraintLocator *locator,
42284228
ConstraintLocator *calleeLocator) {
42294229
Expr *anchor = getAsExpr(calleeLocator->getAnchor());
42304230

4231-
auto recordPropertyWrapperFix = [&](ConstraintFix *fix) -> TypeMatchResult {
4231+
auto recordPropertyWrapperFix = [&](ConstraintFix *fix) -> SolutionKind {
42324232
if (!shouldAttemptFixes())
4233-
return getTypeMatchFailure(locator);
4233+
return SolutionKind::Error;
42344234

42354235
recordAnyTypeVarAsPotentialHole(paramType);
42364236

42374237
if (recordFix(fix))
4238-
return getTypeMatchFailure(locator);
4238+
return SolutionKind::Error;
42394239

4240-
return getTypeMatchSuccess();
4240+
return SolutionKind::Solved;
42414241
};
42424242

42434243
// Incorrect use of projected value argument
@@ -4277,10 +4277,10 @@ ConstraintSystem::applyPropertyWrapperToParameter(
42774277

42784278
applyPropertyWrapper(anchor, { wrapperType, PropertyWrapperInitKind::WrappedValue });
42794279
} else {
4280-
return getTypeMatchFailure(locator);
4280+
return SolutionKind::Error;
42814281
}
42824282

4283-
return getTypeMatchSuccess();
4283+
return SolutionKind::Solved;
42844284
}
42854285

42864286
struct ResolvedMemberResult::Implementation {

0 commit comments

Comments
 (0)