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-142 mutating function in protocol extension erroneously requires var declaration of class variables
Issue Description:
In a class C<T> where T is constrained to be a reference type and conform to a protocol which is not, Swift does not recognize that T should be treated as a reference type and mutations should be allowed even on {{let}}s of type T.
{{
class ReferenceType {
}
protocol Protocol {
var settable: Bool { get set }
}
class C<T> where T: ReferenceType, T: Protocol {
func f(t: T) {
t.settable = false // cannot assign to property: 't' is a 'let' constant
}
}
}}
Making Protocol inherit from AnyObject or NSObjectProtocol fixes the problem in this case.
In the real-world case that led me to report this, my equivalent of Protocol actually did conform to NSObjectProtocol through UITextInput, but Swift still didn't realize it was a reference type. Only be changing all of the other protocols that Protocol inherited from was I able to convince the compiler that t was a reference type. As t was a commonly used property of C in my case, being forced to make it a var was quite inconvenient. I wasn't able to make a small test case of this, however.
The text was updated successfully, but these errors were encountered:
Seems like there should be an explanation of why this isn't allowed in the error message, assuming that it's possible to identify that only a subset of the conformed-to protocols or concrete type are reference types.
Environment
Xcode 9.3.
Additional Detail from JIRA
md5: 036bbf33b4aecd6cab44045b2c813b89
duplicates:
var
declaration of class variablesIssue Description:
In a class
C<T>
whereT
is constrained to be a reference type and conform to a protocol which is not, Swift does not recognize thatT
should be treated as a reference type and mutations should be allowed even on {{let}}s of typeT
.{{
class ReferenceType {
}
protocol Protocol {
var settable: Bool { get set }
}
class C<T> where T: ReferenceType, T: Protocol {
func f(t: T) {
t.settable = false // cannot assign to property: 't' is a 'let' constant
}
}
}}
Making
Protocol
inherit fromAnyObject
orNSObjectProtocol
fixes the problem in this case.In the real-world case that led me to report this, my equivalent of
Protocol
actually did conform toNSObjectProtocol
throughUITextInput
, but Swift still didn't realize it was a reference type. Only be changing all of the other protocols thatProtocol
inherited from was I able to convince the compiler thatt
was a reference type. Ast
was a commonly used property ofC
in my case, being forced to make it avar
was quite inconvenient. I wasn't able to make a small test case of this, however.The text was updated successfully, but these errors were encountered: