Skip to content

Commit c77caf2

Browse files
committed
[CS] NFC: Simplify SyntacticElementTarget construction for ExprPattern
1 parent ae756d0 commit c77caf2

File tree

4 files changed

+17
-24
lines changed

4 files changed

+17
-24
lines changed

Diff for: include/swift/Sema/SyntacticElementTarget.h

+2-12
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,6 @@ class SyntacticElementTarget {
190190
ConstraintLocator *convertTypeLocator,
191191
bool isDiscarded);
192192

193-
SyntacticElementTarget(Expr *expr, DeclContext *dc, ExprPattern *pattern,
194-
Type patternType)
195-
: SyntacticElementTarget(expr, dc, CTP_ExprPattern, patternType,
196-
/*isDiscarded=*/false) {
197-
setPattern(pattern);
198-
}
199-
200193
SyntacticElementTarget(ClosureExpr *closure, Type convertType) {
201194
kind = Kind::closure;
202195
this->closure.closure = closure;
@@ -296,11 +289,8 @@ class SyntacticElementTarget {
296289
forPropertyWrapperInitializer(VarDecl *wrappedVar, DeclContext *dc,
297290
Expr *initializer);
298291

299-
static SyntacticElementTarget forExprPattern(Expr *expr, DeclContext *dc,
300-
ExprPattern *pattern,
301-
Type patternTy) {
302-
return {expr, dc, pattern, patternTy};
303-
}
292+
/// Form a target for the match expression of an ExprPattern.
293+
static SyntacticElementTarget forExprPattern(ExprPattern *pattern);
304294

305295
/// This is useful for code completion.
306296
ASTNode getAsASTNode() const {

Diff for: lib/Sema/CSSimplify.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -9957,17 +9957,13 @@ static bool inferEnumMemberThroughTildeEqualsOperator(
99579957
if (!pattern->hasUnresolvedOriginalExpr())
99589958
return true;
99599959

9960-
auto *DC = pattern->getDeclContext();
99619960
auto &ctx = cs.getASTContext();
99629961

99639962
// Retrieve a corresponding ExprPattern which we can solve with ~=.
99649963
auto *EP =
99659964
llvm::cantFail(ctx.evaluator(EnumElementExprPatternRequest{pattern}));
99669965

9967-
// result of ~= operator is always a `Bool`.
9968-
auto *matchCall = EP->getMatchExpr();
9969-
auto target = SyntacticElementTarget::forExprPattern(
9970-
matchCall, DC, EP, ctx.getBoolDecl()->getDeclaredInterfaceType());
9966+
auto target = SyntacticElementTarget::forExprPattern(EP);
99719967

99729968
DiagnosticTransaction diagnostics(ctx.Diags);
99739969
{

Diff for: lib/Sema/SyntacticElementTarget.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ SyntacticElementTarget SyntacticElementTarget::forPropertyWrapperInitializer(
202202
return target;
203203
}
204204

205+
SyntacticElementTarget
206+
SyntacticElementTarget::forExprPattern(ExprPattern *pattern) {
207+
auto *DC = pattern->getDeclContext();
208+
auto &ctx = DC->getASTContext();
209+
210+
// Result of ~= operator is always a `Bool`.
211+
SyntacticElementTarget target(pattern->getMatchExpr(), DC, CTP_ExprPattern,
212+
ctx.getBoolType(), /*isDiscarded*/ false);
213+
target.setPattern(pattern);
214+
return target;
215+
}
216+
205217
ContextualPattern SyntacticElementTarget::getContextualPattern() const {
206218
if (kind == Kind::uninitializedVar) {
207219
assert(patternBinding);

Diff for: lib/Sema/TypeCheckConstraints.cpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ bool TypeChecker::typeCheckCondition(Expr *&expr, DeclContext *dc) {
919919
return !resultTy;
920920
}
921921

922-
/// Find the '~=` operator that can compare an expression inside a pattern to a
922+
/// Find the `~=` operator that can compare an expression inside a pattern to a
923923
/// value of a given type.
924924
bool TypeChecker::typeCheckExprPattern(ExprPattern *EP, DeclContext *DC,
925925
Type rhsType) {
@@ -930,13 +930,8 @@ bool TypeChecker::typeCheckExprPattern(ExprPattern *EP, DeclContext *DC,
930930

931931
EP->getMatchVar()->setInterfaceType(rhsType->mapTypeOutOfContext());
932932

933-
// Result of `~=` should always be a boolean.
934-
auto *matchCall = EP->getMatchExpr();
935-
auto contextualTy = Context.getBoolDecl()->getDeclaredInterfaceType();
936-
auto target =
937-
SyntacticElementTarget::forExprPattern(matchCall, DC, EP, contextualTy);
938-
939933
// Check the expression as a condition.
934+
auto target = SyntacticElementTarget::forExprPattern(EP);
940935
auto result = typeCheckExpression(target);
941936
if (!result)
942937
return true;

0 commit comments

Comments
 (0)