Represent trait constness as a distinct predicate by compiler-errors · Pull Request #131985 · rust-lang/rust (original) (raw)
cc @rust-lang/project-const-traits
r? @ghost for now
Also mirrored everything that is written below on this hackmd here: https://hackmd.io/@compiler-errors/r12zoixg1l
Tl;dr:
- This PR removes the bulk of the old effect desugaring.
- This PR reimplements most of the effect desugaring as a new predicate and set of a couple queries. I believe it majorly simplifies the implementation and allows us to move forward more easily on its implementation.
I'm putting this up both as a request for comments and a vibe-check, but also as a legitimate implementation that I'd like to see land (though no rush of course on that last part).
Background
Early days
Once upon a time, we represented trait constness in the param-env and in TraitPredicate. This was very difficult to implement correctly; it had bugs and was also incomplete; I don't think this was anyone's fault though, it was just the limit of experimental knowledge we had at that point.
Dealing with ~const within predicates themselves meant dealing with constness all throughout the trait solver. This was difficult to keep track of, and afaict was not handled well with all the corners of candidate assembly.
Specifically, we had to (in various places) remap constness according to the param-env constness:
| pred.remap_constness(&mut param_env); |
|---|
This was annoying and manual and also error prone.
Beginning of the effects desugaring
Later on, #113210 reimplemented a new desugaring for const traits via a <const HOST: bool> predicate. This essentially "reified" the const checking and separated it from any of the remapping or separate tracking in param-envs. For example, if I was in a const-if-const environment, but I wanted to call a trait that was non-const, this reification would turn the constness mismatch into a simple type mismatch of the effect parameter.
While this was a monumental step towards straightening out const trait checking in the trait system, it had its own issues, since that meant that the constness of a trait (or any item within it, like an associated type) was early-bound. This essentially meant that <T as Trait>::Assoc was distinct from <T as ~const Trait>::Assoc, which was bad.
Associated-type bound based effects desugaring
After this, #120639 implemented a new effects desugaring. This used an associated type to more clearly represent the fact that the constness is not an input parameter of a trait, but a property that could be computed of a impl. The write-up linked in that PR explains it better than I could.
However, I feel like it really reached the limits of what can comfortably be expressed in terms of associated type and trait calculus. Also, <const HOST: bool> remains a synthetic const parameter, which is observable in nested items like RPITs and closures, and comes with tons of its own hacks in the astconv and middle layer.
For example, there are pieces of unintuitive code that are needed to represent semantics like elaboration, and eventually will be needed to make error reporting intuitive, and hopefully in the future assist us in implementing built-in traits (eventually we'll want something like ~const Fn trait bounds!).
elaboration hack:
| // HACK(effects): The following code is required to get implied bounds for effects associated |
|---|
| // types to work with super traits. |
| // |
| // Suppose `data` is a trait predicate with the form `::Fx: EffectsCompat` |
| // and we know that `trait Tr: ~const SuperTr`, we need to elaborate this predicate into |
| // `::Fx: EffectsCompat`. |
| // |
| // Since the semantics for elaborating bounds about effects is equivalent to elaborating |
| // bounds about super traits (elaborate `T: Tr` into `T: SuperTr`), we place effects elaboration |
| // next to super trait elaboration. |
| if cx.is_lang_item(data.def_id(), TraitSolverLangItem::EffectsCompat) |
| && matches!(self.mode, Filter::All) |
| { |
| // first, ensure that the predicate we've got looks like a `::Fx: EffectsCompat`. |
| if let ty::Alias(ty::AliasTyKind::Projection, alias_ty) = data.self_ty().kind() |
| { |
| // look for effects-level bounds that look like `::Fx: TyCompat<::Fx>` |
| // on the trait, which is proof to us that `Tr: ~const SuperTr`. We're looking for bounds on the |
| // associated trait, so we use `explicit_implied_predicates_of` since it gives us more than just |
| // `Self: SuperTr` bounds. |
| let bounds = cx.explicit_implied_predicates_of(cx.parent(alias_ty.def_id)); |
| // instantiate the implied bounds, so we get `::Fx` and not `::Fx`. |
| let elaborated = bounds.iter_instantiated(cx, alias_ty.args).filter_map( |
| |(clause, _) |
| let ty::ClauseKind::Trait(tycompat_bound) = |
| clause.kind().skip_binder() |
| else { |
| return None; |
| }; |
| if !cx.is_lang_item( |
| tycompat_bound.def_id(), |
| TraitSolverLangItem::EffectsTyCompat, |
| ) { |
| return None; |
| } |
| // extract `::Fx` from the `TyCompat` bound. |
| let supertrait_effects_ty = |
| tycompat_bound.trait_ref.args.type_at(1); |
| let ty::Alias(ty::AliasTyKind::Projection, supertrait_alias_ty) = |
| supertrait_effects_ty.kind() |
| else { |
| return None; |
| }; |
| // The self types (`T`) must be equal for `::Fx` and `::Fx`. |
| if supertrait_alias_ty.self_ty() != alias_ty.self_ty() { |
| return None; |
| }; |
| // replace the self type in the original bound `::Fx: EffectsCompat` |
| // to the effects type of the super trait. (`::Fx`) |
| let elaborated_bound = data.with_self_ty(cx, supertrait_effects_ty); |
| Some( |
| elaboratable |
| .child(bound_clause.rebind(elaborated_bound).upcast(cx)), |
| ) |
| }, |
| ); |
| self.extend_deduped(elaborated); |
| } |
| } |
trait bound remapping hack for diagnostics:
| /// For effects predicates such as `::Effects: Compat`, pretend that the |
|---|
| /// predicate that failed was `u32: Add`. Return the constness of such predicate to later |
| /// print as `u32: ~const Add`. |
| fn get_effects_trait_pred_override( |
| &self, |
| p: ty::PolyTraitPredicate<'tcx>, |
| leaf: ty::PolyTraitPredicate<'tcx>, |
| span: Span, |
| ) -> (ty::PolyTraitPredicate<'tcx>, ty::PolyTraitPredicate<'tcx>, ty::BoundConstness) { |
| let trait_ref = p.to_poly_trait_ref(); |
| if !self.tcx.is_lang_item(trait_ref.def_id(), LangItem::EffectsCompat) { |
| return (p, leaf, ty::BoundConstness::NotConst); |
| } |
| let Some(ty::Alias(ty::AliasTyKind::Projection, projection)) = |
| trait_ref.self_ty().no_bound_vars().map(Ty::kind) |
| else { |
| return (p, leaf, ty::BoundConstness::NotConst); |
| }; |
| let constness = trait_ref.skip_binder().args.const_at(1); |
| let constness = if constness == self.tcx.consts.true_ | |
| ty::BoundConstness::NotConst |
| } else if constness == self.tcx.consts.false_ { |
| ty::BoundConstness::Const |
| } else if matches!(constness.kind(), ty::ConstKind::Param(_)) { |
| ty::BoundConstness::ConstIfConst |
| } else { |
| self.dcx().span_bug(span, format!("Unknown constness argument: {constness:?}")); |
| }; |
| let new_pred = p.map_bound(|mut trait_pred |
| trait_pred.trait_ref = projection.trait_ref(self.tcx); |
| trait_pred |
| }); |
| let new_leaf = leaf.map_bound(|mut trait_pred |
| trait_pred.trait_ref = projection.trait_ref(self.tcx); |
| trait_pred |
| }); |
| (new_pred, new_leaf, constness) |
| } |
I want to be clear that I don't think this is a issue of implementation quality or anything like that; I think it's simply a very clear sign that we're using types and traits in a way that they're not fundamentally supposed to be used, especially given that constness deserves to be represented as a first-class concept.
What now?
This PR implements a new desugaring for const traits. Specifically, it introduces a HostEffect predicate to represent the obligation an impl is const, rather than using associated type bounds and the compat trait that exists for effects today.
HostEffect predicate
A HostEffect clause has two parts -- the TraitRef we're trying to prove, and a HostPolarity::{Maybe, Const}.
HostPolarity::Const corresponds to T: const Trait bounds, which must always be proven as const, and which can be written in any context. These are lowered directly into the predicates of an item, since they're not "context-specific".
On the other hand, HostPolarity::Maybe corresponds to T: ~const Trait bounds which must only exist in a conditionally-const context like a method in a #[const_trait], or a const fn free function. We do not lower these immediately into the predicates of an item; instead, we collect them into a new query called the const_conditions. These are the set of trait refs that we need to prove have const implementations for an item to be const.
Notably, they're represented as bare (poly) trait refs because they are meant to be paired back together with a HostPolarity when they're being registered in typeck (see next section).
For example, given:
const fn foo<T: ~const A + const B>() {}
foo's const conditions would contain T: A, but not T: B. On the flip side, foo's predicates (predicates_of) query would contain HostEffect(T: B, HostPolarity::Const) but not HostEffect(T: A, HostPolarity::Maybe) since we don't need to prove that predicate in a non-const environment (and it's not even the right predicate to prove in an unconditionally const environment).
Type checking const bodies
When type checking bodies in HIR, when we encounter a call expression, we additionally register the callee item's const conditions with the HostPolarity from the body we're typechecking (Const for unconditionally const things like const/static items, and Maybe for conditionally const things like const fns; and we don't register HostPolarity predicates for non-const bodies).
When type-checking a conditionally const body, we augment its param-env with HostEffect(..., Maybe) predicates.
Checking that const impls are WF
We extend the logic in compare_method_predicate_entailment to also check the const-conditions of the impl method, to make sure that we error for:
#[const_trait] Bar {} #[const_trait] trait Foo { fn method<T: Bar>(); }
impl Foo for () { fn method<T: ~const Bar>() {} // stronger assumption! }
We also extend the WF check for impls to register the const conditions of the trait that is being implemented. This is to make sure we error for:
#[const_trait] trait Bar {} #[const_trait] trait Foo where T: ~const Bar {}
impl const Foo for () {}
//~^ T: ~const Bar is missing!
Proving a HostEffect predicate
We have several ways of proving a HostEffect predicate:
- Matching a
HostEffectpredicate from the param-env - From an impl - we do impl selection very similar to confirming a trait goal, except we filter for only const impls, and we additionally register the impl's const conditions (i.e. the impl's
~constwhere clauses).
Later I expect that we will add more built-in implementations for things like Fn.
What next?
After this PR, I'd like to split out the work more so it can proceed in parallel and probably amongst others that are not me.
- Register
HostEffectgoal for places in HIR typeck that correspond to call terminators, like autoderef. - Make traits in libstd const again.
- Probably need to impl host effect preds in old solver.
- Implement built-in
HostEffectrules for traits likeFn. - Rip out const checking from MIR altogether.
So what?
This ends up being super convenient basically everywhere in the compiler. Due to the design of the new trait solver, we end up having an almost parallel structure to the existing trait and projection predicates for assembling HostEffect predicates; adding new candidates and especially new built-in implementations is now basically trivial, and it's quite straightforward to understand the confirmation logic for these predicates.
Same with diagnostics reporting; since we have predicates which represent the obligation to prove an impl is const, we can simplify and make these diagnostics richer without having to write a ton of logic to intercept and rewrite the existing Compat trait errors.
Finally, it gives us a much more straightforward path for supporting the const effect on the old trait solver. I'm personally quite passionate about getting const trait support into the hands of users without having to wait until the new solver lands1, so I think after this PR lands we can begin to gauge how difficult it would be to implement constness in the old trait solver too. This PR will not do this yet.
Footnotes
- Though this is not a prerequisite or by any means the only justification for this PR. ↩