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-8208] Compiler doesn't indicate a warning about switch-statement even if default will never be executed. #50740

Open
YOCKOW opened this issue Jul 9, 2018 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@YOCKOW
Copy link
Collaborator

YOCKOW commented Jul 9, 2018

Previous ID SR-8208
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
Assignee None
Priority Medium

md5: 9d87fa186411d1838211549f925233c3

Issue Description:

Although `default` will never be executed in the second switch statement in the following code, the compiler shows only a warning about the first switch statement.

[Code]

enum E {
  case a,b
}

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

f(.a, .a)
f(.a, .b)
f(.b, .a)
f(.b, .b)
@YOCKOW
Copy link
Collaborator Author

YOCKOW commented Jul 9, 2018

On the other hand, as shown below, the compiler indicates a wrong error "case will never be executed" even if it must be executed.

enum E {
  case a // only one case
}

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

f(.a, .a)

@belkadan
Copy link
Contributor

belkadan commented Jul 9, 2018

Ah, that second issue is trying to say the second pattern will never match, not the entire case. Can you file a separate bug to clean up that diagnostic?

CodaFi (JIRA User), @xedin, what do you think about this one? Is this supposed to be caught in the Space Engine or in SIL?

@YOCKOW
Copy link
Collaborator Author

YOCKOW commented Jul 10, 2018

@belkadan

Thank you for the advice.

I have posted it as SR-8213.

@xedin
Copy link
Member

xedin commented Jul 12, 2018

I think Space Engine should be able to determine that second pattern would never match.

@CodaFi
Copy link
Member

CodaFi commented Dec 18, 2018

I disagree. If we turned on optimal pattern dispatch in SILGen we could catch this.

Take this code:

func f(_ e0:E, _ e1:E) -> Int {
  switch (e0, e1) {
  case (.a, _): return 11
  case (_, .a): return 22
  case (.b, _): return 33
  default: return 44
  }
}

SIL is essentially emitting this for the switch

if e0 == .a {
  return 11
} else {
  if e1 == .a {
    return 22
  } else {
    if e0 == .b {
      return 33
    } else {
      return 44
    }
  }
}

When it should be emitting this:

if e0 == .a {
  return 11
} else {
  if e1 == .a {
    return 22
  } else {
    return 33
  } /* else { return 44 } <- dead */
}

But we don't specialize inside tuple patterns.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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