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
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
associated-item-privacy-inherent.rs
associated-item-privacy-inherent.stderr
associated-item-privacy-trait.rs
associated-item-privacy-trait.stderr
associated-item-privacy-type-binding.rs
associated-item-privacy-type-binding.stderr
crate-private-reexport.rs
crate-private-reexport.stderr
ctor.rs
decl-macro-infinite-global-import-cycle-ice-64784.rs
decl-macro.rs
decl-macro.stderr
effective_visibilities_full_priv.rs
effective_visibilities_full_priv.stderr
effective_visibilities_glob.rs
effective_visibilities_glob.stderr
effective_visibilities_invariants.rs
effective_visibilities_invariants.stderr
effective_visibilities.rs
effective_visibilities.stderr
export-tag-variant.rs
export-tag-variant.stderr
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
import-list-stem-visibility-issue-119126.rs
issue-11593.rs
issue-11593.stderr
issue-13641.rs
issue-13641.stderr
issue-17718-const-privacy.rs
issue-17718-const-privacy.stderr
issue-29161.rs
issue-29161.stderr Improve diagnostic by suggesting to remove visibility qualifier 2024-04-12 12:59:40 +09:00
issue-30079.rs
issue-30079.stderr
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
issue-57264-2.rs
issue-75062-fieldless-tuple-struct.rs
issue-75062-fieldless-tuple-struct.stderr
issue-75906.rs
issue-75906.stderr
issue-75907_b.rs
issue-75907_b.stderr
issue-75907.rs
issue-75907.stderr
issue-79593.rs
issue-79593.stderr
issue-92755.rs
issue-111220-2-tuple-struct-fields-projection.rs
issue-111220-2-tuple-struct-fields-projection.stderr
issue-111220-tuple-struct-fields.rs
issue-111220-tuple-struct-fields.stderr
issue-113860-1.rs
issue-113860-1.stderr Improve diagnostic by suggesting to remove visibility qualifier 2024-04-12 12:59:40 +09:00
issue-113860-2.rs
issue-113860-2.stderr Improve diagnostic by suggesting to remove visibility qualifier 2024-04-12 12:59:40 +09:00
issue-113860.rs
issue-113860.stderr Improve diagnostic by suggesting to remove visibility qualifier 2024-04-12 12:59:40 +09:00
issue-117997.rs
issue-119463.rs
issue-119463.stderr
legacy-ctor-visibility.rs
legacy-ctor-visibility.stderr
macro-private-reexport.rs
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
priv-in-bad-locations.rs
priv-in-bad-locations.stderr Improve diagnostic by suggesting to remove visibility qualifier 2024-04-12 12:59:40 +09:00
privacy1-rpass.rs
privacy1.rs
privacy1.stderr
privacy2.rs
privacy2.stderr
privacy3.rs
privacy3.stderr
privacy4.rs
privacy4.stderr
privacy5.rs
privacy5.stderr
privacy-in-paths.rs
privacy-in-paths.stderr
privacy-ns1.rs
privacy-ns1.stderr
privacy-ns2.rs
privacy-ns2.stderr
privacy-ns.rs
privacy-reexport.rs
privacy-sanity.rs
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
private-bounds-locally-allowed.rs
private-class-field.rs
private-field-ty-err.rs
private-field-ty-err.stderr
private-impl-method.rs
private-impl-method.stderr
private-in-public-assoc-ty.rs
private-in-public-assoc-ty.stderr
private-in-public-expr-pat.rs
private-in-public-ill-formed.rs
private-in-public-ill-formed.stderr
private-in-public-non-principal-2.rs
private-in-public-non-principal-2.stderr
private-in-public-non-principal.rs
private-in-public-non-principal.stderr
private-in-public-type-alias-impl-trait.rs
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
private-in-public.stderr
private-inferred-type-1.rs
private-inferred-type-1.stderr
private-inferred-type-2.rs
private-inferred-type-2.stderr
private-inferred-type-3.rs
private-inferred-type-3.stderr
private-inferred-type.rs
private-inferred-type.stderr
private-item-simple.rs
private-item-simple.stderr
private-method-cross-crate.rs
private-method-cross-crate.stderr
private-method-inherited.rs
private-method-inherited.stderr
private-method-rpass.rs
private-method.rs
private-method.stderr
private-struct-field-cross-crate.rs
private-struct-field-cross-crate.stderr
private-struct-field-ctor.rs
private-struct-field-ctor.stderr
private-struct-field-pattern.rs
private-struct-field-pattern.stderr
private-struct-field.rs
private-struct-field.stderr
private-type-in-interface.rs
private-type-in-interface.stderr
private-variant-reexport.rs
private-variant-reexport.stderr
projections2.rs
projections2.stderr
projections.rs
projections.stderr
pub_use_mods_xcrate_exe.rs
pub-extern-privacy.rs
pub-use-xcrate.rs
reachable-unnameable-items.rs
struct-field-type.rs
struct-field-type.stderr
suggest-box-new.rs
suggest-box-new.stderr
suggest-making-field-public.fixed
suggest-making-field-public.rs
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
unnameable_types.rs
unnameable_types.stderr
unreachable-issue-121455.rs
unreachable-issue-121455.stderr
unresolved-trait-impl-item.rs
unresolved-trait-impl-item.stderr
useless-pub.rs
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
where-pub-type-impls-priv-trait.stderr
xc-private-method2.rs
xc-private-method2.stderr
xc-private-method.rs
xc-private-method.stderr