You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SR-7634 EXC_BAD_ACCESS when using a Protocol which has a Constraint on it's definition
Issue Description:
Swift can't decide whether a variable belonging to a class constrained protocol counts as a subtype of the constrained class and therefore has access to the members of that class. I would expect it to always have such access, but it's possible that it's not supposed to. Either way the compiler is being nutty here.
class Strawman {
let name: String
public func bar(_ x: Strawman) {
print("\(name) bars \(x.name) from entering.")
}
public init(name: String) {
self.name = name
}
}
// A type must inherit from Strawman in order to implement
// Fooable, therefore all Fooable are a kind of Strawman.
// Q.E.D.
protocol Fooable where Self: Strawman {
func foo()
}
class StrawFooable: Strawman, Fooable {
public func foo() { print("Also Foo!") }
}
let sm1 = StrawFooable(name: "Strawman1")
let sm2 = StrawFooable(name: "Strawman2")
// This will not compile if you define doStuff as
// func doStuff(with x: Fooable, and y: Fooable) {
func doStuff<T: Fooable>(with x: T, and y: T) {
x.bar(y)
x.foo()
y.bar(x)
y.foo()
}
// This will not compile if you annotate sm1 and sm2 as Fooable.
doStuff(with: sm1, and: sm2)
The text was updated successfully, but these errors were encountered:
The last part, "This will not compile if you annotate sm1 and sm2 as Fooable", is a well-known corner of the language: a protocol value doesn't necessarily provide a type for a generic parameter constrained by that protocol. (Note that sm1 and sm2 could have different dynamic types.)
However, actually using protocol-typed arguments probably ought to work. @slavapestov, @huonw?
Environment
Swift 4.
Additional Detail from JIRA
md5: b4794a58f4d0c7b151aaad9efd595e7a
is duplicated by:
Issue Description:
Swift can't decide whether a variable belonging to a class constrained protocol counts as a subtype of the constrained class and therefore has access to the members of that class. I would expect it to always have such access, but it's possible that it's not supposed to. Either way the compiler is being nutty here.
The text was updated successfully, but these errors were encountered: