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-13811] Error not diagnosed in inactive clause #56208

Closed
swift-ci opened this issue Nov 3, 2020 · 3 comments
Closed

[SR-13811] Error not diagnosed in inactive clause #56208

swift-ci opened this issue Nov 3, 2020 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Nov 3, 2020

Previous ID SR-13811
Radar rdar://problem/70999140
Original Reporter maustinstar (JIRA User)
Type Bug
Status Closed
Resolution Invalid
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 4852cfd70cb40642db091fad21e232e7

Issue Description:

Using an if declaration within a function body that expects a return value, the following code compiles with no warnings or errors. This is because the active clause is only examined for a return statement.

var a: Int = {
  #if true
  return 0
  #else
  let x = 1 + 2
  x
  #endif
}

Switching the flag:

var a: Int = {
 #if false
 return 0
 #else
 let x = 1 + 2
 x
 #endif
} 

You'll get an error missing a return statement.

This should be diagnosed without having to switch the flag.

@typesanitizer
Copy link

@swift-ci create

@beccadax
Copy link
Contributor

beccadax commented Nov 4, 2020

Hi maustinstar (JIRA User),

This is expected behavior:

  • Swift requires the code in inactive branches (that don't check language versions) to parse, but not to typecheck. After all, people often use #if to adjust their code to differences elsewhere that can affect the types of declarations.

  • We can't detect missing {{return}}s in code that hasn't been typechecked because that analysis depends on the types in the code. For instance, if you call a function that returns Never like fatalError(), it's not an error to have no return statement. We wouldn't want to diagnose an error in an inactive branch that won't actually happen when the branch is active.

  • Therefore, we do not attempt to diagnose missing return statements or other control-flow-sensitive errors (like uses of uninitialized variables) in inactive branches.

Since Swift is working as designed here, I'm going to resolve this bug as "Invalid". (Maybe not the best terminology, but it's the term we have.) If you think I've made a mistake, please reopen this report; otherwise please close it.

Thanks for your report!

@swift-ci
Copy link
Collaborator Author

swift-ci commented Nov 4, 2020

Comment by Michael Verges (JIRA)

Thanks for the explanation!

@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

3 participants