33
44use rustc_hir:: LangItem ;
55use rustc_middle:: ty:: { self , ToPredicate , Ty , TyCtxt } ;
6- use rustc_span:: Span ;
6+ use rustc_span:: { def_id :: DefId , Span } ;
77
88/// Collects together a list of type bounds. These lists of bounds occur in many places
99/// in Rust's syntax:
@@ -40,19 +40,11 @@ impl<'tcx> Bounds<'tcx> {
4040 pub fn push_trait_bound (
4141 & mut self ,
4242 tcx : TyCtxt < ' tcx > ,
43+ defining_def_id : DefId ,
4344 trait_ref : ty:: PolyTraitRef < ' tcx > ,
4445 span : Span ,
4546 polarity : ty:: ImplPolarity ,
46- ) {
47- self . push_trait_bound_inner ( tcx, trait_ref, span, polarity) ;
48- }
49-
50- fn push_trait_bound_inner (
51- & mut self ,
52- tcx : TyCtxt < ' tcx > ,
53- trait_ref : ty:: PolyTraitRef < ' tcx > ,
54- span : Span ,
55- polarity : ty:: ImplPolarity ,
47+ constness : ty:: BoundConstness ,
5648 ) {
5749 self . clauses . push ( (
5850 trait_ref
@@ -62,6 +54,28 @@ impl<'tcx> Bounds<'tcx> {
6254 . to_predicate ( tcx) ,
6355 span,
6456 ) ) ;
57+ // For `T: ~const Tr` or `T: const Tr`, we need to add an additional bound on the
58+ // associated type of `<T as Tr>` and make sure that the effect is compatible.
59+ if let Some ( compat_val) = match constness {
60+ // TODO: do we need `T: const Trait` anymore?
61+ ty:: BoundConstness :: Const => Some ( tcx. consts . false_ ) ,
62+ ty:: BoundConstness :: ConstIfConst => {
63+ Some ( tcx. expected_host_effect_param_for_body ( defining_def_id) )
64+ }
65+ ty:: BoundConstness :: NotConst => None ,
66+ } {
67+ // create a new projection type `<T as Tr>::Effects`
68+ let assoc = tcx. associated_type_for_effects ( trait_ref. def_id ( ) ) . unwrap ( ) ;
69+ let self_ty = Ty :: new_projection ( tcx, assoc, trait_ref. skip_binder ( ) . args ) ;
70+ // make `<T as Tr>::Effects: Compat<runtime>`
71+ let new_trait_ref = ty:: TraitRef :: new ( tcx, tcx. require_lang_item ( LangItem :: EffectsCompat , Some ( span) ) , [ ty:: GenericArg :: from ( self_ty) , compat_val. into ( ) ] ) ;
72+ self . clauses . push ( (
73+ trait_ref
74+ . rebind ( new_trait_ref)
75+ . to_predicate ( tcx) ,
76+ span,
77+ ) ) ;
78+ }
6579 }
6680
6781 pub fn push_projection_bound (
0 commit comments