Skip to content

[SR-14918] Improve diagnostic on incorrect usage of private type in private extension #57265

@typesanitizer

Description

@typesanitizer
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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions