Mutating protocol func not registered as mutating
| Originator: | robnapier | ||
| Number: | rdar://21448659 | Date Originated: | 18-Jun-2015 05:06 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Swift 2beta1 |
| Classification: | Enhancement | Reproducible: | Not Applicable |
Given the following code, Swift issues warnings on copy() that “Parameter ‘src’ (and ‘dst’) was never mutated.” However, the function calls src.read and dst.write, which are both mutating methods. If ‘var’ is removed, the compiler correctly emits an error that “Immutable value of type ‘ReaderType’ only has mutating members called ‘read’” (and similarly for write). The warning and the error cannot both be correct. (The program appears to work despite the warning.)
protocol ReaderType {
mutating func read(buffer: UnsafeMutablePointer<UInt8>, maxLength: Int) throws -> Int
}
extension ReaderType {
mutating func read(inout buffer: [UInt8]) throws -> Int {
return try self.read(&buffer, maxLength: buffer.count)
}
}
protocol WriterType {
mutating func write(buffer: UnsafePointer<UInt8>, maxLength: Int) throws -> Int
}
enum WriterError: ErrorType {
case ShortWrite(Int)
}
func copy(var to dst: WriterType, var from src: ReaderType) throws -> Int {
var buf = [UInt8](count: 32*1024, repeatedValue: 0)
var written = 0
while true {
let nr = try src.read(&buf)
precondition(nr >= 0)
if nr > 0 {
let nw = try dst.write(buf, maxLength: nr)
precondition(nw >= 0)
if nw > 0 {
written += nw
}
guard nr == nw else { throw WriterError.ShortWrite(written) }
}
if nr == 0 {
break
}
}
return written
}
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!