Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-7636] Reference type has struct mutability rules applied to it #50177

Closed
swift-ci opened this issue May 8, 2018 · 2 comments
Closed

[SR-7636] Reference type has struct mutability rules applied to it #50177

swift-ci opened this issue May 8, 2018 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented May 8, 2018

Previous ID SR-7636
Radar None
Original Reporter benpious (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment

Xcode 9.3.

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 036bbf33b4aecd6cab44045b2c813b89

duplicates:

  • 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.

@belkadan
Copy link
Contributor

belkadan commented May 9, 2018

This is correct behavior, if confusing. See the example in the comments for SR-142.

@swift-ci
Copy link
Collaborator Author

Comment by Ben Pious (JIRA)

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.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants