Skip to content

Commit c82189d

Browse files
authored
Merge pull request #63747 from hamishknight/oh-bool
2 parents b8a4b87 + f7191b3 commit c82189d

File tree

8 files changed

+62
-40
lines changed

8 files changed

+62
-40
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5110,7 +5110,8 @@ class ConstraintSystem {
51105110
/// \returns true if an error occurred, false otherwise.
51115111
LLVM_NODISCARD
51125112
bool generateConstraints(SolutionApplicationTarget &target,
5113-
FreeTypeVariableBinding allowFreeTypeVariables);
5113+
FreeTypeVariableBinding allowFreeTypeVariables =
5114+
FreeTypeVariableBinding::Disallow);
51145115

51155116
/// Generate constraints for the body of the given function or closure.
51165117
///

lib/Sema/BuilderTransform.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ class BuilderClosureVisitor
359359
// binding.
360360
if (cs) {
361361
SolutionApplicationTarget target(patternBinding);
362-
if (cs->generateConstraints(target, FreeTypeVariableBinding::Disallow))
362+
if (cs->generateConstraints(target))
363363
hadError = true;
364364
}
365365
}
@@ -661,7 +661,7 @@ class BuilderClosureVisitor
661661
// FIXME: Add contextual type purpose for switch subjects?
662662
SolutionApplicationTarget target(subjectExpr, dc, CTP_Unused, Type(),
663663
/*isDiscarded=*/false);
664-
if (cs->generateConstraints(target, FreeTypeVariableBinding::Disallow)) {
664+
if (cs->generateConstraints(target)) {
665665
hadError = true;
666666
return nullptr;
667667
}
@@ -789,7 +789,7 @@ class BuilderClosureVisitor
789789
auto target = SolutionApplicationTarget::forForEachStmt(
790790
forEachStmt, dc, /*bindPatternVarsOneWay=*/true);
791791
if (cs) {
792-
if (cs->generateConstraints(target, FreeTypeVariableBinding::Disallow)) {
792+
if (cs->generateConstraints(target)) {
793793
hadError = true;
794794
return nullptr;
795795
}
@@ -886,15 +886,15 @@ class BuilderClosureVisitor
886886
}
887887

888888
if (cs) {
889-
SolutionApplicationTarget target(
890-
throwStmt->getSubExpr(), dc, CTP_ThrowStmt,
891-
ctx.getErrorExistentialType(),
892-
/*isDiscarded=*/false);
893-
if (cs->generateConstraints(target, FreeTypeVariableBinding::Disallow))
894-
hadError = true;
895-
896-
cs->setSolutionApplicationTarget(throwStmt, target);
897-
}
889+
SolutionApplicationTarget target(throwStmt->getSubExpr(), dc,
890+
CTP_ThrowStmt,
891+
ctx.getErrorExistentialType(),
892+
/*isDiscarded=*/false);
893+
if (cs->generateConstraints(target))
894+
hadError = true;
895+
896+
cs->setSolutionApplicationTarget(throwStmt, target);
897+
}
898898

899899
return nullptr;
900900
}

lib/Sema/CSApply.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9224,20 +9224,13 @@ ExprWalker::rewriteTarget(SolutionApplicationTarget target) {
92249224
}
92259225

92269226
// If there is a guard expression, coerce that.
9227-
if (auto guardExpr = info.guardExpr) {
9228-
guardExpr = guardExpr->walk(*this);
9229-
if (!guardExpr)
9230-
return None;
9231-
9232-
// FIXME: Feels like we could leverage existing code more.
9233-
Type boolType = cs.getASTContext().getBoolType();
9234-
guardExpr = solution.coerceToType(
9235-
guardExpr, boolType, cs.getConstraintLocator(info.guardExpr));
9236-
if (!guardExpr)
9227+
if (auto *guardExpr = info.guardExpr) {
9228+
auto target = *cs.getSolutionApplicationTarget(guardExpr);
9229+
auto resultTarget = rewriteTarget(target);
9230+
if (!resultTarget)
92379231
return None;
92389232

9239-
(*caseLabelItem)->setGuardExpr(guardExpr);
9240-
solution.setExprTypes(guardExpr);
9233+
(*caseLabelItem)->setGuardExpr(resultTarget->getAsExpr());
92419234
}
92429235

92439236
return target;

lib/Sema/CSGen.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4074,7 +4074,7 @@ namespace {
40744074
auto &CS = CG.getConstraintSystem();
40754075
for (const auto &capture : captureList->getCaptureList()) {
40764076
SolutionApplicationTarget target(capture.PBD);
4077-
if (CS.generateConstraints(target, FreeTypeVariableBinding::Disallow))
4077+
if (CS.generateConstraints(target))
40784078
return Action::Stop();
40794079
}
40804080
}
@@ -4327,8 +4327,7 @@ generateForEachStmtConstraints(
43274327
TypeLoc::withoutLoc(sequenceProto->getDeclaredInterfaceType()),
43284328
CTP_ForEachSequence);
43294329

4330-
if (cs.generateConstraints(makeIteratorTarget,
4331-
FreeTypeVariableBinding::Disallow))
4330+
if (cs.generateConstraints(makeIteratorTarget))
43324331
return None;
43334332

43344333
forEachStmtInfo.makeIteratorVar = PB;
@@ -4613,7 +4612,7 @@ bool ConstraintSystem::generateConstraints(
46134612
: SolutionApplicationTarget::forUninitializedVar(
46144613
patternBinding, index, patternType);
46154614

4616-
if (generateConstraints(target, FreeTypeVariableBinding::Disallow)) {
4615+
if (generateConstraints(target)) {
46174616
hadError = true;
46184617
continue;
46194618
}
@@ -4698,7 +4697,7 @@ bool ConstraintSystem::generateConstraints(StmtCondition condition,
46984697
auto target = SolutionApplicationTarget(symbolExpr, dc, CTP_Unused,
46994698
Type(), /*isDiscarded=*/false);
47004699

4701-
if (generateConstraints(target, FreeTypeVariableBinding::Disallow))
4700+
if (generateConstraints(target))
47024701
return true;
47034702

47044703
setSolutionApplicationTarget(&condElement, target);
@@ -4769,9 +4768,15 @@ bool ConstraintSystem::generateConstraints(
47694768
// Generate constraints for the guard expression, if there is one.
47704769
Expr *guardExpr = caseLabelItem.getGuardExpr();
47714770
if (guardExpr) {
4772-
guardExpr = generateConstraints(guardExpr, dc);
4773-
if (!guardExpr)
4771+
auto &ctx = dc->getASTContext();
4772+
SolutionApplicationTarget guardTarget(
4773+
guardExpr, dc, CTP_Condition, ctx.getBoolType(), /*discarded*/ false);
4774+
4775+
if (generateConstraints(guardTarget))
47744776
return true;
4777+
4778+
guardExpr = guardTarget.getAsExpr();
4779+
setSolutionApplicationTarget(guardExpr, guardTarget);
47754780
}
47764781

47774782
// Save this info.

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9869,7 +9869,7 @@ static bool inferEnumMemberThroughTildeEqualsOperator(
98699869
}
98709870
}
98719871

9872-
if (cs.generateConstraints(target, FreeTypeVariableBinding::Disallow))
9872+
if (cs.generateConstraints(target))
98739873
return true;
98749874

98759875
// Sub-expression associated with expression pattern is the enum element

lib/Sema/CSSolver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,7 @@ bool ConstraintSystem::solveForCodeCompletion(
16841684
<< "--- Code Completion ---\n";
16851685
}
16861686

1687-
if (generateConstraints(target, FreeTypeVariableBinding::Disallow))
1687+
if (generateConstraints(target))
16881688
return false;
16891689

16901690
solveForCodeCompletion(solutions);

lib/Sema/CSSyntacticElement.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,9 @@ class SyntacticElementConstraintGenerator
529529
void visitCaseItem(CaseLabelItem *caseItem, ContextualTypeInfo contextInfo) {
530530
assert(contextInfo.purpose == CTP_CaseStmt);
531531

532+
auto *DC = context.getAsDeclContext();
533+
auto &ctx = DC->getASTContext();
534+
532535
// Resolve the pattern.
533536
auto *pattern = caseItem->getPattern();
534537
if (!caseItem->isPatternResolved()) {
@@ -554,11 +557,15 @@ class SyntacticElementConstraintGenerator
554557

555558
// Generate constraints for `where` clause (if any).
556559
if (guardExpr) {
557-
guardExpr = cs.generateConstraints(guardExpr, context.getAsDeclContext());
558-
if (!guardExpr) {
560+
SolutionApplicationTarget guardTarget(
561+
guardExpr, DC, CTP_Condition, ctx.getBoolType(), /*discarded*/ false);
562+
563+
if (cs.generateConstraints(guardTarget)) {
559564
hadError = true;
560565
return;
561566
}
567+
guardExpr = guardTarget.getAsExpr();
568+
cs.setSolutionApplicationTarget(guardExpr, guardTarget);
562569
}
563570

564571
// Save information about case item so it could be referenced during
@@ -580,7 +587,7 @@ class SyntacticElementConstraintGenerator
580587
forEachStmt, context.getAsDeclContext(),
581588
/*bindTypeVarsOneWay=*/false);
582589

583-
if (cs.generateConstraints(target, FreeTypeVariableBinding::Disallow)) {
590+
if (cs.generateConstraints(target)) {
584591
hadError = true;
585592
return;
586593
}
@@ -734,7 +741,7 @@ class SyntacticElementConstraintGenerator
734741
if (isPlaceholderVar(patternBinding))
735742
return;
736743

737-
if (cs.generateConstraints(*target, FreeTypeVariableBinding::Disallow)) {
744+
if (cs.generateConstraints(*target)) {
738745
hadError = true;
739746
return;
740747
}
@@ -1128,7 +1135,7 @@ class SyntacticElementConstraintGenerator
11281135
contextualResultInfo.getType(),
11291136
/*isDiscarded=*/false);
11301137

1131-
if (cs.generateConstraints(target, FreeTypeVariableBinding::Disallow)) {
1138+
if (cs.generateConstraints(target)) {
11321139
hadError = true;
11331140
return;
11341141
}
@@ -1430,7 +1437,7 @@ ConstraintSystem::simplifySyntacticElementConstraint(
14301437
contextInfo.purpose, contextInfo.getType(),
14311438
contextualTypeLoc, isDiscarded);
14321439

1433-
if (generateConstraints(target, FreeTypeVariableBinding::Disallow))
1440+
if (generateConstraints(target))
14341441
return SolutionKind::Error;
14351442

14361443
setSolutionApplicationTarget(expr, target);

test/Constraints/diagnostics.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,22 @@ func testUnwrapFixIts(x: Int?) throws {
15311531
let _: Int = try! .optionalThrowsMember ?? 0
15321532
}
15331533

1534+
// https://github.com/apple/swift/issues/63746
1535+
func issue63746() {
1536+
let fn1 = {
1537+
switch 0 {
1538+
case 1 where 0: // expected-error {{type 'Int' cannot be used as a boolean; test for '!= 0' instead}}
1539+
()
1540+
}
1541+
}
1542+
let fn2 = {
1543+
switch 0 {
1544+
case 1 where 0: // expected-error {{type 'Int' cannot be used as a boolean; test for '!= 0' instead}}
1545+
break
1546+
}
1547+
}
1548+
}
1549+
15341550
func rdar86611718(list: [Int]) {
15351551
String(list.count())
15361552
// expected-error@-1 {{cannot call value of non-function type 'Int'}}

0 commit comments

Comments
 (0)