Files
swift-mirror/test/decl/protocol/conforms/self.swift
Slava Pestov 7cbd0c0b37 Sema: Lift restriction on classes conforming to protocols with default implementations returning 'Self'
Now that we pass in the correct type metadata for 'Self', it is
sound for a class to conform to a protocol with a default implementation
for a method returning 'Self'.

Fixes <rdar://problem/23671426>.
2017-10-09 19:53:51 -07:00

38 lines
841 B
Swift

// RUN: %target-typecheck-verify-swift
protocol P {
associatedtype T = Int
func hasDefault()
func returnsSelf() -> Self
func hasDefaultTakesT(_: T)
func returnsSelfTakesT(_: T) -> Self
}
extension P {
func hasDefault() {}
func returnsSelf() -> Self {
return self
}
func hasDefaultTakesT(_: T) {}
func returnsSelfTakesT(_: T) -> Self { // expected-error {{method 'returnsSelfTakesT' in non-final class 'Class' cannot be implemented in a protocol extension because it returns `Self` and has associated type requirements}}
return self
}
}
// This fails
class Class : P {}
// This succeeds, because the class is final
final class FinalClass : P {}
// This succeeds, because we're not using the default implementation
class NonFinalClass : P {
func returnsSelfTakesT(_: T) -> Self {
return self
}
}