@@ -19,8 +19,8 @@ use rustc_middle::ty::abstract_const::NotConstEvaluatable;
1919use rustc_middle:: ty:: error:: { ExpectedFound , TypeError } ;
2020use rustc_middle:: ty:: fold:: { TypeFolder , TypeSuperFoldable } ;
2121use rustc_middle:: ty:: print:: {
22- FmtPrinter , Print , PrintTraitPredicateExt as _ , PrintTraitRefExt as _,
23- with_forced_trimmed_paths,
22+ FmtPrinter , Print , PrintPolyTraitPredicateExt , PrintTraitPredicateExt as _,
23+ PrintTraitRefExt as _ , with_forced_trimmed_paths,
2424} ;
2525use rustc_middle:: ty:: {
2626 self , ToPolyTraitRef , TraitRef , Ty , TyCtxt , TypeFoldable , TypeVisitableExt , Upcast ,
@@ -154,6 +154,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
154154 } else {
155155 ( leaf_trait_predicate, & obligation)
156156 } ;
157+
158+ let ( main_trait_predicate, leaf_trait_predicate, predicate_constness) = self . get_effects_trait_pred_override ( main_trait_predicate, leaf_trait_predicate, span) ;
159+
157160 let main_trait_ref = main_trait_predicate. to_poly_trait_ref ( ) ;
158161 let leaf_trait_ref = leaf_trait_predicate. to_poly_trait_ref ( ) ;
159162
@@ -164,9 +167,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
164167 return guar;
165168 }
166169
167- // FIXME(effects)
168- let predicate_is_const = false ;
169-
170170 if let Err ( guar) = leaf_trait_predicate. error_reported ( )
171171 {
172172 return guar;
@@ -227,7 +227,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
227227 let err_msg = self . get_standard_error_message (
228228 main_trait_predicate,
229229 message,
230- predicate_is_const ,
230+ predicate_constness ,
231231 append_const_msg,
232232 post_message,
233233 ) ;
@@ -286,7 +286,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
286286 }
287287
288288 if tcx. is_lang_item ( leaf_trait_ref. def_id ( ) , LangItem :: Drop )
289- && predicate_is_const
289+ && matches ! ( predicate_constness , ty :: BoundConstness :: ConstIfConst | ty :: BoundConstness :: Const )
290290 {
291291 err. note ( "`~const Drop` was renamed to `~const Destruct`" ) ;
292292 err. note ( "See <https://github.com/rust-lang/rust/pull/94901> for more details" ) ;
@@ -2187,29 +2187,34 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
21872187 & self ,
21882188 trait_predicate : ty:: PolyTraitPredicate < ' tcx > ,
21892189 message : Option < String > ,
2190- predicate_is_const : bool ,
2190+ predicate_constness : ty :: BoundConstness ,
21912191 append_const_msg : Option < AppendConstMessage > ,
21922192 post_message : String ,
21932193 ) -> String {
21942194 message
21952195 . and_then ( |cannot_do_this| {
2196- match ( predicate_is_const , append_const_msg) {
2196+ match ( predicate_constness , append_const_msg) {
21972197 // do nothing if predicate is not const
2198- ( false , _) => Some ( cannot_do_this) ,
2198+ ( ty :: BoundConstness :: NotConst , _) => Some ( cannot_do_this) ,
21992199 // suggested using default post message
2200- ( true , Some ( AppendConstMessage :: Default ) ) => {
2201- Some ( format ! ( "{cannot_do_this} in const contexts" ) )
2202- }
2200+ (
2201+ ty:: BoundConstness :: Const | ty:: BoundConstness :: ConstIfConst ,
2202+ Some ( AppendConstMessage :: Default ) ,
2203+ ) => Some ( format ! ( "{cannot_do_this} in const contexts" ) ) ,
22032204 // overridden post message
2204- ( true , Some ( AppendConstMessage :: Custom ( custom_msg, _) ) ) => {
2205- Some ( format ! ( "{cannot_do_this}{custom_msg}" ) )
2206- }
2205+ (
2206+ ty:: BoundConstness :: Const | ty:: BoundConstness :: ConstIfConst ,
2207+ Some ( AppendConstMessage :: Custom ( custom_msg, _) ) ,
2208+ ) => Some ( format ! ( "{cannot_do_this}{custom_msg}" ) ) ,
22072209 // fallback to generic message
2208- ( true , None ) => None ,
2210+ ( ty :: BoundConstness :: Const | ty :: BoundConstness :: ConstIfConst , None ) => None ,
22092211 }
22102212 } )
22112213 . unwrap_or_else ( || {
2212- format ! ( "the trait bound `{trait_predicate}` is not satisfied{post_message}" )
2214+ format ! (
2215+ "the trait bound `{}` is not satisfied{post_message}" ,
2216+ trait_predicate. print_with_bound_constness( predicate_constness)
2217+ )
22132218 } )
22142219 }
22152220
@@ -2333,6 +2338,51 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
23332338 }
23342339 }
23352340
2341+ /// For effects predicates such as `<u32 as Add>::Effects: Compat<host>`, pretend that the
2342+ /// predicate that failed was `u32: Add`. Return the constness of such predicate to later
2343+ /// print as `u32: ~const Add`.
2344+ fn get_effects_trait_pred_override (
2345+ & self ,
2346+ p : ty:: PolyTraitPredicate < ' tcx > ,
2347+ leaf : ty:: PolyTraitPredicate < ' tcx > ,
2348+ span : Span ,
2349+ ) -> ( ty:: PolyTraitPredicate < ' tcx > , ty:: PolyTraitPredicate < ' tcx > , ty:: BoundConstness ) {
2350+ let trait_ref = p. to_poly_trait_ref ( ) ;
2351+ if !self . tcx . is_lang_item ( trait_ref. def_id ( ) , LangItem :: EffectsCompat ) {
2352+ return ( p, leaf, ty:: BoundConstness :: NotConst ) ;
2353+ }
2354+
2355+ let Some ( ty:: Alias ( ty:: AliasTyKind :: Projection , projection) ) =
2356+ trait_ref. self_ty ( ) . no_bound_vars ( ) . map ( Ty :: kind)
2357+ else {
2358+ return ( p, leaf, ty:: BoundConstness :: NotConst ) ;
2359+ } ;
2360+
2361+ let constness = trait_ref. skip_binder ( ) . args . const_at ( 1 ) ;
2362+
2363+ let constness = if constness == self . tcx . consts . true_ || constness. is_ct_infer ( ) {
2364+ ty:: BoundConstness :: NotConst
2365+ } else if constness == self . tcx . consts . false_ {
2366+ ty:: BoundConstness :: Const
2367+ } else if matches ! ( constness. kind( ) , ty:: ConstKind :: Param ( _) ) {
2368+ ty:: BoundConstness :: ConstIfConst
2369+ } else {
2370+ self . dcx ( ) . span_bug ( span, format ! ( "Unknown constness argument: {constness:?}" ) ) ;
2371+ } ;
2372+
2373+ let new_pred = p. map_bound ( |mut trait_pred| {
2374+ trait_pred. trait_ref = projection. trait_ref ( self . tcx ) ;
2375+ trait_pred
2376+ } ) ;
2377+
2378+ let new_leaf = leaf. map_bound ( |mut trait_pred| {
2379+ trait_pred. trait_ref = projection. trait_ref ( self . tcx ) ;
2380+ trait_pred
2381+ } ) ;
2382+
2383+ ( new_pred, new_leaf, constness)
2384+ }
2385+
23362386 fn add_tuple_trait_message (
23372387 & self ,
23382388 obligation_cause_code : & ObligationCauseCode < ' tcx > ,
0 commit comments