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-2636] + Operator fails to infer type for more than 2 force unwrapped values #45241

Closed
swift-ci opened this issue Sep 14, 2016 · 5 comments
Closed
Assignees
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-2636
Radar None
Original Reporter wattson12 (JIRA User)
Type Bug
Status Closed
Resolution Duplicate

Attachment: Download

Environment

Xcode 8

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, TypeChecker
Assignee @rudkx
Priority Medium

md5: b79c6133c0059751b9920db443c6f49b

duplicates:

  • SR-1122 Failure to typecheck chain of binary operators on force-unwrapped values

is duplicated by:

  • SR-3134 force-unwrapping fails mysteriously when interleaved with + operators

Issue Description:

When summing values pulled (and force unwrapped) from a dictionary, Swift doesn't pick up the types when adding more than 2 values.

e.g. given

let menu: [String: Double] = ["pizza": 10.99, "ice cream": 4.99 , "salad": 7.99]

These are both fine:

let singleValue = menu["pizza"]!
let twoValueSum = menu["pizza"]! + menu["salad"]!

And this fails:

let threeValueSum = menu["pizza"]! + menu["salad"]! + menu["ice cream"]!
Playground execution failed: error: TestBetaPlayground.playground:7:36: error: ambiguous reference to member '+'
let threeValueSum = menu["pizza"]! + menu["salad"]! + menu["ice cream"]!

To give some extra info, all of these are valid

let threeValueSumFromTwoValueSum = twoValueSum + menu["ice cream"]!
print("The total cost of the meal = \(menu["pizza"]! + menu["salad"]! + menu["ice cream"]!)")
let sumWithAddedZero = menu["pizza"]! + menu["salad"]! + menu["ice cream"]! + 0
@swift-ci
Copy link
Collaborator Author

Comment by Richard Cook (JIRA)

I'm trying this in a playground. Version 8 (8A218a) and it shows the following compiler error:

var menu = ["coffee": 0.50, "pizza": 10.99, "ice cream": 4.99]

var totalCost = menu["coffee"]! + menu["pizza"]! + menu["ice cream"]!

print("The total cost of three items is \(totalCost)")

Playground execution failed: error: MyPlayground.playground:26:21: error: cannot subscript a value of type 'inout [String : Double]' (aka 'inout Dictionary<String, Double>')
var totalCost = menu["coffee"]! + menu["pizza"]! + menu["ice cream"]!

@swift-ci
Copy link
Collaborator Author

Comment by Sam Watts (JIRA)

awhig (JIRA User) Not sure if that is the same issue, but does seem related. If i change menu to a let, the error is the same as the one I mentioned above

When using var the totalCost line is fine adding only 2 values, but adding the third results in the subscript error you posted

@swift-ci
Copy link
Collaborator Author

Comment by Sam Watts (JIRA)

Also the same fix of adding + 0 somewhere in the totalCost line removes the subscript error

@swift-ci
Copy link
Collaborator Author

Comment by Steve B (JIRA)

May not help but this is another verbose workaround.

let cost = Double(menu["pizza"]!) + Double(menu["ice cream"]!) + Double(menu["salad"]!) // explicit cast to type works

This problem also occurs with other dictionary types. ie. [String: Int].

@swift-ci
Copy link
Collaborator Author

swift-ci commented Dec 9, 2016

Comment by David Friberg (JIRA)

Possible duplicate of SR-1122.

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

No branches or pull requests

1 participant