Relax priv-in-pub lint on generic bounds and where clauses of trait impls. - #90586
Conversation
commented
Nov 4, 2021
|
(rust-highfive has picked a reviewer for you, use r? to override) |
commented
Nov 5, 2021
commented
Nov 15, 2021
|
I'm going to send this to the lang team for sanity checking. This PR removes the private-in-public warnings from generic bounds and where clauses on
|
commented
Nov 16, 2021
|
@petrochenkov I cannot quite tell from your comment what your take is here -- do you feel this is in the spirit of the original RFC? In what way does it differ? It seems surprising to me that you would say it conforms or not to the RFC based on whether the lint in the RFC is allow by default. |
commented
Nov 17, 2021
It's in the spirit of the RFC.
Back then I wanted to start with warn-by-default to get some experience, but maybe be we already have enough experience to just allow it now. |
commented
Nov 17, 2021
|
This PR also draws a finer distinction than the proposed Lint #2 makes: this PR distinguishes between private items in the bounds of trait impls, and private items in other bounds. I describe the rationale for this distinction in the description of this PR, and this PR only impacts private items in the @scottmcm suggested that all uses of private items in bounds should be allowed, but this PR doesn't go that far. I'm inclined to agree, though. The only danger of private items in bounds is that end-users can't 'forward' such bounds when they're writing generic code. But:
So, the lint proposed proposed by the type privacy RFC would just a heuristic that sometimes helps avoid an API design decision that sometimes is a pitfall. There's enough "sometimes"s in there, that I'd argue such a lint would probably be more at home in clippy than rustc. |
commented
Nov 23, 2021
|
FWIW, I like the middle-ground proposed here. The OP did a good job convincing me that the "you can forward the public trait" is an interesting distinction. We can always remove more cases later. |
commented
Nov 30, 2021
|
We didn't have time to go into this in detail in today's lang team meeting, so let's try to make progress async: @rfcbot fcp merge The OP here has a good description of what this allows. People seem to want to do this enough that the workarounds are well-known; I think we should just allow it directly. |
|
Team member @scottmcm has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
commented
Dec 7, 2021
|
@rfcbot reviewed |
commented
Dec 7, 2021
|
🔔 This is now entering its final comment period, as per the review above. 🔔 |
commented
Dec 17, 2021
|
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
commented
Dec 26, 2021
|
r=me after squashing commits and removing accidental changes from the resulting commit. |
6a5c0d6 to
ebef8a8
Compare
commented
Dec 27, 2021
|
@bors r+ |
commented
Dec 27, 2021
|
📌 Commit ebef8a8 has been approved by |
commented
Dec 27, 2021
|
⌛ Testing commit ebef8a8 with merge 1c74c60a963806a73f5fcb12ebf3c7cf1e8b7b16... |
commented
Dec 27, 2021
|
💔 Test failed - checks-actions |
commented
Dec 27, 2021
commented
Dec 27, 2021
|
@bors retry network error |
The priv-in-pub lint is a legacy mechanism of the compiler, supplanted by a reachability-based type privacy analysis. This PR does not relax type privacy; it only relaxes the lint (as proposed by the type privacy RFC) in the case of trait impls.
Current Behavior
On public trait impls, it's currently an error to have a
wherebound constraining a private type with a trait:...and it's a warning to have have a public type constrained by a private trait:
This lint applies to
whereclauses in other contexts, too; e.g. on free functions:These constraints could be relaxed without issue.
New Behavior
This lint is relaxed for
whereclauses on trait impls, such that it's okay to have awherebound constraining a private type with a trait:...and it's okay to have a public type constrained by a private trait:
Rationale
While the priv-in-pub lint is not essential for soundness, it can help programmers avoid pitfalls that would make their libraries difficult to use by others. For instance, such a lint is useful for free functions; e.g. if a downstream crate tries to call the
functionin the previous snippet in a generic context:...it cannot do so without repeating
function'swherebound, which we cannot do becausePrivis out-of-scope. A lint for this case is arguably helpful.However, this same reasoning doesn't hold for trait impls. To call an unconstrained method on a public trait impl with private bounds, you don't need to forward those private bounds, you can forward the public trait:
This PR only eliminates the lint on trait impls. It leaves it intact for all other contexts, including trait definitions, inherent impls, and function definitions. It doesn't need to exist in those cases either, but I figured I'd first target a case where it's mostly pointless.
Other Notes