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-2948] Discarding a closure from a @discardableResult function results in a compile error. #45542

Closed
swift-ci opened this issue Oct 14, 2016 · 8 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself good first issue Good for newcomers

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-2948
Radar None
Original Reporter Amnykon (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, StarterBug
Assignee None
Priority Medium

md5: 021904a7cf17206d058f15ce5ed4b66f

relates to:

  • SR-7297 Discarding a function from a discardableResult method causes a compiler error

Issue Description:

A function marked with @discardableResult should not throw an error if it is returning closure is unused.

The current work around is to set _ to the returning closure.

@discardableResult func run (_ closure: @escaping ()->()) -> (()->()) {
  closure()
  return closure
}

var closure={print(1)}
_ = run(closure)
// Prints 1

_ = run(run(closure))
// Prints 1
// Prints 1

run(closure)
// should Print 1 but errors with:
//   discardableResultOfClosure.swift:14:1: error: expression resolves to an unused function
//   run(closure)
//   ^~~~~~~~~~~~
@swift-ci
Copy link
Collaborator Author

Comment by Erik Verbruggen (JIRA)

Matthew: Oh, sorry, yes: #6785

Please do comment on that patch! 🙂

@swift-ci
Copy link
Collaborator Author

Comment by Erik Verbruggen (JIRA)

@belkadan should I also close this issue now that the pull-request has merged?

@belkadan
Copy link
Contributor

Usually "resolved" means the implementer thinks it's done and "closed" means the reporter thinks it's done. So we can leave it here for Micah to verify or not.

@hamishknight
Copy link
Collaborator

Found a variant of this bug with methods: https://bugs.swift.org/browse/SR-7297

@swift-ci
Copy link
Collaborator Author

Comment by Rafael Gutierrez (JIRA)

I'm still seeing this issue happen for swift 4.2.

@belkadan
Copy link
Contributor

You sure you're not seeing SR-7297?

@swift-ci
Copy link
Collaborator Author

Comment by Rafael Gutierrez (JIRA)

In reality @belkadan it might be a different issue altogether. I have the following code:

@discardableResult func testFunc() -> () -> Void {
    return {
        print("yehaw")
    }
}

//call function
testFunc() // Expression resolves to an unused function

Let me know if you think this issue is unrelated to this one and I can file a separate issue. It does not seem related to SR-7297 .

@belkadan
Copy link
Contributor

That's not producing any message for me when using the Swift that comes with Xcode 10.0 (from the command line). How are you compiling this?

@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 good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants