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

Conversation

jameesbrown
Copy link
Contributor

Resolves: swiftlang/swift#61817.

Boolean literals are treated as ExprPatterns when switching over Bool? because they require implicit conversion to Optionals, resulting in them being wrapped in InjectIntoOptionalExprs. Consequently, they do not contribute to exhausting the switch cases.

In this special case, we can instead treat them as true?/false?.

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

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

We need equivalent change in CSGen I think and a test-case where a switch is used inside of a closure.

@hamishknight could you please take a look at the changes?

@xedin xedin self-requested a review March 4, 2025 02:49
@xedin
Copy link
Contributor

xedin commented Mar 4, 2025

Approved by mistake, meant only to comment.

@jameesbrown
Copy link
Contributor Author

@xedin Thanks for looking at it. I will investigate a CSGen change and add that test case.

@xedin
Copy link
Contributor

xedin commented Mar 4, 2025

It should be pretty similar to what you already did, look at getTypeForPattern.

@hamishknight
Copy link
Contributor

I'm not actually sure this needs a CSGen change, trySimplifyExprPattern is called for both coercePatternToType and CSApply, and this is something that needs to operate on the type-checked pattern (since ExpressibleByBooleanLiteral means you can't always turn a top-level BooleanLiteralExpr into a BoolPattern). But yes, please add a test case in a closure :)

@jameesbrown jameesbrown force-pushed the bool-literal-case branch 3 times, most recently from 5e11cdd to 1e18a68 Compare March 4, 2025 23:47
@jameesbrown
Copy link
Contributor Author

@hamishknight Thanks! I took your suggestion and rewrote it recursively to handle any level of optionality. I also included the test case inside a closure.

@xedin I explored possible changes in getTypeForPattern, but without already knowing the type of ExprPattern, I couldn’t find a clear solution. It doesn’t seem like a necessary change, though it might be an optimization—potentially solving the constraints earlier?

@jameesbrown jameesbrown force-pushed the bool-literal-case branch 3 times, most recently from f37e0a0 to c904b83 Compare March 5, 2025 02:08
Copy link
Contributor

@hamishknight hamishknight left a comment

Choose a reason for hiding this comment

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

Thanks!

Bool literals are regarded as expr patterns when
switching over Optional<Bool>. Simplify the expr
by converting to their optional counterparts.
@hamishknight
Copy link
Contributor

@swift-ci please test

@hamishknight
Copy link
Contributor

@swift-ci please test source compatibility

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

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

LGTM! I originally thought that this needs to happen earlier.

@jameesbrown
Copy link
Contributor Author

jameesbrown commented Mar 5, 2025

The source compatibility checks are failing but so far I haven't found a failure that seems related to this change. For example, https://ci.swift.org/job/swift-PR-source-compat-suite-macos/2271/artifact/swift-source-compat-suite/FAIL_SwiftLint_5.0_BuildSwiftPackage.log.

Most seem related to not finding MacOS specific modules, but it looks like that sdk is being properly selected so I'm not sure.

@hamishknight
Copy link
Contributor

Those are the same failures as on main, so yeah they're unrelated

@hamishknight
Copy link
Contributor

@swift-ci please test source compatibility Debug

@hamishknight
Copy link
Contributor

Source compat failures unrelated

@hamishknight hamishknight merged commit cc14548 into swiftlang:main Mar 6, 2025
5 of 7 checks passed
@jameesbrown jameesbrown deleted the bool-literal-case branch March 6, 2025 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optional boolean doesn't satisfy switch exhaustivity with literal case statements
3 participants