Skip to content

[SR-142] mutating function in protocol extension erroneously requires var declaration of class variables #42764

@groue

Description

@groue
Previous ID SR-142
Radar None
Original Reporter @groue
Type Bug
Status Resolved
Resolution Done
Environment

Version 7.1.1 (7B1005)

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, LanguageFeatureRequest
Assignee None
Priority Medium

md5: c88df0cab57ceac17b5038fd1dff4b54

is duplicated by:

  • SR-2464 Reference types can't call mutating functions declared in a protocol extension.
  • SR-2693 Cannot assign to property of an instance of a class declared in a protocol extension
  • SR-4229 Cannot use mutating member on immutable value: 'self' is immutable
  • SR-7636 Reference type has struct mutability rules applied to it
  • SR-8805 Cannot set protocol extension property on adopting reference type which is immutable
  • SR-4227 Can't call mutating protocol extension method in class init
  • SR-6120 Bug with Class and Subtype existentials, when protocol doesn't have class requirement
  • SR-10775 Mutating method in protocol extension always requires variable to be var

relates to:

  • SR-2220 A protocol's mutating keyword is enforced on AnyObject when using generics
  • SR-4541 Class-Only protocol inheriting from normal protocol require to be var to allow assign to their properties.

Issue Description:

Hello,

The following code won't compile, when it should:

protocol P { }

extension P {
    mutating func m() { }
}

class C : P { }

let c = C()
// error: cannot use mutating member on immutable value: 'c' is a 'let' constant
c.m()

This error should not exist: the type of variable c is C. Mutating methods can not be added to C, because of the 'mutating' isn't valid on methods in classes or class-bound protocols error. Hence the mutating qualifier of P.m should not apply to c, and c should be able to be declared as a let variable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    compilerThe Swift compiler itself

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions