mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-04 04:39:16 +00:00
ceae37188b
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. |
||
---|---|---|
.. | ||
adt_const_params | ||
array-impls | ||
auxiliary | ||
backcompat | ||
defaults | ||
early | ||
generic_arg_infer | ||
generic_const_exprs | ||
infer | ||
issues | ||
late-bound-vars | ||
min_const_generics | ||
occurs-check | ||
parser-error-recovery | ||
std | ||
type-dependent | ||
apit-with-const-param.rs | ||
arg-in-pat-1.rs | ||
arg-in-pat-2.rs | ||
arg-in-pat-3.rs | ||
argument_order.rs | ||
argument_order.stderr | ||
array-wrapper-struct-ctor.rs | ||
assoc_const_as_type_argument.rs | ||
assoc_const_as_type_argument.stderr | ||
assoc_const_eq_diagnostic.rs | ||
assoc_const_eq_diagnostic.stderr | ||
associated-type-bound-fail.rs | ||
associated-type-bound-fail.stderr | ||
associated-type-bound.rs | ||
bad-const-generic-exprs.rs | ||
bad-const-generic-exprs.stderr | ||
bad-generic-in-copy-impl.rs | ||
bad-generic-in-copy-impl.stderr | ||
bad-subst-const-kind.rs | ||
bad-subst-const-kind.stderr | ||
broken-mir-1.rs | ||
broken-mir-2.rs | ||
cannot-infer-type-for-const-param.rs | ||
coerce_unsized_array.rs | ||
concrete-const-as-fn-arg.rs | ||
concrete-const-impl-method.rs | ||
condition-in-trait-const-arg.rs | ||
const_trait_fn-issue-88433.rs | ||
const-arg-in-const-arg.min.stderr | ||
const-arg-in-const-arg.rs | ||
const-arg-in-fn.rs | ||
const-arg-type-arg-misordered.rs | ||
const-arg-type-arg-misordered.stderr | ||
const-argument-cross-crate-mismatch.rs | ||
const-argument-cross-crate-mismatch.stderr | ||
const-argument-cross-crate.rs | ||
const-argument-if-length.full.stderr | ||
const-argument-if-length.min.stderr | ||
const-argument-if-length.rs | ||
const-argument-non-static-lifetime.min.stderr | ||
const-argument-non-static-lifetime.rs | ||
const-fn-with-const-param.rs | ||
const-generic-default-wont-borrowck.fixed | ||
const-generic-default-wont-borrowck.rs | ||
const-generic-default-wont-borrowck.stderr | ||
const-generic-function.rs | ||
const-generic-function.stderr | ||
const-generic-type_name.rs | ||
const-param-after-const-literal-arg.rs | ||
const-param-before-other-params.rs | ||
const-param-before-other-params.stderr | ||
const-param-elided-lifetime.full.stderr | ||
const-param-elided-lifetime.min.stderr | ||
const-param-elided-lifetime.rs | ||
const-param-in-async.rs | ||
const-param-type-depends-on-const-param.full.stderr | ||
const-param-type-depends-on-const-param.min.stderr | ||
const-param-type-depends-on-const-param.rs | ||
const-param-type-depends-on-type-param-ungated.rs | ||
const-param-type-depends-on-type-param-ungated.stderr | ||
const-param-type-depends-on-type-param.full.stderr | ||
const-param-type-depends-on-type-param.min.stderr | ||
const-param-type-depends-on-type-param.rs | ||
const-param-with-additional-obligations.rs | ||
const-param-with-additional-obligations.stderr | ||
const-parameter-uppercase-lint.rs | ||
const-parameter-uppercase-lint.stderr | ||
core-types.rs | ||
cross_crate_complex.rs | ||
default-ty-closure.rs | ||
default-ty-closure.stderr | ||
deref-into-array-generic.rs | ||
different_generic_args_array.rs | ||
different_generic_args_array.stderr | ||
different_generic_args.full.stderr | ||
different_generic_args.min.stderr | ||
different_generic_args.rs | ||
dont-evaluate-array-len-on-err-1.rs | ||
dont-evaluate-array-len-on-err-1.stderr | ||
dyn-supertraits.rs | ||
dyn-supertraits.stderr | ||
ensure_is_evaluatable.rs | ||
ensure_is_evaluatable.stderr | ||
enum-variants.rs | ||
exhaustive-value.rs | ||
exhaustive-value.stderr | ||
expose-default-substs-param-env.rs | ||
float-generic.adt_const_params.stderr | ||
float-generic.full.stderr | ||
float-generic.rs | ||
float-generic.simple.stderr | ||
fn_with_two_const_inputs.rs | ||
fn_with_two_const_inputs.stderr | ||
fn_with_two_same_const_inputs.rs | ||
fn-const-param-call.adt_const_params.stderr | ||
fn-const-param-call.full.stderr | ||
fn-const-param-call.min.stderr | ||
fn-const-param-call.rs | ||
fn-const-param-infer.adt_const_params.stderr | ||
fn-const-param-infer.full.stderr | ||
fn-const-param-infer.min.stderr | ||
fn-const-param-infer.rs | ||
forbid-non-structural_match-types.rs | ||
forbid-non-structural_match-types.stderr | ||
foreign-item-const-parameter.rs | ||
foreign-item-const-parameter.stderr | ||
generic-param-mismatch.rs | ||
generic-param-mismatch.stderr | ||
ice-68875.rs | ||
ice-68875.stderr | ||
ice-118285-fn-ptr-value.rs | ||
ice-118285-fn-ptr-value.stderr | ||
ice-const-generic-function-return-ty.rs | ||
ice-const-generic-function-return-ty.stderr | ||
ice-unexpected-inference-var-122549.rs | ||
ice-unexpected-inference-var-122549.stderr | ||
impl-const-generic-struct.rs | ||
incorrect-number-of-const-args.rs | ||
incorrect-number-of-const-args.stderr | ||
infer_arg_from_pat.rs | ||
infer_arr_len_from_pat.rs | ||
inhabited-assoc-ty-ice-1.rs | ||
inhabited-assoc-ty-ice-2.rs | ||
integer-literal-generic-arg-in-where-clause.rs | ||
intrinsics-type_name-as-const-argument.min.stderr | ||
intrinsics-type_name-as-const-argument.rs | ||
invalid-const-arg-for-type-param.rs | ||
invalid-const-arg-for-type-param.stderr | ||
invalid-constant-in-args.rs | ||
invalid-constant-in-args.stderr | ||
invalid-enum.rs | ||
invalid-enum.stderr | ||
invariant.rs | ||
invariant.stderr | ||
issue-46511.rs | ||
issue-46511.stderr | ||
issue-66451.rs | ||
issue-66451.stderr | ||
issue-70408.rs | ||
issue-80471.rs | ||
issue-80471.stderr | ||
issue-93647.rs | ||
issue-93647.stderr | ||
issue-97007.rs | ||
issue-102124.rs | ||
issue-105689.rs | ||
issue-106419-struct-with-multiple-const-params.rs | ||
issue-112505-overflow.rs | ||
issue-112505-overflow.stderr | ||
kind_mismatch.rs | ||
kind_mismatch.stderr | ||
legacy-const-generics-bad.rs | ||
legacy-const-generics-bad.stderr | ||
legacy-const-generics.rs | ||
lifetime-in-const-param.rs | ||
lifetime-in-const-param.stderr | ||
lookup-method.rs | ||
lookup-method.stderr | ||
mistyped_const_in_pat.rs | ||
mistyped_const_in_pat.stderr | ||
nested-type.full.stderr | ||
nested-type.min.stderr | ||
nested-type.rs | ||
not_wf_param_in_rpitit.rs | ||
not_wf_param_in_rpitit.stderr | ||
opaque_types2.rs | ||
opaque_types2.stderr | ||
opaque_types.rs | ||
opaque_types.stderr | ||
outer-lifetime-in-const-generic-default.rs | ||
outer-lifetime-in-const-generic-default.stderr | ||
overlapping_impls.rs | ||
params-in-ct-in-ty-param-lazy-norm.full.stderr | ||
params-in-ct-in-ty-param-lazy-norm.min.stderr | ||
params-in-ct-in-ty-param-lazy-norm.rs | ||
parent_generics_of_encoding_impl_trait.rs | ||
parent_generics_of_encoding_impl_trait.stderr | ||
parent_generics_of_encoding.rs | ||
polymorphic-drop-shim.rs | ||
projection-as-arg-const.rs | ||
projection-as-arg-const.stderr | ||
promotion.rs | ||
raw-ptr-const-param-deref.adt_const_params.stderr | ||
raw-ptr-const-param-deref.full.stderr | ||
raw-ptr-const-param-deref.min.stderr | ||
raw-ptr-const-param-deref.rs | ||
raw-ptr-const-param.adt_const_params.stderr | ||
raw-ptr-const-param.full.stderr | ||
raw-ptr-const-param.min.stderr | ||
raw-ptr-const-param.rs | ||
repeat_expr_hack_gives_right_generics.rs | ||
repeat_expr_hack_gives_right_generics.stderr | ||
slice-const-param-mismatch.adt_const_params.stderr | ||
slice-const-param-mismatch.full.stderr | ||
slice-const-param-mismatch.min.stderr | ||
slice-const-param-mismatch.rs | ||
slice-const-param.rs | ||
sneaky-array-repeat-expr.rs | ||
sneaky-array-repeat-expr.stderr | ||
struct-with-invalid-const-param.rs | ||
struct-with-invalid-const-param.stderr | ||
suggest_const_for_array.rs | ||
suggest_const_for_array.stderr | ||
trait-const-args.rs | ||
transmute_no_gate.rs | ||
transmute_no_gate.stderr | ||
transmute-const-param-static-reference.adt_const_params.stderr | ||
transmute-const-param-static-reference.min.stderr | ||
transmute-const-param-static-reference.rs | ||
transmute-fail.rs | ||
transmute-fail.stderr | ||
transmute.rs | ||
transparent-maybeunit-array-wrapper.rs | ||
try_unify_ignore_lifetimes.rs | ||
two_matching_preds.rs | ||
type_mismatch.rs | ||
type_mismatch.stderr | ||
type_not_in_scope.rs | ||
type_not_in_scope.stderr | ||
type_of_anon_const.rs | ||
type-after-const-ok.rs | ||
types-mismatch-const-args.full.stderr | ||
types-mismatch-const-args.min.stderr | ||
types-mismatch-const-args.rs | ||
unify_with_nested_expr.rs | ||
unify_with_nested_expr.stderr | ||
uninferred-consts-during-codegen-1.rs | ||
uninferred-consts-during-codegen-2.rs | ||
unknown_adt.rs | ||
unknown_adt.stderr | ||
unused_braces.fixed | ||
unused_braces.full.fixed | ||
unused_braces.min.fixed | ||
unused_braces.rs | ||
unused_braces.stderr | ||
unused-const-param.rs | ||
unused-type-param-suggestion.rs | ||
unused-type-param-suggestion.stderr | ||
variant-discrimiant-no-generics.full.stderr | ||
variant-discrimiant-no-generics.min.stderr | ||
variant-discrimiant-no-generics.rs | ||
where-clauses.rs | ||
wrong-normalization.rs | ||
wrong-normalization.stderr |