Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions include/swift/Sema/SyntacticElementTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,6 @@ class SyntacticElementTarget {
ConstraintLocator *convertTypeLocator,
bool isDiscarded);

SyntacticElementTarget(Expr *expr, DeclContext *dc, ExprPattern *pattern,
Type patternType)
: SyntacticElementTarget(expr, dc, CTP_ExprPattern, patternType,
/*isDiscarded=*/false) {
setPattern(pattern);
}

SyntacticElementTarget(ClosureExpr *closure, Type convertType) {
kind = Kind::closure;
this->closure.closure = closure;
Expand Down Expand Up @@ -296,11 +289,8 @@ class SyntacticElementTarget {
forPropertyWrapperInitializer(VarDecl *wrappedVar, DeclContext *dc,
Expr *initializer);

static SyntacticElementTarget forExprPattern(Expr *expr, DeclContext *dc,
ExprPattern *pattern,
Type patternTy) {
return {expr, dc, pattern, patternTy};
}
/// Form a target for the match expression of an ExprPattern.
static SyntacticElementTarget forExprPattern(ExprPattern *pattern);

/// This is useful for code completion.
ASTNode getAsASTNode() const {
Expand Down Expand Up @@ -781,11 +771,13 @@ class SyntacticElementTarget {

// For an initialization, include the pattern in the range too.
if (isForInitialization()) {
if (auto patternRange = getInitializationPattern()->getSourceRange()) {
if (range.isInvalid()) {
range = patternRange;
} else {
range.widen(patternRange);
if (auto *pattern = getInitializationPattern()) {
if (auto patternRange = pattern->getSourceRange()) {
if (range.isInvalid()) {
range = patternRange;
} else {
range.widen(patternRange);
}
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9957,17 +9957,13 @@ static bool inferEnumMemberThroughTildeEqualsOperator(
if (!pattern->hasUnresolvedOriginalExpr())
return true;

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

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

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

DiagnosticTransaction diagnostics(ctx.Diags);
{
Expand Down
7 changes: 4 additions & 3 deletions lib/Sema/CSSyntacticElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,12 @@ using ElementInfo = std::tuple<ASTNode, ContextualTypeInfo,

static void createConjunction(ConstraintSystem &cs,
ArrayRef<ElementInfo> elements,
ConstraintLocator *locator) {
bool isIsolated = false;

ConstraintLocator *locator,
bool isIsolated = false,
ArrayRef<TypeVariableType *> extraTypeVars = {}) {
SmallVector<Constraint *, 4> constraints;
SmallVector<TypeVariableType *, 2> referencedVars;
referencedVars.append(extraTypeVars.begin(), extraTypeVars.end());

if (locator->directlyAt<ClosureExpr>()) {
auto *closure = castToExpr<ClosureExpr>(locator->getAnchor());
Expand Down
12 changes: 12 additions & 0 deletions lib/Sema/SyntacticElementTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ SyntacticElementTarget SyntacticElementTarget::forPropertyWrapperInitializer(
return target;
}

SyntacticElementTarget
SyntacticElementTarget::forExprPattern(ExprPattern *pattern) {
auto *DC = pattern->getDeclContext();
auto &ctx = DC->getASTContext();

// Result of ~= operator is always a `Bool`.
SyntacticElementTarget target(pattern->getMatchExpr(), DC, CTP_ExprPattern,
ctx.getBoolType(), /*isDiscarded*/ false);
target.setPattern(pattern);
return target;
}

ContextualPattern SyntacticElementTarget::getContextualPattern() const {
if (kind == Kind::uninitializedVar) {
assert(patternBinding);
Expand Down
9 changes: 2 additions & 7 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ bool TypeChecker::typeCheckCondition(Expr *&expr, DeclContext *dc) {
return !resultTy;
}

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

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

// Result of `~=` should always be a boolean.
auto *matchCall = EP->getMatchExpr();
auto contextualTy = Context.getBoolDecl()->getDeclaredInterfaceType();
auto target =
SyntacticElementTarget::forExprPattern(matchCall, DC, EP, contextualTy);

// Check the expression as a condition.
auto target = SyntacticElementTarget::forExprPattern(EP);
auto result = typeCheckExpression(target);
if (!result)
return true;
Expand Down