Commit b57a6b3
authored
Rollup merge of rust-lang#90586 - jswrenn:relax-privacy-lints, r=petrochenkov
Relax priv-in-pub lint on generic bounds and where clauses of trait impls.
The priv-in-pub lint is a legacy mechanism of the compiler, supplanted by a reachability-based [type privacy](https://github.com/rust-lang/rfcs/blob/master/text/2145-type-privacy.md) 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 `where` bound constraining a private type with a trait:
```rust
pub trait Trait {}
pub struct Type {}
struct Priv {}
impl Trait for Priv {}
impl Trait for Type
where
Priv: Trait // ERROR
{}
```
...and it's a **warning** to have have a public type constrained by a private trait:
```rust
pub trait Trait {}
pub struct Type {}
pub struct Pub {}
trait Priv {}
impl Priv for Pub {}
impl Trait for Type
where
Pub: Priv // WARNING
{}
```
This lint applies to `where` clauses in other contexts, too; e.g. on free functions:
```rust
struct Priv<T>(T);
pub trait Pub {}
impl<T: Pub> Pub for Priv<T> {}
pub fn function<T>()
where
Priv<T>: Pub // WARNING
{}
```
**These constraints could be relaxed without issue.**
## New Behavior
This lint is relaxed for `where` clauses on trait impls, such that it's okay to have a `where` bound constraining a private type with a trait:
```rust
pub trait Trait {}
pub struct Type {}
struct Priv {}
impl Trait for Priv {}
impl Trait for Type
where
Priv: Trait // OK
{}
```
...and it's okay to have a public type constrained by a private trait:
```rust
pub trait Trait {}
pub struct Type {}
pub struct Pub {}
trait Priv {}
impl Priv for Pub {}
impl Trait for Type
where
Pub: Priv // OK
{}
```
## 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 `function` in the previous snippet in a generic context:
```rust
fn callsite<T>()
where
Priv<T>: Pub // ERROR: omitting this bound is a compile error, but including it is too
{
function::<T>()
}
```
...it cannot do so without repeating `function`'s `where` bound, which we cannot do because `Priv` is 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:
```rust
mod upstream {
pub trait Trait {
fn method(&self) {}
}
pub struct Type<T>(T);
pub struct Pub<T>(T);
trait Priv {}
impl<T: Priv> Priv for Pub<T> {}
impl<T> Trait for Type<T>
where
Pub<T>: Priv // WARNING
{}
}
mod downstream {
use super::upstream::*;
fn function<T>(value: Type<T>)
where
Type<T>: Trait // <- no private deets!
{
value.method();
}
}
```
**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
- See discussion [on zulip](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/relax.20priv-in-pub.20lint.20for.20trait.20impl.20.60where.60.20bounds/near/222458397).
- This PR effectively reverts rust-lang#79291.File tree
9 files changed
+325
-82
lines changed- compiler/rustc_privacy/src
- src/test/ui
- const-generics/generic_const_exprs
- privacy
9 files changed
+325
-82
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2064 | 2064 | | |
2065 | 2065 | | |
2066 | 2066 | | |
2067 | | - | |
| 2067 | + | |
| 2068 | + | |
| 2069 | + | |
| 2070 | + | |
| 2071 | + | |
2068 | 2072 | | |
2069 | 2073 | | |
2070 | 2074 | | |
| |||
Lines changed: 1 addition & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
| 12 | + | |
18 | 13 | | |
19 | 14 | | |
20 | 15 | | |
| |||
Lines changed: 2 additions & 33 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | 1 | | |
33 | | - | |
| 2 | + | |
34 | 3 | | |
35 | 4 | | |
36 | 5 | | |
37 | 6 | | |
38 | 7 | | |
39 | 8 | | |
40 | 9 | | |
41 | | - | |
| 10 | + | |
42 | 11 | | |
43 | 12 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
67 | | - | |
| 66 | + | |
68 | 67 | | |
69 | 68 | | |
70 | 69 | | |
| |||
87 | 86 | | |
88 | 87 | | |
89 | 88 | | |
90 | | - | |
91 | | - | |
92 | | - | |
| 89 | + | |
93 | 90 | | |
94 | 91 | | |
95 | 92 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | 159 | | |
169 | | - | |
| 160 | + | |
170 | 161 | | |
171 | 162 | | |
172 | 163 | | |
| |||
175 | 166 | | |
176 | 167 | | |
177 | 168 | | |
178 | | - | |
| 169 | + | |
179 | 170 | | |
180 | 171 | | |
181 | 172 | | |
| |||
184 | 175 | | |
185 | 176 | | |
186 | 177 | | |
187 | | - | |
| 178 | + | |
188 | 179 | | |
189 | 180 | | |
190 | 181 | | |
| |||
193 | 184 | | |
194 | 185 | | |
195 | 186 | | |
196 | | - | |
| 187 | + | |
197 | 188 | | |
198 | 189 | | |
199 | 190 | | |
200 | 191 | | |
201 | 192 | | |
202 | 193 | | |
203 | 194 | | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | 195 | | |
214 | | - | |
| 196 | + | |
215 | 197 | | |
216 | 198 | | |
217 | 199 | | |
| |||
220 | 202 | | |
221 | 203 | | |
222 | 204 | | |
223 | | - | |
| 205 | + | |
224 | 206 | | |
225 | 207 | | |
226 | 208 | | |
| |||
229 | 211 | | |
230 | 212 | | |
231 | 213 | | |
232 | | - | |
| 214 | + | |
233 | 215 | | |
234 | 216 | | |
235 | 217 | | |
| |||
238 | 220 | | |
239 | 221 | | |
240 | 222 | | |
241 | | - | |
| 223 | + | |
242 | 224 | | |
243 | 225 | | |
244 | 226 | | |
| |||
247 | 229 | | |
248 | 230 | | |
249 | 231 | | |
250 | | - | |
| 232 | + | |
251 | 233 | | |
252 | 234 | | |
253 | 235 | | |
| |||
256 | 238 | | |
257 | 239 | | |
258 | 240 | | |
259 | | - | |
| 241 | + | |
260 | 242 | | |
261 | 243 | | |
262 | 244 | | |
| |||
265 | 247 | | |
266 | 248 | | |
267 | 249 | | |
268 | | - | |
| 250 | + | |
269 | 251 | | |
270 | 252 | | |
271 | 253 | | |
| |||
274 | 256 | | |
275 | 257 | | |
276 | 258 | | |
277 | | - | |
| 259 | + | |
278 | 260 | | |
279 | 261 | | |
280 | 262 | | |
| |||
283 | 265 | | |
284 | 266 | | |
285 | 267 | | |
286 | | - | |
| 268 | + | |
287 | 269 | | |
288 | 270 | | |
289 | 271 | | |
| |||
292 | 274 | | |
293 | 275 | | |
294 | 276 | | |
295 | | - | |
| 277 | + | |
296 | 278 | | |
297 | 279 | | |
298 | 280 | | |
| |||
301 | 283 | | |
302 | 284 | | |
303 | 285 | | |
304 | | - | |
| 286 | + | |
305 | 287 | | |
306 | 288 | | |
307 | 289 | | |
| |||
310 | 292 | | |
311 | 293 | | |
312 | 294 | | |
313 | | - | |
| 295 | + | |
314 | 296 | | |
315 | 297 | | |
316 | 298 | | |
| |||
319 | 301 | | |
320 | 302 | | |
321 | 303 | | |
322 | | - | |
| 304 | + | |
323 | 305 | | |
324 | 306 | | |
325 | 307 | | |
| |||
341 | 323 | | |
342 | 324 | | |
343 | 325 | | |
344 | | - | |
| 326 | + | |
345 | 327 | | |
346 | 328 | | |
347 | 329 | | |
| |||
352 | 334 | | |
353 | 335 | | |
354 | 336 | | |
355 | | - | |
| 337 | + | |
356 | 338 | | |
357 | 339 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
0 commit comments