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-8213] Compiler may indicate "case will never be executed" even if it is unavoidable to execute the case in switch statement. #50745

Open
YOCKOW opened this issue Jul 10, 2018 · 14 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers pattern matching Feature: pattern matching patterns Feature: patterns SILGen Area → compiler: The SIL generation stage statements Feature: statements swift 5.9 switch cases Feature → statements → switch: 'switch' statement cases switch Feature → statements: 'switch' statements type checker Area → compiler: Semantic analysis

Comments

@YOCKOW
Copy link
Collaborator

YOCKOW commented Jul 10, 2018

Previous ID SR-8213
Radar None
Original Reporter @YOCKOW
Type Bug
Environment
  • OS: macOS, Ubuntu 16.04

  • Swift: 4.1.2, 4.2-DEVELOPMENT-SNAPSHOT-2018-07-08

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, StarterBug
Assignee Gabriel Igliozzi (JIRA)
Priority Medium

md5: df5f44ab5b8a1d3c8ee6ed4a803d858c

is duplicated by:

  • SR-11318 Incorrect "Case will never be executed" warning

Issue Description:

(As I commented on SR-8208,)
The compiler shows a wrong warning, when the following code is compiled:

enum E {
  case a // only one case
}

func f(_ e0:E, _ e1:E) {
  switch (e0, e1) {
  case (.a, _): print("e0 is .a") // -> No warning.
  }
  
  switch (e0, e1) {
  case (.a, _), (_, .a): print("e0 or e1 is .a") // -> warning: case will never be executed
  }
}

f(.a, .a)

Consideration

  • The warning should be
    • case will be always executed
    • (_, .a) will never match
    • ... and so on
@belkadan
Copy link
Contributor

@xedin, think this one's easy enough to be a StarterBug? You'd have to do some checking about whether a pattern was the only one in the case…

@xedin
Copy link
Member

xedin commented Jul 12, 2018

I think it makes sense to mark it as a StarterBug, it seems like that could be checked when cases are projected...

@vguerra
Copy link
Contributor

vguerra commented Aug 1, 2018

hello @YOCKOW, @belkadan, @xedin 🙂

If none of you is working on this, I would like to tackle it.

@xedin
Copy link
Member

xedin commented Aug 1, 2018

Sure, go ahead, @vguerra!

@belkadan
Copy link
Contributor

Resetting assignee for all Starter Bugs not touched since 2018.

@LucianoPAlmeida
Copy link
Collaborator

@belkadan @xedin There is someone working on this one? If not, I could give it a try

@swift-ci
Copy link
Collaborator

Comment by Gabriel Igliozzi (JIRA)

@xedin @belkadan If no-one is working on this bug I would like to give it a go as my first contribution

@xedin
Copy link
Member

xedin commented Aug 10, 2020

Maybe @LucianoPAlmeida is working on this, otherwise please feel free to take it over!

@LucianoPAlmeida
Copy link
Collaborator

I'm not working on this so go-ahead Gabriel Igliozzi (JIRA User) 🙂
But I think since this is linked as duplicated by an issue that is marked as resolved already, chances are that this is fixed as well.
So maybe the first step is to verify is the issue is still reproducible on the master branch

@swift-ci
Copy link
Collaborator

swift-ci commented Aug 11, 2020

Comment by Gabriel Igliozzi (JIRA)

Hey @LucianoPAlmeida thanks for the response, I just did an pull on the master branch, compiled and ran that test code (with the swiftc command) and got

test.swift:11:3: warning: case will never be executed
  case (.a, _), (_, .a): print("e0 or e1 is .a") // -> warning: case will never be executed
  ^

Is this a valid way to test the master branch?, if so I believe the error is not yet resolved.

@LucianoPAlmeida
Copy link
Collaborator

Seems like the problem is still here, normally a quick and easy way to test if an issue is reproducible on master is with https://swift.godbolt.org there you can find the latest nightly snapshot from the master, but of course, you can download the lastest snapshot or build master locally and create a new file with the code as a test case and run it using lit 🙂

@jreference
Copy link

Should this issue be closed? In the current version of swift, no warnings are produced. Added a test case in #65819 to verify.

@AnthonyLatsis AnthonyLatsis added swift 5.3 SIL pattern matching Feature: pattern matching switch Feature → statements: 'switch' statements statements Feature: statements switch cases Feature → statements → switch: 'switch' statement cases missing warning Bug: Missing warning unexpected warning Bug: Unexpected warning patterns Feature: patterns SILGen Area → compiler: The SIL generation stage and removed missing warning Bug: Missing warning SIL labels May 10, 2023
@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented May 10, 2023

The warning itself was not a bug. It means that the second pattern will never be matched because a previous one always matches, which is true for case (.a, _), (_, .a):. The warning was silenced in #33336, but the issue is still relevant because we ought to diagnose the redundancy in the type checker (as in e.g. case .a, .a:) if we choose not to diagnose it further down the line.

@jreference
Copy link

@AnthonyLatsis Thank you for explaining!

@AnthonyLatsis AnthonyLatsis removed the unexpected warning Bug: Unexpected warning label May 11, 2023
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 diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers pattern matching Feature: pattern matching patterns Feature: patterns SILGen Area → compiler: The SIL generation stage statements Feature: statements swift 5.9 switch cases Feature → statements → switch: 'switch' statement cases switch Feature → statements: 'switch' statements type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

8 participants