Skip to content
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

[SR-7907] The compiler is unable to check that this is exhaustive in reasonable time #50442

Closed
drewcrawford opened this issue Jun 7, 2018 · 6 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@drewcrawford
Copy link
Contributor

Previous ID SR-7907
Radar None
Original Reporter @drewcrawford
Type Bug
Status Resolved
Resolution Done
Environment

Xcode Version 10.0 beta (10L176w)

Xcode toolchain

OSX 10.13.5 (17F77)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 93dafc41578786c6f6c4391116f23f0f

Issue Description:

Extracted from a Network.framework playground. This is a simple enum, why can't we check it in reasonable time?

import Network
let s = NWConnection.State.ready
switch s {
case .failed(let err):break
case .cancelled: break
case .preparing, .setup: break
case .waiting(let err):break
case .ready: break
//default:
//    preconditionFailure("The compiler is unable to check that this is exhaustive in reasonable time")
}
@swift-ci
Copy link
Collaborator

swift-ci commented Jun 7, 2018

Comment by Ding Ye (JIRA)

It is actually not as simple as it appears. Two occurrences of "NWError" is the culprit.

NWConnection.State takes 223 spaces, where either of ".waiting(: NWError)" and ".failed(: NWError)" occupies 109 spaces. This is because the subcase ".posix(_: POSIXErrorCode)" takes 106 spaces.

Such a large space size exceeds the MAX_SPACE_SIZE (i.e., 128), and consequently triggers "diagnoseCannotCheck()".

@drewcrawford
Copy link
Contributor Author

It's not immediately clear to me why the associated values are entering into exhaustion checking for a switch with no where clauses. All we do with the associated variables is bind them to a variable, all we need to do is check that each case appears, there should be a fast path for this scenario

Separately I think this enum is poorly designed for the application, these two cases can be combined and I will be lobbying the author to do so. IMO we should support bad designs though

@belkadan
Copy link
Contributor

belkadan commented Jun 8, 2018

Nice analysis, Ding. And Drew is right: estimating the full space is overkill when most people probably won't match into the payload. cc CodaFi (JIRA User), @xedin, me

@allevato
Copy link
Collaborator

We've run into a similar issue in swift-protobuf against the Swift 5.1 compiler. Our conformance tests generate some enums with a pretty huge number of cases, so perhaps we're doing this to ourselves in a way, but this particular example compiled without error in Swift 5.0:

https://github.com/apple/swift-protobuf/blob/e4cb49ed5d10b387dd50d6ab387b6c45c9a74c5c/Tests/SwiftProtobufTests/generated_swift_names_enum_cases.pb.swift#L1749

That enum has 853 cases with no payloads and 1 case with an Int32 payload. However, with some local testing, it looks like we have the same issue if we remove the UNRECOGNIZED case and just have a simple enum with a large number of cases.

We can work around it by tweaking our generator to partition the switch into "buckets" each with a default clause, but we imagine that this will incur some codegen cost (at a minimum, a final default clause with a fatalError that the compiler can't determine is actually unreachable). The concern there is that we always pick the right "magic number" for the bucket size that will make the compiler happy from version to version.

Are there any other options here, aside from "don't generate huge enums"?

@belkadan
Copy link
Contributor

Since Drew's original case now works, it may be better to file a new bug here.

@allevato
Copy link
Collaborator

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

4 participants