Skip to content

[SR-2948] Discarding a closure from a @discardableResult function results in a compile error. #45542

Closed
@swift-ci

Description

@swift-ci
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)
//   ^~~~~~~~~~~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfgood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions