Skip to content

[CS] Add contextual Bool type to switch-case where clause #63747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 18, 2023
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
3 changes: 2 additions & 1 deletion include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -5110,7 +5110,8 @@ class ConstraintSystem {
/// \returns true if an error occurred, false otherwise.
LLVM_NODISCARD
bool generateConstraints(SolutionApplicationTarget &target,
FreeTypeVariableBinding allowFreeTypeVariables);
FreeTypeVariableBinding allowFreeTypeVariables =
FreeTypeVariableBinding::Disallow);

/// Generate constraints for the body of the given function or closure.
///
Expand Down
24 changes: 12 additions & 12 deletions lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ class BuilderClosureVisitor
// binding.
if (cs) {
SolutionApplicationTarget target(patternBinding);
if (cs->generateConstraints(target, FreeTypeVariableBinding::Disallow))
if (cs->generateConstraints(target))
hadError = true;
}
}
Expand Down Expand Up @@ -661,7 +661,7 @@ class BuilderClosureVisitor
// FIXME: Add contextual type purpose for switch subjects?
SolutionApplicationTarget target(subjectExpr, dc, CTP_Unused, Type(),
/*isDiscarded=*/false);
if (cs->generateConstraints(target, FreeTypeVariableBinding::Disallow)) {
if (cs->generateConstraints(target)) {
hadError = true;
return nullptr;
}
Expand Down Expand Up @@ -789,7 +789,7 @@ class BuilderClosureVisitor
auto target = SolutionApplicationTarget::forForEachStmt(
forEachStmt, dc, /*bindPatternVarsOneWay=*/true);
if (cs) {
if (cs->generateConstraints(target, FreeTypeVariableBinding::Disallow)) {
if (cs->generateConstraints(target)) {
hadError = true;
return nullptr;
}
Expand Down Expand Up @@ -886,15 +886,15 @@ class BuilderClosureVisitor
}

if (cs) {
SolutionApplicationTarget target(
throwStmt->getSubExpr(), dc, CTP_ThrowStmt,
ctx.getErrorExistentialType(),
/*isDiscarded=*/false);
if (cs->generateConstraints(target, FreeTypeVariableBinding::Disallow))
hadError = true;

cs->setSolutionApplicationTarget(throwStmt, target);
}
SolutionApplicationTarget target(throwStmt->getSubExpr(), dc,
CTP_ThrowStmt,
ctx.getErrorExistentialType(),
/*isDiscarded=*/false);
if (cs->generateConstraints(target))
hadError = true;

cs->setSolutionApplicationTarget(throwStmt, target);
}

return nullptr;
}
Expand Down
17 changes: 5 additions & 12 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9224,20 +9224,13 @@ ExprWalker::rewriteTarget(SolutionApplicationTarget target) {
}

// If there is a guard expression, coerce that.
if (auto guardExpr = info.guardExpr) {
guardExpr = guardExpr->walk(*this);
if (!guardExpr)
return None;

// FIXME: Feels like we could leverage existing code more.
Type boolType = cs.getASTContext().getBoolType();
guardExpr = solution.coerceToType(
guardExpr, boolType, cs.getConstraintLocator(info.guardExpr));
if (!guardExpr)
if (auto *guardExpr = info.guardExpr) {
auto target = *cs.getSolutionApplicationTarget(guardExpr);
auto resultTarget = rewriteTarget(target);
if (!resultTarget)
return None;

(*caseLabelItem)->setGuardExpr(guardExpr);
solution.setExprTypes(guardExpr);
(*caseLabelItem)->setGuardExpr(resultTarget->getAsExpr());
}

return target;
Expand Down
19 changes: 12 additions & 7 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4074,7 +4074,7 @@ namespace {
auto &CS = CG.getConstraintSystem();
for (const auto &capture : captureList->getCaptureList()) {
SolutionApplicationTarget target(capture.PBD);
if (CS.generateConstraints(target, FreeTypeVariableBinding::Disallow))
if (CS.generateConstraints(target))
return Action::Stop();
}
}
Expand Down Expand Up @@ -4327,8 +4327,7 @@ generateForEachStmtConstraints(
TypeLoc::withoutLoc(sequenceProto->getDeclaredInterfaceType()),
CTP_ForEachSequence);

if (cs.generateConstraints(makeIteratorTarget,
FreeTypeVariableBinding::Disallow))
if (cs.generateConstraints(makeIteratorTarget))
return None;

forEachStmtInfo.makeIteratorVar = PB;
Expand Down Expand Up @@ -4613,7 +4612,7 @@ bool ConstraintSystem::generateConstraints(
: SolutionApplicationTarget::forUninitializedVar(
patternBinding, index, patternType);

if (generateConstraints(target, FreeTypeVariableBinding::Disallow)) {
if (generateConstraints(target)) {
hadError = true;
continue;
}
Expand Down Expand Up @@ -4698,7 +4697,7 @@ bool ConstraintSystem::generateConstraints(StmtCondition condition,
auto target = SolutionApplicationTarget(symbolExpr, dc, CTP_Unused,
Type(), /*isDiscarded=*/false);

if (generateConstraints(target, FreeTypeVariableBinding::Disallow))
if (generateConstraints(target))
return true;

setSolutionApplicationTarget(&condElement, target);
Expand Down Expand Up @@ -4769,9 +4768,15 @@ bool ConstraintSystem::generateConstraints(
// Generate constraints for the guard expression, if there is one.
Expr *guardExpr = caseLabelItem.getGuardExpr();
if (guardExpr) {
guardExpr = generateConstraints(guardExpr, dc);
if (!guardExpr)
auto &ctx = dc->getASTContext();
SolutionApplicationTarget guardTarget(
guardExpr, dc, CTP_Condition, ctx.getBoolType(), /*discarded*/ false);

if (generateConstraints(guardTarget))
return true;

guardExpr = guardTarget.getAsExpr();
setSolutionApplicationTarget(guardExpr, guardTarget);
}

// Save this info.
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9869,7 +9869,7 @@ static bool inferEnumMemberThroughTildeEqualsOperator(
}
}

if (cs.generateConstraints(target, FreeTypeVariableBinding::Disallow))
if (cs.generateConstraints(target))
return true;

// Sub-expression associated with expression pattern is the enum element
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1684,7 +1684,7 @@ bool ConstraintSystem::solveForCodeCompletion(
<< "--- Code Completion ---\n";
}

if (generateConstraints(target, FreeTypeVariableBinding::Disallow))
if (generateConstraints(target))
return false;

solveForCodeCompletion(solutions);
Expand Down
19 changes: 13 additions & 6 deletions lib/Sema/CSSyntacticElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,9 @@ class SyntacticElementConstraintGenerator
void visitCaseItem(CaseLabelItem *caseItem, ContextualTypeInfo contextInfo) {
assert(contextInfo.purpose == CTP_CaseStmt);

auto *DC = context.getAsDeclContext();
auto &ctx = DC->getASTContext();

// Resolve the pattern.
auto *pattern = caseItem->getPattern();
if (!caseItem->isPatternResolved()) {
Expand All @@ -554,11 +557,15 @@ class SyntacticElementConstraintGenerator

// Generate constraints for `where` clause (if any).
if (guardExpr) {
guardExpr = cs.generateConstraints(guardExpr, context.getAsDeclContext());
if (!guardExpr) {
SolutionApplicationTarget guardTarget(
guardExpr, DC, CTP_Condition, ctx.getBoolType(), /*discarded*/ false);

if (cs.generateConstraints(guardTarget)) {
hadError = true;
return;
}
guardExpr = guardTarget.getAsExpr();
cs.setSolutionApplicationTarget(guardExpr, guardTarget);
}

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

if (cs.generateConstraints(target, FreeTypeVariableBinding::Disallow)) {
if (cs.generateConstraints(target)) {
hadError = true;
return;
}
Expand Down Expand Up @@ -734,7 +741,7 @@ class SyntacticElementConstraintGenerator
if (isPlaceholderVar(patternBinding))
return;

if (cs.generateConstraints(*target, FreeTypeVariableBinding::Disallow)) {
if (cs.generateConstraints(*target)) {
hadError = true;
return;
}
Expand Down Expand Up @@ -1128,7 +1135,7 @@ class SyntacticElementConstraintGenerator
contextualResultInfo.getType(),
/*isDiscarded=*/false);

if (cs.generateConstraints(target, FreeTypeVariableBinding::Disallow)) {
if (cs.generateConstraints(target)) {
hadError = true;
return;
}
Expand Down Expand Up @@ -1430,7 +1437,7 @@ ConstraintSystem::simplifySyntacticElementConstraint(
contextInfo.purpose, contextInfo.getType(),
contextualTypeLoc, isDiscarded);

if (generateConstraints(target, FreeTypeVariableBinding::Disallow))
if (generateConstraints(target))
return SolutionKind::Error;

setSolutionApplicationTarget(expr, target);
Expand Down
16 changes: 16 additions & 0 deletions test/Constraints/diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,22 @@ func testUnwrapFixIts(x: Int?) throws {
let _: Int = try! .optionalThrowsMember ?? 0
}

// https://github.com/apple/swift/issues/63746
func issue63746() {
let fn1 = {
switch 0 {
case 1 where 0: // expected-error {{type 'Int' cannot be used as a boolean; test for '!= 0' instead}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an aside, this fix-it is useless for literals.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh yeah, filed #63753

()
}
}
let fn2 = {
switch 0 {
case 1 where 0: // expected-error {{type 'Int' cannot be used as a boolean; test for '!= 0' instead}}
break
}
}
}

func rdar86611718(list: [Int]) {
String(list.count())
// expected-error@-1 {{cannot call value of non-function type 'Int'}}
Expand Down