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-13516] Exhaustive switch not recognized as exhaustive #55952

Closed
dhoepfl opened this issue Sep 8, 2020 · 2 comments
Closed

[SR-13516] Exhaustive switch not recognized as exhaustive #55952

dhoepfl opened this issue Sep 8, 2020 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@dhoepfl
Copy link
Contributor

dhoepfl commented Sep 8, 2020

Previous ID SR-13516
Radar None
Original Reporter @dhoepfl
Type Bug
Status Resolved
Resolution Invalid
Environment

Swift version 5.3-dev (LLVM f8bd914, Swift 059344d)
Target: x86_64-unknown-linux-gnu

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

md5: d1348a4186b52b1c33e4e919bffe2ca6

duplicates:

  • SR-766 Switch doesn't need to cover negative Int cases

Issue Description:

The following code …

import Foundation
import CoreFoundation
func parseFrom(_ stream: InputStream) -> Bool {
   let _chunkSize = 512
   guard let buffer = malloc(_chunkSize)?.bindMemory(to: UInt8.self, capacity: _chunkSize) else { return false }
   defer { free(buffer) }

   stream.open()
   defer { stream.close() }

   switch stream.read(buffer, maxLength: _chunkSize) {
   case let len where len > 0:
      print("Result was > 0")
   case ..<0:
      print("Result was < 0")
   case 0:
      print("Result was = 0")
   }

   return true
}

results in:

> swift Sources/Bug/main.swift
Sources/Bug/main.swift:14:4: error: switch must be exhaustive
   switch stream.read(buffer, maxLength: _chunkSize) {
   ^
Sources/Bug/main.swift:14:4: note: do you want to add a default clause?
   switch stream.read(buffer, maxLength: _chunkSize) {
   ^
@typesanitizer
Copy link

I think we can improve the diagnostic to say "cannot be shown to be exhaustive" or something like that whenever we see things other than pure enum cases, but exhaustiveness checking works with enum cases, not with general functions like integer comparisons.

@typesanitizer
Copy link

Filed https://bugs.swift.org/browse/SR-13521 for improving the diagnostic.

@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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants