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-2336] Ternary operator with optionals results in lousy diagnostic #44943

Open
iby opened this issue Aug 14, 2016 · 4 comments
Open

[SR-2336] Ternary operator with optionals results in lousy diagnostic #44943

iby opened this issue Aug 14, 2016 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis

Comments

@iby
Copy link

iby commented Aug 14, 2016

Previous ID SR-2336
Radar None
Original Reporter @iby
Type Bug
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, DiagnosticsQoI, TypeChecker
Assignee None
Priority Medium

md5: 1aef8ca43f09236c0d2778ed7d4634ea

is duplicated by:

  • SR-2372 Using ternary operator breaks initialization of an array of optional tuples in some cases

Issue Description:

@belkadan
Copy link
Contributor

Seems to work in Swift 3, although the error is completely bogus. Which Swift were you using?

<stdin>:5:30: error: cannot subscript a value of type 'inout [AnyObject]' (aka 'inout Array<AnyObject>')
    bar = ["key": i < 2 ? foo[i] : nil]
                          ~~~^

@swift-ci
Copy link
Collaborator

Comment by Abdullah Abanmi (JIRA)

The types in ternary operator have to match: ( foo: AnyObject? == bar[string]: AnyObject?)

var foo: [AnyObject?] = []
var bar: [String:AnyObject?] = [:]

for i in 0 ..< 5 {
    foo.append(i as AnyObject?)
    bar = ["key": (i < 2 ? foo[i] : nil)]
}

tested in xcode 8b6 w/ swift 3

@swift-ci
Copy link
Collaborator

Comment by Corey Watson (JIRA)

I'm getting this problem too, though with an error more similar to [SR-2372]. Here's my code that fails with the error: 'Int1' is not convertible to 'Bool'

let indexes: [(row: Int, section: Int)] = [(0, 0), (0, 1), (0, 2)]
let sectionToMatch: Int = 2

let indexOfSectionToMatch = indexes.reduce(nil) { $0?.section == sectionToMatch ? $0 : $1 }

However, when I explicitly specify variable names and a return type, the error disappears and the code compiles fine. Here's my code that works:

let indexes: [(row: Int, section: Int)] = [(0, 0), (0, 1), (0, 2)]
let sectionToMatch: Int = 2

let indexOfSectionToMatch = indexes.reduce(nil) { (index, nextIndex) -> (row: Int, section: Int)? in
    index?.section == sectionToMatch ? index : nextIndex
}

@Dante-Broggi
Copy link
Contributor

What should happen with this report?

@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 diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants