Swift 2: Protocol extension method can't use protocol method call as default parameter value

Originator:brent
Number:rdar://21500900 Date Originated:23-Jun-2015 02:35 AM
Status:Closed Resolved:25-Jun-2015 06:08 PM
Product:Developer Tools Product Version:Xcode-beta (7A120f)
Classification:Feature (New) Reproducible:Always
 
Summary:
When adding a method to a protocol using a protocol extension, swiftc segfaults if the method has a parameter with a default value, and that default value is calculated by calling another method in the protocol.

Steps to Reproduce:
1. Write this:

public extension SequenceType {
    mutating func read(estimatedSize size: Int = underestimateCount()) -> [Generator.Element] {
        var array: [Generator.Element] = []
        array.reserveCapacity(size)
        array.extend(self)
        return array
    }
}

2. Compile it

Expected Results:
The code compiles correctly.

Actual Results:
Segmentation fault with output like:

1.	While type-checking 'read' at [omitted]
2.	While type-checking expression at [omitted] RangeText="underestimateCount()"

The full text of this error is attached, although the protocol extension was in this case embedded in a larger file within a project.

Regression:
New feature in Swift 2.

Notes:
This is easily worked around by making the parameter’s type optional, setting a default value of nil, and ??’ing to the default value you really wanted. That’s not how this feature is supposed to work, though!





Engineering has provided the following feedback regarding this issue:

We correctly reject this now:

t.swift:2:50: error: cannot invoke 'underestimateCount' with no arguments
    mutating func read(estimatedSize size: Int = underestimateCount()) -> [Generator.Element] {
                                                 ^

because 'self' is not available within the default argument of a method. This appears fixed in Xcode 7 beta 2.

Please let us know whether the issue is resolved for you by updating your bug report.

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!