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-7999] Adding optional arrays produces unexpected result #50532

Open
swift-ci opened this issue Jun 14, 2018 · 3 comments
Open

[SR-7999] Adding optional arrays produces unexpected result #50532

swift-ci opened this issue Jun 14, 2018 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-7999
Radar None
Original Reporter mark (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: e7443c8cd248c361249a2a21b6079fac

Issue Description:

Tested on Xcode 10 beta 1/Swift 4.2

If you have two optional arrays and add their contents into a new result variable, you might lose the contents of the right-hand array if you don't properly wrap both arrays in parenthesis when nil coalescing.

It's unclear if this is a compiler bug, or defined behavior if you know what's going on, but the results of this are unexpected so I'm considering it a bug.

Observe the following reproducible code snippet:

let array1: [Int]? = [1, 2]
let array2: [Int]? = [3]

let result1 = array1 ?? [] + (array2 ?? [])
result1
// products: [1, 2] // array2's contents are dropped

let result2 = (array1 ?? []) + (array2 ?? [])
result2
// products: [1, 2, 3]

The first concatenation compiles, as you have to use at least one set of parenthesis, however this produces an unexpected result as it completely drops the contents in array2. The second concatenation compiles and produces the expected result by concatenating the contents of both arrays.

@swift-ci
Copy link
Collaborator Author

Comment by Mark Sands (JIRA)

I’m surprised I never realized the nil colesceing operator doesn’t have higher precedence than the addition operator. I don’t see that changing in Swift so perhaps improved warnings and other compiler diagnostics might help avoid this subtle bug?

@belkadan
Copy link
Contributor

@rudkx, @xedin, @rjmccall, do we have any other bugs about precedence warnings?

@rjmccall
Copy link
Member

I don't remember any other bugs here.

I agree that ?? feels like it ought to be higher precedence; either that or it shouldn't associate with anything else.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants