Skip to content

[Sema] Setter has incorrect mutating-ness inside class-constrained protocol extension #26669

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

Merged
merged 2 commits into from
Aug 16, 2019
Merged

Conversation

theblixguy
Copy link
Collaborator

If we have a class-constrained protocol extension, where the protocol does not impose a class requirement, then the setter is incorrectly inferred to be mutating by default.

Resolves SR-11298.

@theblixguy
Copy link
Collaborator Author

cc @slavapestov

@jrose-apple
Copy link
Contributor

Technically source-breaking, but I kinda hope no one is depending on this.

protocol SR_11298_P {
  init()
}

class SR_11298_C: SR_11298_P {
  var property: String = ""
  required init() {}
}

extension SR_11298_P where Self: SR_11298_C {
  var wrappingProperty: String {
    get { return property }
    set { self = .init() }
  }
}

let origInstance = SR_11298_C()
var instance = origInstance
instance.wrappingProperty = ""
print(instance === origInstance)

@jrose-apple
Copy link
Contributor

@swift-ci Please test

@jrose-apple
Copy link
Contributor

@swift-ci Please test source compatibility

@swift-ci

This comment has been minimized.

@swift-ci

This comment has been minimized.

@jrose-apple jrose-apple merged commit afd249b into swiftlang:master Aug 16, 2019
@jrose-apple
Copy link
Contributor

Thanks, @theblixguy!

@theblixguy theblixguy deleted the fix/SR-11298 branch August 16, 2019 15:57
@hamishknight
Copy link
Contributor

Should we make a note of this in the changelog? Note that this change can also break code where the setter delegates to another mutating requirement or extension member, such as:

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

extension P where Self : AnyObject {
  var wrappingProperty: String {
    get { property }
    set { property = newValue }
  }
}

@jrose-apple
Copy link
Contributor

Ah, hm. That might be a more serious concern, especially since people are already confused about why protocol methods can be mutating for classes.

@jrose-apple
Copy link
Contributor

Reverting for now.

@slavapestov
Copy link
Contributor

@theblixguy Can you create a PR that adds a test case for the current behavior, including @hamishknight's example? I'm not sure it's tested anywhere in our suite and we want to make sure any change is intentional.

@theblixguy
Copy link
Collaborator Author

Sure. I’ve also created a forum thread to discuss this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants