@@ -19,8 +19,8 @@ use rustc_middle::ty::abstract_const::NotConstEvaluatable;
19
19
use rustc_middle:: ty:: error:: { ExpectedFound , TypeError } ;
20
20
use rustc_middle:: ty:: fold:: { TypeFolder , TypeSuperFoldable } ;
21
21
use 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,
24
24
} ;
25
25
use rustc_middle:: ty:: {
26
26
self , ToPolyTraitRef , TraitRef , Ty , TyCtxt , TypeFoldable , TypeVisitableExt , Upcast ,
@@ -154,6 +154,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
154
154
} else {
155
155
( leaf_trait_predicate, & obligation)
156
156
} ;
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
+
157
160
let main_trait_ref = main_trait_predicate. to_poly_trait_ref ( ) ;
158
161
let leaf_trait_ref = leaf_trait_predicate. to_poly_trait_ref ( ) ;
159
162
@@ -164,9 +167,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
164
167
return guar;
165
168
}
166
169
167
- // FIXME(effects)
168
- let predicate_is_const = false ;
169
-
170
170
if let Err ( guar) = leaf_trait_predicate. error_reported ( )
171
171
{
172
172
return guar;
@@ -227,7 +227,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
227
227
let err_msg = self . get_standard_error_message (
228
228
main_trait_predicate,
229
229
message,
230
- predicate_is_const ,
230
+ predicate_constness ,
231
231
append_const_msg,
232
232
post_message,
233
233
) ;
@@ -286,7 +286,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
286
286
}
287
287
288
288
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 )
290
290
{
291
291
err. note ( "`~const Drop` was renamed to `~const Destruct`" ) ;
292
292
err. note ( "See <https://github.com/rust-lang/rust/pull/94901> for more details" ) ;
@@ -2187,29 +2187,34 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
2187
2187
& self ,
2188
2188
trait_predicate : ty:: PolyTraitPredicate < ' tcx > ,
2189
2189
message : Option < String > ,
2190
- predicate_is_const : bool ,
2190
+ predicate_constness : ty :: BoundConstness ,
2191
2191
append_const_msg : Option < AppendConstMessage > ,
2192
2192
post_message : String ,
2193
2193
) -> String {
2194
2194
message
2195
2195
. and_then ( |cannot_do_this| {
2196
- match ( predicate_is_const , append_const_msg) {
2196
+ match ( predicate_constness , append_const_msg) {
2197
2197
// do nothing if predicate is not const
2198
- ( false , _) => Some ( cannot_do_this) ,
2198
+ ( ty :: BoundConstness :: NotConst , _) => Some ( cannot_do_this) ,
2199
2199
// 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" ) ) ,
2203
2204
// 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}" ) ) ,
2207
2209
// fallback to generic message
2208
- ( true , None ) => None ,
2210
+ ( ty :: BoundConstness :: Const | ty :: BoundConstness :: ConstIfConst , None ) => None ,
2209
2211
}
2210
2212
} )
2211
2213
. 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
+ )
2213
2218
} )
2214
2219
}
2215
2220
@@ -2333,6 +2338,51 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
2333
2338
}
2334
2339
}
2335
2340
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
+
2336
2386
fn add_tuple_trait_message (
2337
2387
& self ,
2338
2388
obligation_cause_code : & ObligationCauseCode < ' tcx > ,
0 commit comments