Swift compiler mismatches generics in extension

Originator:rainerbrockerhoff
Number:rdar://17754431 Date Originated:21/7/2014
Status:Open Resolved:
Product:Xcode 6 Product Version:beta 4
Classification:Serious Bug Reproducible:Always
 
Summary:
A Dictionary extension with a where clause to restrict the input parameter to be a Sequence of tuples of the same (KeyType, ValueType) as that Dictionary itself.

The compiler matches against any sequence. The code produced for the extension correctly matches against KeyType only.

Steps to Reproduce:
Copy & paste this into Swift (xcrun swift or Xcode):

extension Dictionary {
	mutating func merge <E: Element, S: Sequence where S.GeneratorType.Element == Element> (seq: S) {
		var gen = seq.generate()
		while let n: E = gen.next() {
			self[n.0] = n.1
		}
	}
}
 
var dict = [0:0]
let array = [1.0,2.0,3.0]
dict.merge(array)
println(dict)
let more = [1,2,3]
dict.merge(more)
println(dict)
dict.merge(again)
println(dict)

Expected Results:
Should give a compiler error on both dict.merge() lines, since the Sequences GeneratorType.Elements are Int or Double, not Dictionary.Element (aka (KeyType,ValueType)).

Actual Results:
Runs with no error, prints out [0: 0, 1: 140735543552768, 2: 140735543552768, 3: 140735543552768]

Note that the first dict.merge() call does nothing.
Note garbage values for the second call.

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!