Xcode-beta (7C62b) / Swift 2.1.1: Misleading diagnostic when incomplete generics are used in Array/Dictionary shorthand syntax with initializers
| Originator: | Karoly.Lorentey | ||
| Number: | rdar://23722205 | Date Originated: | 02-Dec-2015 12:39 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode-beta (7C62b) |
| Classification: | Other Bug | Reproducible: | Always |
Summary:
The Swift compiler produces an incorrect diagnostic message when an incompletely specified generic type is used in an Array or Dictionary declaration using the shorthand notation combined with an initializer expression.
1) var values: [Optional] = []
2) var values: [String: Optional] = [:]
3) var values: [Optional]
4) var values: [String: Optional]
5) var values: Array<Optional> = []
6) var values: Dictionary<Optional> = [:]
Lines 1 and 2 produce misleading error diagnostics of the form “cannot convert value of type ’[_]’ to specified type ’[Optional]’“, pointing to a problem with the initializer.
Lines 3 to 6 produce awesome diagnostics of the form “reference to generic type ’Optional’ requires arguments in <…>”, pointing to the incomplete Optional type, as expected.
As you can see, both the use of the shorthand syntax and an initializer is necessary to reproduce the problem.
Steps to Reproduce:
$ env DEVELOPER_DIR=/Applications/Xcode-beta.app xcrun swift
Welcome to Apple Swift version 2.1.1 (swiftlang-700.1.101.13 clang-700.1.81). Type :help for assistance.
1> var values: [Optional] = []
repl.swift:1:26: error: cannot convert value of type '[_]' to specified type '[Optional]'
var values: [Optional] = []
^~
1> var values: [String: Optional] = [:]
repl.swift:1:34: error: cannot convert value of type '[_ : _]' to specified type '[String : Optional]'
var values: [String: Optional] = [:]
^~~
1> var values: [Optional]
repl.swift:1:14: error: reference to generic type 'Optional' requires arguments in <...>
var values: [Optional]
^
generic type 'Optional' declared here
1> var values: [String: Optional]
repl.swift:1:22: error: reference to generic type 'Optional' requires arguments in <...>
var values: [String: Optional]
^
generic type 'Optional' declared here
1> var values: Array<Optional> = []
repl.swift:1:19: error: reference to generic type 'Optional' requires arguments in <...>
var values: Array<Optional> = []
^
generic type 'Optional' declared here
1> var values: Dictionary<String: Optional> = [:]
repl.swift:1:30: error: expected '>' to complete generic argument list
var values: Dictionary<String: Optional> = [:]
^
repl.swift:1:23: note: to match this opening '<'
var values: Dictionary<String: Optional> = [:]
^
Expected Results:
The compiler complains about the unspecified type parameter in the array type.
$ xcrun swift < test.swift
repl.swift:6:15: error: reference to generic type ’Result’ requires arguments in <…>
var results: [Result] = []
^~
repl.swift:1:6: note: generic type 'Result' declared here
enum Result<Value> {
^
Actual Results:
The compiler complains about the initializer.
$ xcrun swift < test.swift
repl.swift:6:25: error: cannot convert value of type '[_]' to specified type '[Result]'
var results: [Result] = []
^~
Regression:
Unknown
Notes:
Expanding the shorthand notation to Array[Result] results in the correct diagnostic:
repl.swift:6:20: error: reference to generic type 'Result' requires arguments in <...>
var results: Array<Result> = []
^
repl.swift:1:6: note: generic type 'Result' declared here
enum Result<Value> {
^
env DEVELOPER_DIR=/Applications/Xcode-beta.app xcrun swift
Welcome to Apple Swift version 2.1.1 (swiftlang-700.1.101.13 clang-700.1.81). Type :help for assistance.
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!