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

@discardableResult works with try and try!, but not try? #71870

Open
AnthonyLatsis opened this issue Feb 24, 2024 · 2 comments · May be fixed by #72380
Open

@discardableResult works with try and try!, but not try? #71870

AnthonyLatsis opened this issue Feb 24, 2024 · 2 comments · May be fixed by #72380
Labels
attributes Feature: Declaration and type attributes bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself @discardableResult Feature → attributes: The @discardableResult attribute error handling expressions Feature: expressions swift 6.0 type checker Area → compiler: Semantic analysis unexpected warning Bug: Unexpected warning

Comments

@AnthonyLatsis
Copy link
Collaborator

Description

No response

Reproduction

@discardableResult
func foo() throws -> Int {}

func test() {
  try? foo() // warning: result of 'try?' is unused [expression_unused_optional_try]
}

Expected behavior

No warning

Environment

Swift version 5.11-dev (Swift 11ef6e5)

Additional information

No response

@AnthonyLatsis AnthonyLatsis added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself type checker Area → compiler: Semantic analysis error handling attributes Feature: Declaration and type attributes @discardableResult Feature → attributes: The @discardableResult attribute expressions Feature: expressions unexpected warning Bug: Unexpected warning swift 6.0 labels Feb 24, 2024
@li3zhen1
Copy link
Contributor

li3zhen1 commented Mar 8, 2024

Hi, I think I can fix this. but in lib/Sema/TypeCheckStmt.cpp there was a commit from 9 years ago:

Always warn when 'try?' is discarded.
rdar://problem/22195906
Swift SVN r31090

(as as? as! are all treated as non-discardable, and I feel like it's reasonable if try? behaves like them.)

I also found another corner case with discardable computed member reference, not sure if it's a expected behavior:

struct S {
    var s: S {
        @discardableResult
        get {
            return self
        }
    }
}
24 
25  func test() {
26      S().s
            ╰─ warning: expression of type 'S' is unused
27  }
28 

@AnthonyLatsis
Copy link
Collaborator Author

AnthonyLatsis commented Apr 1, 2024

there was a commit from 9 years ago:

Always warn when 'try?' is discarded.
rdar://problem/22195906
Swift SVN r31090

Apparently, that was supposed to mean "warn even if the call returns Void", perhaps to encourage error handling or explicit discarding. @discardableResult should override these rules.

I also found another corner case with discardable computed member reference, not sure if it's a expected behavior

Is that even a reasonable thing to do? Either way the current behavior is a bug. Could you report it?


If you made up your mind to work on an issue (not just take it for maybe later), please give us a clear cue and ask us — you can ask me — to assign you. You can always hand it back if plans change on your side. Otherwise, there is really nothing wrong with someone else showing up and making progress faster, which we know is demotivating.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
attributes Feature: Declaration and type attributes bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself @discardableResult Feature → attributes: The @discardableResult attribute error handling expressions Feature: expressions swift 6.0 type checker Area → compiler: Semantic analysis unexpected warning Bug: Unexpected warning
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants