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-12278] "Cannot match several associated values at once" should explain itself or be removed #54706

Closed
rnapier opened this issue Feb 26, 2020 · 4 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@rnapier
Copy link

rnapier commented Feb 26, 2020

Previous ID SR-12278
Radar rdar://problem/56436226
Original Reporter @rnapier
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @typesanitizer
Priority Medium

md5: 04f75c69b330cde1c9919a42bdfde1ca

Issue Description:

In Swift 5.2 (Xcode 11.4), there appears to be a new warning:

"Cannot match several associated values at once, implicitly tupling the associated values and trying to match that instead."

This is the behavior I want, and it's unclear what the problem is. Swift is doing exactly the right thing, but warning about it without indicating what problem it's fixing or how I should resolve the issue.

Specifically I have a line of code:

case let (.loading(lhs), .loading(rhs)): return lhs == rhs

Where .loading is defined as

case loading(mediaList: MediaList, startingTrack: Int, autoPlay: Bool)

The warning seems to suggest this is the correct way to write it:

case let (.loading(mediaList: lhsMediaList, startingTrack: lhsStartingTrack, autoPlay: lhsAutoPlay),
          .loading(mediaList: rhsMediaList, startingTrack: rhsStartingTrack, autoPlay: rhsAutoPlay)):
    return (lhsMediaList, lhsStartingTrack, lhsAutoPlay) == (rhsMediaList, rhsStartingTrack, rhsAutoPlay)

Given that the default behavior seems to be exactly as desired, this warning needs to explain why I should write so much highly-fragile code instead, or the warning should be removed and the this behavior codified as correct.

@rnapier
Copy link
Author

rnapier commented Feb 26, 2020

This might be related to SR-11212, but reading the defect, I still don't understand the problem this warning is trying to prevent. (But it's not clear this is the same issue, since the whole point here is that I'm not splatting the tuple.)

@typesanitizer
Copy link

Sorry about the poor diagnostic.

That said, this warning has been improved on master. a98d20f

If I run this example with the master compiler, it gives me the following diagnostic:

Tmp.swift:7:24: warning: enum case 'loading' has 3 associated values; matching them as a tuple is deprecated
    case let (.loading(lhs), .loading(rhs)):
                       ^
Tmp.swift:2:8: note: 'loading(mediaList:startingTrack:autoPlay:)' declared here
  case loading(mediaList: Int, startingTrack: Int, autoPlay: Int)
       ^
Tmp.swift:7:39: warning: enum case 'loading' has 3 associated values; matching them as a tuple is deprecated
    case let (.loading(lhs), .loading(rhs)):
                                      ^
Tmp.swift:2:8: note: 'loading(mediaList:startingTrack:autoPlay:)' declared here
  case loading(mediaList: Int, startingTrack: Int, autoPlay: Int)

> This might be related to SR-11212

Yes it is related.

> I still don't understand the problem this warning is trying to prevent.

The problem with the old behavior is that (1) it cannot be made to work consistently in the presence of nested patterns (a user ran into this in SR-11160) and (2) it is an oversight in SE-0110, patterns should behave analogous to function arguments – there is a difference between an enum case which has 1 associated value which is a tuple of N elements and an enum case which has N associated values.

> Given that the default behavior seems to be exactly as desired, this warning needs to explain why I should write so much highly-fragile code instead

I agree that for this particular case, the behavior is convenient and there is not much that can go wrong.

However in the general case, the old behavior makes it unpredictable what should/should not work. The table in SR-11212 shows four cases where the compiler's behavior was not what one would predict before the fix.

@typesanitizer
Copy link

Marking this as resolved for now (since we have improved the warning), but please feel free to unresolve it if you feel like something is unclear or you feel that this should not be a warning (in the latter case, maybe we can have a discussion on the Swift forum or somewhere else).

@typesanitizer
Copy link

Fixed on master.

@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