@@ -55,25 +55,26 @@ use super::{
55
55
56
56
pub use rustc_infer:: traits:: error_reporting:: * ;
57
57
58
+ pub enum OverflowCause < ' tcx > {
59
+ DeeplyNormalize ( ty:: AliasTy < ' tcx > ) ,
60
+ TraitSolver ( ty:: Predicate < ' tcx > ) ,
61
+ }
62
+
58
63
pub trait TypeErrCtxtExt < ' tcx > {
59
- fn build_overflow_error < T > (
64
+ fn build_overflow_error (
60
65
& self ,
61
- predicate : & T ,
66
+ cause : OverflowCause < ' tcx > ,
62
67
span : Span ,
63
68
suggest_increasing_limit : bool ,
64
- ) -> DiagnosticBuilder < ' tcx >
65
- where
66
- T : fmt:: Display + TypeFoldable < TyCtxt < ' tcx > > + Print < ' tcx , FmtPrinter < ' tcx , ' tcx > > ;
69
+ ) -> DiagnosticBuilder < ' tcx > ;
67
70
68
- fn report_overflow_error < T > (
71
+ fn report_overflow_error (
69
72
& self ,
70
- predicate : & T ,
73
+ cause : OverflowCause < ' tcx > ,
71
74
span : Span ,
72
75
suggest_increasing_limit : bool ,
73
76
mutate : impl FnOnce ( & mut Diagnostic ) ,
74
- ) -> !
75
- where
76
- T : fmt:: Display + TypeFoldable < TyCtxt < ' tcx > > + Print < ' tcx , FmtPrinter < ' tcx , ' tcx > > ;
77
+ ) -> !;
77
78
78
79
fn report_overflow_no_abort ( & self , obligation : PredicateObligation < ' tcx > ) -> ErrorGuaranteed ;
79
80
@@ -241,17 +242,14 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
241
242
/// whose result could not be truly determined and thus we can't say
242
243
/// if the program type checks or not -- and they are unusual
243
244
/// occurrences in any case.
244
- fn report_overflow_error < T > (
245
+ fn report_overflow_error (
245
246
& self ,
246
- predicate : & T ,
247
+ cause : OverflowCause < ' tcx > ,
247
248
span : Span ,
248
249
suggest_increasing_limit : bool ,
249
250
mutate : impl FnOnce ( & mut Diagnostic ) ,
250
- ) -> !
251
- where
252
- T : fmt:: Display + TypeFoldable < TyCtxt < ' tcx > > + Print < ' tcx , FmtPrinter < ' tcx , ' tcx > > ,
253
- {
254
- let mut err = self . build_overflow_error ( predicate, span, suggest_increasing_limit) ;
251
+ ) -> ! {
252
+ let mut err = self . build_overflow_error ( cause, span, suggest_increasing_limit) ;
255
253
mutate ( & mut err) ;
256
254
err. emit ( ) ;
257
255
@@ -264,33 +262,52 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
264
262
) ;
265
263
}
266
264
267
- fn build_overflow_error < T > (
265
+ fn build_overflow_error (
268
266
& self ,
269
- predicate : & T ,
267
+ cause : OverflowCause < ' tcx > ,
270
268
span : Span ,
271
269
suggest_increasing_limit : bool ,
272
- ) -> DiagnosticBuilder < ' tcx >
273
- where
274
- T : fmt:: Display + TypeFoldable < TyCtxt < ' tcx > > + Print < ' tcx , FmtPrinter < ' tcx , ' tcx > > ,
275
- {
276
- let predicate = self . resolve_vars_if_possible ( predicate. clone ( ) ) ;
277
- let mut pred_str = predicate. to_string ( ) ;
278
-
279
- if pred_str. len ( ) > 50 {
280
- // We don't need to save the type to a file, we will be talking about this type already
281
- // in a separate note when we explain the obligation, so it will be available that way.
282
- let mut cx: FmtPrinter < ' _ , ' _ > =
283
- FmtPrinter :: new_with_limit ( self . tcx , Namespace :: TypeNS , rustc_session:: Limit ( 6 ) ) ;
284
- predicate. print ( & mut cx) . unwrap ( ) ;
285
- pred_str = cx. into_buffer ( ) ;
270
+ ) -> DiagnosticBuilder < ' tcx > {
271
+ fn with_short_path < ' tcx , T > ( tcx : TyCtxt < ' tcx > , value : T ) -> String
272
+ where
273
+ T : fmt:: Display + Print < ' tcx , FmtPrinter < ' tcx , ' tcx > > ,
274
+ {
275
+ let s = value. to_string ( ) ;
276
+ if s. len ( ) > 50 {
277
+ // We don't need to save the type to a file, we will be talking about this type already
278
+ // in a separate note when we explain the obligation, so it will be available that way.
279
+ let mut cx: FmtPrinter < ' _ , ' _ > =
280
+ FmtPrinter :: new_with_limit ( tcx, Namespace :: TypeNS , rustc_session:: Limit ( 6 ) ) ;
281
+ value. print ( & mut cx) . unwrap ( ) ;
282
+ cx. into_buffer ( )
283
+ } else {
284
+ s
285
+ }
286
286
}
287
- let mut err = struct_span_code_err ! (
288
- self . dcx( ) ,
289
- span,
290
- E0275 ,
291
- "overflow evaluating the requirement `{}`" ,
292
- pred_str,
293
- ) ;
287
+
288
+ let mut err = match cause {
289
+ OverflowCause :: DeeplyNormalize ( alias_ty) => {
290
+ let alias_ty = self . resolve_vars_if_possible ( alias_ty) ;
291
+ let kind = alias_ty. opt_kind ( self . tcx ) . map_or ( "alias" , |k| k. descr ( ) ) ;
292
+ let alias_str = with_short_path ( self . tcx , alias_ty) ;
293
+ struct_span_code_err ! (
294
+ self . dcx( ) ,
295
+ span,
296
+ E0275 ,
297
+ "overflow normalizing the {kind} `{alias_str}`" ,
298
+ )
299
+ }
300
+ OverflowCause :: TraitSolver ( predicate) => {
301
+ let predicate = self . resolve_vars_if_possible ( predicate) ;
302
+ let pred_str = with_short_path ( self . tcx , predicate) ;
303
+ struct_span_code_err ! (
304
+ self . dcx( ) ,
305
+ span,
306
+ E0275 ,
307
+ "overflow evaluating the requirement `{pred_str}`" ,
308
+ )
309
+ }
310
+ } ;
294
311
295
312
if suggest_increasing_limit {
296
313
self . suggest_new_overflow_limit ( & mut err) ;
@@ -316,7 +333,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
316
333
let predicate = obligation. predicate . clone ( ) . to_predicate ( self . tcx ) ;
317
334
let predicate = self . resolve_vars_if_possible ( predicate) ;
318
335
self . report_overflow_error (
319
- & predicate,
336
+ OverflowCause :: TraitSolver ( predicate) ,
320
337
obligation. cause . span ,
321
338
suggest_increasing_limit,
322
339
|err| {
@@ -367,7 +384,11 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
367
384
368
385
fn report_overflow_no_abort ( & self , obligation : PredicateObligation < ' tcx > ) -> ErrorGuaranteed {
369
386
let obligation = self . resolve_vars_if_possible ( obligation) ;
370
- let mut err = self . build_overflow_error ( & obligation. predicate , obligation. cause . span , true ) ;
387
+ let mut err = self . build_overflow_error (
388
+ OverflowCause :: TraitSolver ( obligation. predicate ) ,
389
+ obligation. cause . span ,
390
+ true ,
391
+ ) ;
371
392
self . note_obligation_cause ( & mut err, & obligation) ;
372
393
self . point_at_returns_when_relevant ( & mut err, & obligation) ;
373
394
err. emit ( )
0 commit comments