Skip to content

Commit b3f54e5

Browse files
authored
Merge pull request #64347 from hamishknight/split-more
[CS] Split off a couple more changes from the ExprPattern work
2 parents 0fd1fde + c44675c commit b3f54e5

File tree

5 files changed

+28
-32
lines changed

5 files changed

+28
-32
lines changed

include/swift/Sema/SyntacticElementTarget.h

Lines changed: 9 additions & 17 deletions
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 {
@@ -781,11 +771,13 @@ class SyntacticElementTarget {
781771

782772
// For an initialization, include the pattern in the range too.
783773
if (isForInitialization()) {
784-
if (auto patternRange = getInitializationPattern()->getSourceRange()) {
785-
if (range.isInvalid()) {
786-
range = patternRange;
787-
} else {
788-
range.widen(patternRange);
774+
if (auto *pattern = getInitializationPattern()) {
775+
if (auto patternRange = pattern->getSourceRange()) {
776+
if (range.isInvalid()) {
777+
range = patternRange;
778+
} else {
779+
range.widen(patternRange);
780+
}
789781
}
790782
}
791783
}

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 5 deletions
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
{

lib/Sema/CSSyntacticElement.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,12 @@ using ElementInfo = std::tuple<ASTNode, ContextualTypeInfo,
306306

307307
static void createConjunction(ConstraintSystem &cs,
308308
ArrayRef<ElementInfo> elements,
309-
ConstraintLocator *locator) {
310-
bool isIsolated = false;
311-
309+
ConstraintLocator *locator,
310+
bool isIsolated = false,
311+
ArrayRef<TypeVariableType *> extraTypeVars = {}) {
312312
SmallVector<Constraint *, 4> constraints;
313313
SmallVector<TypeVariableType *, 2> referencedVars;
314+
referencedVars.append(extraTypeVars.begin(), extraTypeVars.end());
314315

315316
if (locator->directlyAt<ClosureExpr>()) {
316317
auto *closure = castToExpr<ClosureExpr>(locator->getAnchor());

lib/Sema/SyntacticElementTarget.cpp

Lines changed: 12 additions & 0 deletions
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);

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 7 deletions
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)