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-15281] flatMap - incorrect fixit with error #57603

Closed
swift-ci opened this issue Oct 5, 2021 · 3 comments
Closed

[SR-15281] flatMap - incorrect fixit with error #57603

swift-ci opened this issue Oct 5, 2021 · 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 Oct 5, 2021

Previous ID SR-15281
Radar None
Original Reporter maustinstar (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

MacOS 11.6

Swift version 5.5-dev

Xcode 13.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee maustinstar (JIRA)
Priority Medium

md5: 08f9b38fc783fb8ad25eb5408181fa0c

Issue Description:

Example code:

struct BadDiagnosis {
  var a: A? = A()
  var b: B {
    a.flatMap(B.init(a:))
    /// Cannot convert return expression of type 'B?' to return type 'B'
    /// fixit: Insert ' as! B'
  }
}

struct A { }

struct B {
  init(a: A) { }
}

Using the provided fixit:

struct BadDiagnosis {
  var a: A? = A()
  
  var b: B {
    a.flatMap(B.init(a:)) as! B
    /// Forced cast of 'B' to same type has no effect
    /// fixit: Remove 'as! B'
  }
}

struct A { }

struct B {
  init(a: A) { }
}

The fixits directly oppose each other.

The fixit should suggest to force unwrap the value:

a.flatMap(B.init(a:))!

or suggest making b an optional value:

var b: B?
@LucianoPAlmeida
Copy link
Collaborator

The correct warning for ` a.flatMap(B.init(a: )) as![]( Bshould be forced cast from 'B?' to 'B' only unwraps optionals; did you mean to use ')'?` with a fix-it for replace `as![]( Bwith)`. This is another mishandled edge case from migrating some conditional cast diagnostics from application to the fix format...
Also, I think we can do some extra work in the `cannot convert` error fix-it to be as you suggested, I'll take a look at this as soon as possible

@swift-ci
Copy link
Collaborator Author

swift-ci commented Oct 5, 2021

Comment by Michael Verges (JIRA)

Thanks for checking this out!

@LucianoPAlmeida
Copy link
Collaborator

Resolved #39648
maustinstar (JIRA User) Can you please verify using the next available snapshot from main and close?

@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

2 participants