Swift: Compiler error in nested call to generic function

Originator:google
Number:rdar://17239003 Date Originated:09-Jun-2014 11:19 PM
Status:Open Resolved:
Product:Developer Tools Product Version:
Classification: Reproducible:
 
Summary:
Unexpected compiler error occurs with the attached code:
'Int' is not convertible to 'T.IndexType.DistanceType'

Steps to Reproduce:
1. Create a new Swift command line app.
2. Replace “main.swift” with the included file.
3. Build & Run.


Expected Results:
Compiles and works correctly.

Actual Results:
Instead, this will result in a compile-time error. The specific error is:
Compiler Error:
'Int' is not convertible to 'T.IndexType.DistanceType'



Version:
Xcode 6.0 (6A215l)

Notes:
I found that you can work around this my adding the explicit cast. Just remove the // to enable the workaround. 

The code could be more idiomatic. In fact I have rewritten it to be so that it produces and returns a Range<T.IndexType>. Same error, but the workaround doesn’t work. Code available upon request.

Configuration:
OS X 10.9.3


/**
* Determine the common prefix elements of two collections.
* @param collection1 First collection.
* @param collection2 Second collection.
* @return The number of elements common to the start of each collection.
*/
func commonPrefixLength<T: Swift.Collection, U: Swift.Collection where
	T.GeneratorType.Element: Equatable,
	T.GeneratorType.Element == U.GeneratorType.Element,
	T.IndexType : ForwardIndex,
	U.IndexType : ForwardIndex>
	(collection1: T, collection2: U) -> T.IndexType.DistanceType {
		var collection2generator = collection2.generate()
		
		var i: T.IndexType.DistanceType = 0
		
		for element1 in collection1 {
			let element2 = collection2generator.next()
			
			if (element1 != element2) {
				return i
			}
			
			i++
		}
		
		return i
}
println(commonPrefixLength("abX", "abc"))

/**
* Determine the common suffix elements of two collections.
* @param collection1 First collection.
* @param collection2 Second collection.
* @return The number of elements common to the end of each collection.
*/
func commonSuffixLength<T: Swift.Collection, U: Swift.Collection where
	T.GeneratorType.Element: Equatable,
	T.GeneratorType.Element == U.GeneratorType.Element,
	T.IndexType : BidirectionalIndex,
	U.IndexType : BidirectionalIndex>
	(collection1: T, collection2: U) -> T.IndexType.DistanceType {
		return commonPrefixLength(reverse(collection1), reverse(collection2))// as T.IndexType.DistanceType // Adding this explicit casting hack makes it compile and work correctly.
}
println(commonSuffixLength("Xabc", "abc"))

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!