Skip to content

[TypeChecker] Simplify ExprPattern from Bool literals in Bool? switch #79759

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 1 commit into from
Mar 6, 2025
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
47 changes: 34 additions & 13 deletions lib/Sema/TypeCheckPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1011,24 +1011,39 @@ void repairTupleOrAssociatedValuePatternIfApplicable(
}
}

/// Try to simplify an `ExprPattern` with a Boolean literal sub-expression
/// to a `BoolPattern`, recursively unwrapping optional types if necessary.
static NullablePtr<Pattern> simplifyToBoolPattern(ASTContext &Ctx,
const ExprPattern *EP,
const BooleanLiteralExpr *BLE,
Type patternTy) {
// If the type is Bool, return a BoolPattern.
if (patternTy->isBool()) {
auto *BP = new (Ctx) BoolPattern(BLE->getLoc(), BLE->getValue());
BP->setType(patternTy);
return BP;
}
// If the pattern type is optional, attempt to simplify the wrapped type.
// `true` and `false` are treated as if they had `?` appended
// for each level of optionality.
if (auto wrappedType = patternTy->getOptionalObjectType()) {
if (auto P =
simplifyToBoolPattern(Ctx, EP, BLE, wrappedType).getPtrOrNull()) {
auto OP = OptionalSomePattern::createImplicit(Ctx, P, P->getEndLoc());
OP->setType(patternTy);
return OP;
}
}
return nullptr;
}

NullablePtr<Pattern> TypeChecker::trySimplifyExprPattern(ExprPattern *EP,
Type patternTy) {
auto *subExpr = EP->getSubExpr();
auto &ctx = EP->getDeclContext()->getASTContext();

if (patternTy->isBool()) {
// The type is Bool.
// Check if the pattern is a Bool literal
auto *semanticSubExpr = subExpr->getSemanticsProvidingExpr();
if (auto *BLE = dyn_cast<BooleanLiteralExpr>(semanticSubExpr)) {
auto *BP = new (ctx) BoolPattern(BLE->getLoc(), BLE->getValue());
BP->setType(patternTy);
return BP;
}
}

// case nil is equivalent to .none when switching on Optionals.
if (auto *NLE = dyn_cast<NilLiteralExpr>(EP->getSubExpr())) {
if (auto *NLE = dyn_cast<NilLiteralExpr>(subExpr)) {
if (patternTy->getOptionalObjectType()) {
auto *NoneEnumElement = ctx.getOptionalNoneDecl();
return EnumElementPattern::createImplicit(
Expand All @@ -1045,7 +1060,13 @@ NullablePtr<Pattern> TypeChecker::trySimplifyExprPattern(ExprPattern *EP,
return nullptr;
}
}
return nullptr;

const auto *BLE =
dyn_cast<BooleanLiteralExpr>(subExpr->getSemanticsProvidingExpr());
if (!BLE)
return nullptr;

return simplifyToBoolPattern(ctx, EP, BLE, patternTy);
}

/// Perform top-down type coercion on the given pattern.
Expand Down
27 changes: 27 additions & 0 deletions test/Sema/exhaustive_switch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1489,3 +1489,30 @@ do {
case (e: .y, b: true)?: break
}
}

// https://github.com/swiftlang/swift/issues/61817
do {
let a: Bool? = true
switch a {
case true: break
case false: break
case nil: break
}

let result = {
switch a {
case true: true
case false: true
case nil: true
}
}
_ = result()

let b: Bool?? = true
switch b {
case true: break
case false: break
case nil: break
case nil?: break
}
}