Skip to content

Unexpected "cannot assign to property: 'self' is immutable" error in extension of AnyObject-constrained protocol #63112

@calda

Description

@calda

Description

Mutating method on extension of AnyObject-constrained protocol results in unexpected "cannot assign to property: 'self' is immutable" error.

Steps to reproduce

Attempt to build the following code in Swift 5.7 (playground):

class MyClass: MyProtocol {
  var property: String = ""
}

protocol ProtocolSettable {
  var property: String { get set }
}

protocol MyProtocol: AnyObject, ProtocolSettable {

}

extension MyProtocol {
  func updateProperty() {
    // 🛑 "cannot assign to property: 'self' is immutable"
    property = "test"
  }
}

It fails to compile with this error:

/main.swift:15:5: error: cannot assign to property: 'self' is immutable
    property = "test"

Expected behavior

Since MyProtocol is guaranteed to be a class, I expect this to compile.

Metadata

Metadata

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdeclarationsFeature: declarationsextensionFeature → declarations: `extension` declarationsmutabilityFeature: storage mutabilityprotocolFeature → type declarations: Protocol declarationsswift 5.7type checkerArea → compiler: Semantic analysis

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions