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-14918] Improve diagnostic on incorrect usage of private type in private extension #57265

Open
typesanitizer opened this issue Jul 13, 2021 · 1 comment
Labels
compiler The Swift compiler in itself improvement

Comments

@typesanitizer
Copy link

Previous ID SR-14918
Radar rdar://problem/80820291
Original Reporter @typesanitizer
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement
Assignee None
Priority Medium

md5: 8fe9d01d9a2a8a667aa181d42aedbfa2

Issue Description:

Confusion between fileprivate and private extensions is somewhat common; this question on StackOverflow (https://stackoverflow.com/questions/43287220/difference-between-fileprivate-and-private-extension) is at +11 right now.

This leads to surprising behavior sometimes.

struct S {
    private typealias T = Int
}

private extension S {
    var p: T { 0 } // error: property must be declared private because its type uses a private type
}

One reasonable expectation is that code behaves as-if the inner property is also marked private. However, that's the case, it behaves as-if the outer extension (and the inner property) are marked fileprivate. Arguably, we should do the following to improve the diagnostic and make the code clearer:

1. Provide some more explanation: In the case where such a property (subscript etc.) is inside a private extension – perhaps we should point out that an extension marked private is implicitly treated as fileprivate.
2. We should offer a fix-it to add the right annotation (I don't think we are doing this already), and perhaps update the extension's privacy to say fileprivate instead of private so it's clearer because that's what it really means (otherwise, some beginner reading the code may be surprised to see the duplicated private).

@typesanitizer
Copy link
Author

@swift-ci create

@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
compiler The Swift compiler in itself improvement
Projects
None yet
Development

No branches or pull requests

1 participant