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-9812] Disallow shadowing optional protocol members from user extensions #52232

Open
DevAndArtist mannequin opened this issue Jan 30, 2019 · 2 comments
Open

[SR-9812] Disallow shadowing optional protocol members from user extensions #52232

DevAndArtist mannequin opened this issue Jan 30, 2019 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@DevAndArtist
Copy link
Mannequin

DevAndArtist mannequin commented Jan 30, 2019

Previous ID SR-9812
Radar None
Original Reporter @DevAndArtist
Type Bug
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 06ddb1a8670e4226e673abac6618b2cb

Issue Description:

Apparently this corner case is not captured by the compiler with an 'Invalid redeclaration of 'test'' error. We can shadow optional-members in a custom extension and make them non-optional:

// Module A
public protocol P {
  var test: Int? { get }
}

extension P {
  public var test: Int? {
    return nil
  }
}

// Module B
import A

extension P {
  var test: Int {
    return test!
  }
}
@belkadan
Copy link
Contributor

I believe this is deliberate; you're allowed to shadow things from other modules in your own module, which is important if someone introduces a new decl in their library but you want to continue using yours. This also works:

import A

extension P {
  var test: Int? {
    return nil
  }
}

Arguably, though, we could have a warning or something at first, and then you have to add an attribute to say "yes, I know I'm shadowing this".

@theblixguy
Copy link
Collaborator

Spent an hour trying to figure out why my code wasn’t working and this was the culprit. It would be really useful to have warning diagnostic when you have an optional protocol requirement satisfied by a default implementation and the conforming type’s implementation is non-optional. An attribute to silence the warning sounds reasonable, maybe something like “@shadowImplementation” (open to suggestions).

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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