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-6915] Array += is stricter when adding arrays of conformers to arrays of protocols #49464

Closed
ZevEisenberg opened this issue Feb 3, 2018 · 6 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 4.1 type checker Area → compiler: Semantic analysis

Comments

@ZevEisenberg
Copy link
Contributor

Previous ID SR-6915
Radar rdar://problem/37290898
Original Reporter @ZevEisenberg
Type Bug
Status Resolved
Resolution Done
Environment
$ swift --version
Apple Swift version 4.1 (swiftlang-902.0.34 clang-902.0.30)
Target: x86_64-apple-darwin17.4.0
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 4.1Regression, TypeChecker
Assignee None
Priority Medium

md5: 25774d7409bb46b73b10c3ab18e06880

Issue Description:

The following code has one expected error in Xcode 9.2, but it has a new error in Xcode 9.3. It also occurs if you use the newly renamed compactMap instead of flatMap. Both errors can be worked around with `as [MyProtocol]`, but I wasn't sure if it was expected that the second case would be an error in Swift 4.1.

This was reported as a bug on my open source repo here: Rightpoint/BonMot#320

protocol MyProtocol {}

struct ConformingStruct: MyProtocol {}

var arrayOfMyProtocol: [MyProtocol] = []

arrayOfMyProtocol += [ConformingStruct()] // no error

let arrayOfConformingStruct = [ConformingStruct()]
arrayOfMyProtocol += arrayOfConformingStruct // EXPECTED Xcode 9.2 and 9.3 error: Binary operator '+=(_:_:)' cannot be applied to operands of type '[MyProtocol]' and '[ConformingStruct]'

var optionalStruct: ConformingStruct?

arrayOfMyProtocol += [optionalStruct].flatMap { $0 } // NEW Xcode 9.3 ONLY error: Binary operator '+=(_:_:)' cannot be applied to operands of type '[MyProtocol]' and '[ConformingStruct]'
@belkadan
Copy link
Contributor

belkadan commented Feb 5, 2018

Hm. What you really want there is just map, not compactMap (previously flatMap). But since it's a regression it might indicate a bigger problem.

cc @rudkx

@rudkx
Copy link
Member

rudkx commented Feb 5, 2018

I see the same error for the second line when compiling with Xcode 9.2. Are you sure there is a change in behavior from that version?

Actually, I used DEVELOPER_DIR to point to 9.2, but then explicitly used my locally built compiler, so yes, I see a different behavior here as well.

@rudkx
Copy link
Member

rudkx commented Feb 5, 2018

@swift-ci create

@ZevEisenberg
Copy link
Contributor Author

@belkadan I don't think I do want map there. I'm trying to flatten an a [T?] to a [T] with the nils removed. Good point about compactMap, but I left it as flatMap because, as you say, it is a regression, so I wanted to keep the reduced test case as close to real-world usage as possible.

I wanted to add this library to the compatibility repo, but I had a little trouble getting Swift PM set up. Maybe I'll take another crack at it soon, because I'm guessing it would have caught this sooner.

@belkadan
Copy link
Contributor

belkadan commented Feb 5, 2018

Oh, I'm sorry, I did in fact miss the optional. Yes, it seems like there could be enough type context here to make things work.

@ZevEisenberg
Copy link
Contributor Author

Looks like this is fixed in 9.4.1

@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 regression swift 4.1 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants