rust/tests/ui/privacy
Trevor Gross ceae37188b
Rollup merge of #126575 - fmease:update-lint-type_alias_bounds, r=compiler-errors
Make it crystal clear what lint `type_alias_bounds` actually signifies

This is part of my work on https://github.com/rust-lang/rust/labels/F-lazy_type_alias ([tracking issue](#112792)).

---

To recap, the lint `type_alias_bounds` detects bounds on generic parameters and where clauses on (eager) type aliases. These bounds should've never been allowed because they are currently neither enforced[^1] at usage sites of type aliases nor thoroughly checked for correctness at definition sites due to the way type aliases are represented in the compiler. Allowing them was an oversight.

Explicitly label this as a known limitation of the type checker/system and establish the experimental feature `lazy_type_alias` as its eventual proper solution.

Where this becomes a bit tricky (for me as a rustc dev) are the "secondary effects" of these bounds whose existence I sadly can't deny. As a matter of fact, type alias bounds do play some small roles during type checking. However, after a lot of thinking over the last two weeks I've come to the conclusion (not without second-guessing myself though) that these use cases should not trump the fact that these bounds are currently *inherently broken*. Therefore the lint `type_alias_bounds` should and will continue to flag bounds that may have subordinate uses.

The two *known* secondary effects are:

1. They may enable the use of "shorthand" associated type paths `T::Assoc` (as opposed to fully qualified paths `<T as Trait>::Assoc`) where `T` is a type param bounded by some trait `Trait` which defines that assoc ty.
2. They may affect the default lifetime of trait object types passed as a type argument to the type alias. That concept is called (trait) object lifetime default.

The second one is negligible, no question asked. The first one however is actually "kinda nice" (for writability) and comes up in practice from time to time.

So why don't I just special-case trait bounds that "define" shorthand assoc type paths as originally planned in #125709?

1. Starting to permit even a tiny subset of bounds would already be enough to send a signal to users that bounds in type aliases have been legitimized and that they can expect to see type alias bounds in the wild from now on (proliferation). This would be actively misleading and dangerous because those bounds don't behave at all like one would expect, they are *not real*[^2]!
   1. Let's take `type A<T: Trait> = T::Proj;` for example. Everywhere else in the language `T: Trait` means `T: Trait + Sized`. For type aliases, that's not the case though: `T: Trait` and `T: Trait + ?Sized` for that matter do neither mean `T: Trait + Sized` nor `T: Trait + ?Sized` (for both!). Instead, whether `T` requires `Sized` or not entirely depends on the definition of `Trait`[^2]. Namely, whether or not it is bounded by `Sized`.
   2. Given `type A<T: Trait<AssocA = ()>> = T::AssocB;`, while `X: Trait` gets checked given `A<X>` (by virtue of projection wfchecking post alias expansion[^2]), the associated type constraint `AssocA = ()` gets dropped entirely! While we could choose to warn on such cases, it would inevitably lead to a huge pile of special cases.
   3. While it's common knowledge that the body / aliased type / RHS of an (eager) type alias does not get checked for well-formedness, I'm not sure if people would realize that that extends to bounds as well. Namely, `type A<T: Trait<[u8]>> = T::Proj;` compiles even if `Trait`'s generic parameter requires `Sized`. Of course, at usage sites `[u8]: Sized` would still end up getting checked[^2], so it's not a huge problem if you have full control over `A`. However, imagine that `A` was actually part of a public API and was never used inside the defining crate (not unreasonable). In such a scenario, downstream users would be presented with an impossible to use type alias! Remember, bounds may grow arbitrarily complex and nuanced in practice.
   4. Even if we allowed trait bounds that "define" shorthand assoc type paths, we would still need to continue to warn in cases where the assoc ty comes from a supertrait despite the fact that the shorthand syntax can be used: `type A<T: Sub> = T::Assoc;` does compile given `trait Sub: Super {}` and `trait Super { type Assoc; }`. However, `A<X>` does not enforce `X: Sub`, only `X: Super`[^2]. All that to say, type alias bounds are simply not real and we shouldn't pretend they are!
   5. Summarizing the points above, we would be legitimizing bounds that are completely broken!
2. It's infeasible to implement: Due to the lack of `TypeckResults` in `ItemCtxt` (and a way to propagate it to other parts of the compiler), the resolution of type-dependent paths in non-`Body` items (most notably type aliases) is not recoverable from the HIR alone which would be necessary because the information of whether an associated type path (projection) is a shorthand is only present pre&in-HIR and doesn't survive HIR ty lowering. Of course, I could rerun parts of HIR ty lowering inside the lint `type_alias_bounds` (namely, `probe_single_ty_param_bound_for_assoc_ty` which would need to be exposed or alternatively a stripped-down version of it). This likely has a performance impact and introduces complexity. In short, the "benefits" are not worth the costs.

---

* 3rd commit: Update a diagnostic to avoid suggesting type alias bounds
* 4th commit: Flag type alias bounds even if the RHS contains inherent associated types.
  * I started to allow them at some point in the past which was not correct (see commit for details)
* 5th commit: Allow type alias bounds if the RHS contains const projections and GCEs are enabled
  * (and add a `FIXME(generic_const_exprs)` to be revisited before (M)GCE's stabilization)
  * As a matter of fact type alias bounds are enforced in this case because the contained AnonConsts do get checked for well-formedness and crucially they inherit the generics and predicates of their parent item (here: the type alias)
* Remaining commits: Improve the lint `type_alias_bounds` itself

---

Fixes #125789 (sugg diag fix).
Fixes #125709 (wontfix, acknowledgement, sugg diag applic fix).
Fixes #104918 (sugg diag applic fix).
Fixes #100270 (wontfix, acknowledgement, sugg diag applic fix).
Fixes #94398 (true fix).

r? `@compiler-errors` `@oli-obk`

[^1]: From the perspective of the trait solver.
[^2]: Given `type A<T: Trait> = T::Proj;`, the reason why the trait bound "`T: Trait`" gets *seemingly* enforced at usage sites of the type alias `A` is simply because `A<X>` gets expanded to "`<X as Trait>::Proj`" very early on and it's the *expansion* that gets checked for well-formedness, not the type alias reference.
2024-07-26 02:20:28 -04:00
..
auxiliary [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
pub-priv-dep Add some tests for public-private dependencies. 2024-05-22 13:47:15 -07:00
restricted Do not use question as label 2024-07-24 21:03:27 +00:00
sealed-traits [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-item-privacy-inherent.rs Replace old private-in-public diagnostic with type privacy lints 2023-08-02 13:40:28 +03:00
associated-item-privacy-inherent.stderr
associated-item-privacy-trait.rs Allow newly added non_local_definitions lint in tests 2024-02-17 13:59:45 +01:00
associated-item-privacy-trait.stderr Stop using hir_ty_to_ty in rustc_privacy 2024-02-07 14:59:26 +00:00
associated-item-privacy-type-binding.rs Replace old private-in-public diagnostic with type privacy lints 2023-08-02 13:40:28 +03:00
associated-item-privacy-type-binding.stderr Stop using hir_ty_to_ty in rustc_privacy 2024-02-07 14:59:26 +00:00
crate-private-reexport.rs
crate-private-reexport.stderr
ctor.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
decl-macro-infinite-global-import-cycle-ice-64784.rs add issue numbers via // issue: rust-lang/rust#ISSUE_NUM directive 2024-03-24 09:34:11 +01:00
decl-macro.rs
decl-macro.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
effective_visibilities_full_priv.rs Replace old private-in-public diagnostic with type privacy lints 2023-08-02 13:40:28 +03:00
effective_visibilities_full_priv.stderr Add a note to duplicate diagnostics 2023-10-05 01:04:41 +00:00
effective_visibilities_glob.rs
effective_visibilities_glob.stderr
effective_visibilities_invariants.rs
effective_visibilities_invariants.stderr When displaying multispans, ignore empty lines adjacent to ... 2024-03-18 16:25:36 +00:00
effective_visibilities.rs resolve: Restore some effective visibility optimizations 2023-03-31 17:07:59 +04:00
effective_visibilities.stderr Add a note to duplicate diagnostics 2023-10-05 01:04:41 +00:00
export-tag-variant.rs
export-tag-variant.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
generic_struct_field_projection.rs Only inspect user-written predicates for privacy concerns 2024-04-04 14:43:44 +00:00
impl-privacy-xc-2.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
import-list-stem-visibility-issue-119126.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-11593.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-11593.stderr Move some tests 2024-02-09 15:43:08 -03:00
issue-13641.rs
issue-13641.stderr
issue-17718-const-privacy.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-17718-const-privacy.stderr
issue-29161.rs Update ui tests involving invalid visibility qualifiers 2023-04-03 22:28:55 -05:00
issue-29161.stderr Improve diagnostic by suggesting to remove visibility qualifier 2024-04-12 12:59:40 +09:00
issue-30079.rs Replace old private-in-public diagnostic with type privacy lints 2023-08-02 13:40:28 +03:00
issue-30079.stderr Replace old private-in-public diagnostic with type privacy lints 2023-08-02 13:40:28 +03:00
issue-46209-private-enum-variant-reexport.rs Make early lints translatable 2024-05-21 20:16:39 +00:00
issue-46209-private-enum-variant-reexport.stderr Make early lints translatable 2024-05-21 20:16:39 +00:00
issue-57264-1.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-57264-2.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-75062-fieldless-tuple-struct.rs
issue-75062-fieldless-tuple-struct.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-75906.rs
issue-75906.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-75907_b.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-75907_b.stderr
issue-75907.rs
issue-75907.stderr
issue-79593.rs
issue-79593.stderr Tweak message on ADT with private fields building 2023-11-29 18:11:57 +00:00
issue-92755.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-111220-2-tuple-struct-fields-projection.rs fix for Self not respecting tuple Ctor privacy 2023-05-26 06:23:03 +00:00
issue-111220-2-tuple-struct-fields-projection.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-111220-tuple-struct-fields.rs fix for Self not respecting tuple Ctor privacy 2023-05-26 06:23:03 +00:00
issue-111220-tuple-struct-fields.stderr fix for Self not respecting tuple Ctor privacy 2023-05-26 06:23:03 +00:00
issue-113860-1.rs privacy: no nominal visibility for assoc fns 2023-07-28 14:28:02 +01:00
issue-113860-1.stderr Improve diagnostic by suggesting to remove visibility qualifier 2024-04-12 12:59:40 +09:00
issue-113860-2.rs privacy: no nominal visibility for assoc fns 2023-07-28 14:28:02 +01:00
issue-113860-2.stderr Improve diagnostic by suggesting to remove visibility qualifier 2024-04-12 12:59:40 +09:00
issue-113860.rs privacy: no nominal visibility for assoc fns 2023-07-28 14:28:02 +01:00
issue-113860.stderr Improve diagnostic by suggesting to remove visibility qualifier 2024-04-12 12:59:40 +09:00
issue-117997.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-119463.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-119463.stderr fallback visibility for unexpected trait item 2024-01-04 02:02:57 +08:00
legacy-ctor-visibility.rs
legacy-ctor-visibility.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
macro-private-reexport.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
macro-private-reexport.stderr
no-ice-on-inference-failure.rs Do not ICE in privacy when type inference fails. 2024-06-17 10:09:27 +00:00
no-ice-on-inference-failure.stderr Do not ICE in privacy when type inference fails. 2024-06-17 10:09:27 +00:00
priv-impl-prim-ty.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
priv-in-bad-locations.rs Update ui tests involving invalid visibility qualifiers 2023-04-03 22:28:55 -05:00
priv-in-bad-locations.stderr Improve diagnostic by suggesting to remove visibility qualifier 2024-04-12 12:59:40 +09:00
privacy1-rpass.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
privacy1.rs
privacy1.stderr Add a note to duplicate diagnostics 2023-10-05 01:04:41 +00:00
privacy2.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
privacy2.stderr Structured use suggestion on privacy error 2023-12-04 22:26:08 +00:00
privacy3.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
privacy3.stderr typeck in parallel 2023-07-11 17:52:43 +08:00
privacy4.rs
privacy4.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
privacy5.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
privacy5.stderr
privacy-in-paths.rs
privacy-in-paths.stderr Tweak privacy errors to account for reachable items 2023-06-22 16:50:31 +00:00
privacy-ns1.rs
privacy-ns1.stderr Special-case item attributes in the suggestion output 2023-04-12 22:50:10 +00:00
privacy-ns2.rs
privacy-ns2.stderr Special-case item attributes in the suggestion output 2023-04-12 22:50:10 +00:00
privacy-ns.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
privacy-reexport.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
privacy-sanity.rs Update ui tests involving invalid visibility qualifiers 2023-04-03 22:28:55 -05:00
privacy-sanity.stderr Improve diagnostic by suggesting to remove visibility qualifier 2024-04-12 12:59:40 +09:00
privacy-ufcs.rs
privacy-ufcs.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
private-bounds-locally-allowed.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
private-class-field.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
private-field-ty-err.rs
private-field-ty-err.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
private-impl-method.rs
private-impl-method.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
private-in-public-assoc-ty.rs Replace old private-in-public diagnostic with type privacy lints 2023-08-02 13:40:28 +03:00
private-in-public-assoc-ty.stderr Replace old private-in-public diagnostic with type privacy lints 2023-08-02 13:40:28 +03:00
private-in-public-expr-pat.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
private-in-public-ill-formed.rs
private-in-public-ill-formed.stderr
private-in-public-non-principal-2.rs Replace old private-in-public diagnostic with type privacy lints 2023-08-02 13:40:28 +03:00
private-in-public-non-principal-2.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
private-in-public-non-principal.rs Allow newly added non_local_definitions lint in tests 2024-02-17 13:59:45 +01:00
private-in-public-non-principal.stderr Allow newly added non_local_definitions lint in tests 2024-02-17 13:59:45 +01:00
private-in-public-type-alias-impl-trait.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
private-in-public-warn.rs Improve the impl and diag output of lint type_alias_bounds 2024-07-23 01:48:03 +02:00
private-in-public-warn.stderr Improve the impl and diag output of lint type_alias_bounds 2024-07-23 01:48:03 +02:00
private-in-public.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
private-in-public.stderr privacy: visit trait def id of projections 2023-12-08 14:26:03 +00:00
private-inferred-type-1.rs Private-in-public lints implementation 2023-06-12 01:02:19 +03:00
private-inferred-type-1.stderr Private-in-public lints implementation 2023-06-12 01:02:19 +03:00
private-inferred-type-2.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
private-inferred-type-2.stderr Replace old private-in-public diagnostic with type privacy lints 2023-08-02 13:40:28 +03:00
private-inferred-type-3.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
private-inferred-type-3.stderr
private-inferred-type.rs Replace old private-in-public diagnostic with type privacy lints 2023-08-02 13:40:28 +03:00
private-inferred-type.stderr
private-item-simple.rs
private-item-simple.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
private-method-cross-crate.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
private-method-cross-crate.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
private-method-inherited.rs
private-method-inherited.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
private-method-rpass.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
private-method.rs
private-method.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
private-struct-field-cross-crate.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
private-struct-field-cross-crate.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
private-struct-field-ctor.rs
private-struct-field-ctor.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
private-struct-field-pattern.rs
private-struct-field-pattern.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
private-struct-field.rs
private-struct-field.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
private-type-in-interface.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
private-type-in-interface.stderr Stop using hir_ty_to_ty in rustc_privacy 2024-02-07 14:59:26 +00:00
private-variant-reexport.rs use visibility to check unused imports and delete some stmts 2023-10-22 21:27:46 +08:00
private-variant-reexport.stderr vis note for no pub reexports glob import 2023-12-01 12:10:07 +08:00
projections2.rs Add some tests for associated type normalization edge cases 2024-02-08 12:28:35 +00:00
projections2.stderr Add some tests for associated type normalization edge cases 2024-02-08 12:28:35 +00:00
projections.rs Add some tests for associated type normalization edge cases 2024-02-08 12:28:35 +00:00
projections.stderr Add some tests for associated type normalization edge cases 2024-02-08 12:28:35 +00:00
pub_use_mods_xcrate_exe.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
pub-extern-privacy.rs Update test directives for wasm32-wasip1 2024-03-11 09:36:35 -07:00
pub-use-xcrate.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
reachable-unnameable-items.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
struct-field-type.rs Stop using hir_ty_to_ty in rustc_privacy 2024-02-07 14:59:26 +00:00
struct-field-type.stderr Stop using hir_ty_to_ty in rustc_privacy 2024-02-07 14:59:26 +00:00
suggest-box-new.rs Suggest builder functions on struct literal with private fields 2023-11-19 17:50:47 +00:00
suggest-box-new.stderr Tweak message on ADT with private fields building 2023-11-29 18:11:57 +00:00
suggest-making-field-public.fixed Allow unused fields in some tests 2024-03-12 10:59:41 +01:00
suggest-making-field-public.rs Allow unused fields in some tests 2024-03-12 10:59:41 +01:00
suggest-making-field-public.stderr
ufc-method-call.different_name.stderr Add test description 2024-06-04 15:34:04 +00:00
ufc-method-call.rs Add test description 2024-06-04 15:34:04 +00:00
ufc-method-call.same_name.stderr Add test description 2024-06-04 15:34:04 +00:00
union-field-privacy-1.rs
union-field-privacy-1.stderr
union-field-privacy-2.rs
union-field-privacy-2.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
unnameable_types.rs privacy: Stabilize lint unnameable_types 2024-03-13 18:37:40 +03:00
unnameable_types.stderr privacy: Stabilize lint unnameable_types 2024-03-13 18:37:40 +03:00
unreachable-issue-121455.rs Allow for a missing adt_def in NamePrivacyVisitor. 2024-02-23 10:57:11 +11:00
unreachable-issue-121455.stderr Allow for a missing adt_def in NamePrivacyVisitor. 2024-02-23 10:57:11 +11:00
unresolved-trait-impl-item.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unresolved-trait-impl-item.stderr resolve: Feed visibilities for unresolved trait impl items 2023-12-19 22:33:26 +03:00
useless-pub.rs Update ui tests involving invalid visibility qualifiers 2023-04-03 22:28:55 -05:00
useless-pub.stderr Improve diagnostic by suggesting to remove visibility qualifier 2024-04-12 12:59:40 +09:00
where-priv-type.rs Bless tests and handle tests/crashes 2024-06-05 22:25:42 +01:00
where-priv-type.stderr Bless tests and handle tests/crashes 2024-06-05 22:25:42 +01:00
where-pub-type-impls-priv-trait.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
where-pub-type-impls-priv-trait.stderr Extend impl's def_span to include where clauses 2023-10-09 11:47:02 +00:00
xc-private-method2.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
xc-private-method2.stderr Move some UI tests into subdirectories 2023-04-02 19:42:30 -04:00
xc-private-method.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
xc-private-method.stderr Move some UI tests into subdirectories 2023-04-02 19:42:30 -04:00