Skip to content

Commit 5b4da72

Browse files
committed
Sema: Remove TypeMatchResult
1 parent f14820d commit 5b4da72

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
@@ -4688,31 +4688,6 @@ class ConstraintSystem {
46884688
Error
46894689
};
46904690

4691-
class TypeMatchResult {
4692-
SolutionKind Kind;
4693-
4694-
public:
4695-
inline bool isSuccess() const { return Kind == SolutionKind::Solved; }
4696-
inline bool isFailure() const { return Kind == SolutionKind::Error; }
4697-
inline bool isAmbiguous() const { return Kind == SolutionKind::Unsolved; }
4698-
4699-
static TypeMatchResult success() {
4700-
return {SolutionKind::Solved};
4701-
}
4702-
4703-
static TypeMatchResult failure() {
4704-
return {SolutionKind::Error};
4705-
}
4706-
4707-
static TypeMatchResult ambiguous() {
4708-
return {SolutionKind::Unsolved};
4709-
}
4710-
4711-
operator SolutionKind() { return Kind; }
4712-
private:
4713-
TypeMatchResult(SolutionKind result) : Kind(result) {}
4714-
};
4715-
47164691
/// Attempt to repair typing failures and record fixes if needed.
47174692
/// \return true if at least some of the failures has been repaired
47184693
/// successfully, which allows type matcher to continue.
@@ -4721,12 +4696,12 @@ class ConstraintSystem {
47214696
SmallVectorImpl<RestrictionOrFix> &conversionsOrFixes,
47224697
ConstraintLocatorBuilder locator);
47234698

4724-
TypeMatchResult
4699+
SolutionKind
47254700
matchPackTypes(PackType *pack1, PackType *pack2,
47264701
ConstraintKind kind, TypeMatchOptions flags,
47274702
ConstraintLocatorBuilder locator);
47284703

4729-
TypeMatchResult
4704+
SolutionKind
47304705
matchPackExpansionTypes(PackExpansionType *expansion1,
47314706
PackExpansionType *expansion2,
47324707
ConstraintKind kind, TypeMatchOptions flags,
@@ -4735,13 +4710,13 @@ class ConstraintSystem {
47354710
/// Subroutine of \c matchTypes(), which matches up two tuple types.
47364711
///
47374712
/// \returns the result of performing the tuple-to-tuple conversion.
4738-
TypeMatchResult matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
4739-
ConstraintKind kind, TypeMatchOptions flags,
4740-
ConstraintLocatorBuilder locator);
4713+
SolutionKind matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
4714+
ConstraintKind kind, TypeMatchOptions flags,
4715+
ConstraintLocatorBuilder locator);
47414716

47424717
/// Subroutine of \c matchTypes(), which matches up two function
47434718
/// types.
4744-
TypeMatchResult matchFunctionTypes(FunctionType *func1, FunctionType *func2,
4719+
SolutionKind matchFunctionTypes(FunctionType *func1, FunctionType *func2,
47454720
ConstraintKind kind, TypeMatchOptions flags,
47464721
ConstraintLocatorBuilder locator);
47474722

@@ -4752,14 +4727,14 @@ class ConstraintSystem {
47524727

47534728
/// Subroutine of \c matchTypes(), which matches up a value to a
47544729
/// superclass.
4755-
TypeMatchResult matchSuperclassTypes(Type type1, Type type2,
4756-
TypeMatchOptions flags,
4757-
ConstraintLocatorBuilder locator);
4730+
SolutionKind matchSuperclassTypes(Type type1, Type type2,
4731+
TypeMatchOptions flags,
4732+
ConstraintLocatorBuilder locator);
47584733

47594734
/// Subroutine of \c matchTypes(), which matches up two types that
47604735
/// refer to the same declaration via their generic arguments.
4761-
TypeMatchResult matchDeepEqualityTypes(Type type1, Type type2,
4762-
ConstraintLocatorBuilder locator);
4736+
SolutionKind matchDeepEqualityTypes(Type type1, Type type2,
4737+
ConstraintLocatorBuilder locator);
47634738

47644739
/// Subroutine of \c matchTypes(), which matches up a value to an
47654740
/// existential type.
@@ -4768,23 +4743,23 @@ class ConstraintSystem {
47684743
/// Usually this uses Subtype, but when matching the instance type of a
47694744
/// metatype with the instance type of an existential metatype, since we
47704745
/// want an actual conformance check.
4771-
TypeMatchResult matchExistentialTypes(Type type1, Type type2,
4772-
ConstraintKind kind,
4773-
TypeMatchOptions flags,
4774-
ConstraintLocatorBuilder locator);
4746+
SolutionKind matchExistentialTypes(Type type1, Type type2,
4747+
ConstraintKind kind,
4748+
TypeMatchOptions flags,
4749+
ConstraintLocatorBuilder locator);
47754750

47764751
/// Subroutine of \c matchTypes(), used to bind a type to a
47774752
/// type variable.
4778-
TypeMatchResult matchTypesBindTypeVar(
4753+
SolutionKind matchTypesBindTypeVar(
47794754
TypeVariableType *typeVar, Type type, ConstraintKind kind,
47804755
TypeMatchOptions flags, ConstraintLocatorBuilder locator,
4781-
llvm::function_ref<TypeMatchResult()> formUnsolvedResult);
4756+
llvm::function_ref<SolutionKind()> formUnsolvedResult);
47824757

47834758
/// Matches two function result types for a function application. This is
47844759
/// usually a bind, but also handles e.g IUO unwraps.
4785-
TypeMatchResult matchFunctionResultTypes(Type expectedResult, Type fnResult,
4786-
TypeMatchOptions flags,
4787-
ConstraintLocatorBuilder locator);
4760+
SolutionKind matchFunctionResultTypes(Type expectedResult, Type fnResult,
4761+
TypeMatchOptions flags,
4762+
ConstraintLocatorBuilder locator);
47884763

47894764
public: // FIXME: public due to statics in CSSimplify.cpp
47904765
/// Attempt to match up types \c type1 and \c type2, which in effect
@@ -4804,21 +4779,9 @@ class ConstraintSystem {
48044779
/// the specific types being matched.
48054780
///
48064781
/// \returns the result of attempting to solve this constraint.
4807-
TypeMatchResult matchTypes(Type type1, Type type2, ConstraintKind kind,
4808-
TypeMatchOptions flags,
4809-
ConstraintLocatorBuilder locator);
4810-
4811-
TypeMatchResult getTypeMatchSuccess() {
4812-
return TypeMatchResult::success();
4813-
}
4814-
4815-
TypeMatchResult getTypeMatchFailure(ConstraintLocatorBuilder locator) {
4816-
return TypeMatchResult::failure();
4817-
}
4818-
4819-
TypeMatchResult getTypeMatchAmbiguous() {
4820-
return TypeMatchResult::ambiguous();
4821-
}
4782+
SolutionKind matchTypes(Type type1, Type type2, ConstraintKind kind,
4783+
TypeMatchOptions flags,
4784+
ConstraintLocatorBuilder locator);
48224785

48234786
public:
48244787
// Build a disjunction that attempts both T? and T for a particular
@@ -5232,7 +5195,7 @@ class ConstraintSystem {
52325195
///
52335196
/// \returns \c None when the result builder cannot be applied at all,
52345197
/// otherwise the result of applying the result builder.
5235-
std::optional<TypeMatchResult>
5198+
std::optional<SolutionKind>
52365199
matchResultBuilder(AnyFunctionRef fn, Type builderType, Type bodyResultType,
52375200
ConstraintKind bodyResultConstraintKind,
52385201
Type contextualType, ConstraintLocatorBuilder locator);
@@ -5247,7 +5210,7 @@ class ConstraintSystem {
52475210

52485211
/// Matches a wrapped or projected value parameter type to its backing
52495212
/// property wrapper type by applying the property wrapper.
5250-
TypeMatchResult applyPropertyWrapperToParameter(
5213+
SolutionKind applyPropertyWrapperToParameter(
52515214
Type wrapperType, Type paramType, ParamDecl *param, Identifier argLabel,
52525215
ConstraintKind matchKind, ConstraintLocator *locator,
52535216
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
@@ -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
@@ -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
@@ -5061,23 +5061,23 @@ void ConstraintSystem::removePropertyWrapper(Expr *anchor) {
50615061
}
50625062
}
50635063

5064-
ConstraintSystem::TypeMatchResult
5064+
ConstraintSystem::SolutionKind
50655065
ConstraintSystem::applyPropertyWrapperToParameter(
50665066
Type wrapperType, Type paramType, ParamDecl *param, Identifier argLabel,
50675067
ConstraintKind matchKind, ConstraintLocator *locator,
50685068
ConstraintLocator *calleeLocator) {
50695069
Expr *anchor = getAsExpr(calleeLocator->getAnchor());
50705070

5071-
auto recordPropertyWrapperFix = [&](ConstraintFix *fix) -> TypeMatchResult {
5071+
auto recordPropertyWrapperFix = [&](ConstraintFix *fix) -> SolutionKind {
50725072
if (!shouldAttemptFixes())
5073-
return getTypeMatchFailure(locator);
5073+
return SolutionKind::Error;
50745074

50755075
recordAnyTypeVarAsPotentialHole(paramType);
50765076

50775077
if (recordFix(fix))
5078-
return getTypeMatchFailure(locator);
5078+
return SolutionKind::Error;
50795079

5080-
return getTypeMatchSuccess();
5080+
return SolutionKind::Solved;
50815081
};
50825082

50835083
// Incorrect use of projected value argument
@@ -5117,10 +5117,10 @@ ConstraintSystem::applyPropertyWrapperToParameter(
51175117

51185118
applyPropertyWrapper(anchor, { wrapperType, PropertyWrapperInitKind::WrappedValue });
51195119
} else {
5120-
return getTypeMatchFailure(locator);
5120+
return SolutionKind::Error;
51215121
}
51225122

5123-
return getTypeMatchSuccess();
5123+
return SolutionKind::Solved;
51245124
}
51255125

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

0 commit comments

Comments
 (0)