@@ -55,25 +55,26 @@ use super::{
5555
5656pub use rustc_infer:: traits:: error_reporting:: * ;
5757
58+ pub enum OverflowCause < ' tcx > {
59+ DeeplyNormalize ( ty:: AliasTy < ' tcx > ) ,
60+ TraitSolver ( ty:: Predicate < ' tcx > ) ,
61+ }
62+
5863pub trait TypeErrCtxtExt < ' tcx > {
59- fn build_overflow_error < T > (
64+ fn build_overflow_error (
6065 & self ,
61- predicate : & T ,
66+ cause : OverflowCause < ' tcx > ,
6267 span : Span ,
6368 suggest_increasing_limit : bool ,
64- ) -> DiagnosticBuilder < ' tcx >
65- where
66- T : fmt:: Display + TypeFoldable < TyCtxt < ' tcx > > + Print < ' tcx , FmtPrinter < ' tcx , ' tcx > > ;
69+ ) -> DiagnosticBuilder < ' tcx > ;
6770
68- fn report_overflow_error < T > (
71+ fn report_overflow_error (
6972 & self ,
70- predicate : & T ,
73+ cause : OverflowCause < ' tcx > ,
7174 span : Span ,
7275 suggest_increasing_limit : bool ,
7376 mutate : impl FnOnce ( & mut Diagnostic ) ,
74- ) -> !
75- where
76- T : fmt:: Display + TypeFoldable < TyCtxt < ' tcx > > + Print < ' tcx , FmtPrinter < ' tcx , ' tcx > > ;
77+ ) -> !;
7778
7879 fn report_overflow_no_abort ( & self , obligation : PredicateObligation < ' tcx > ) -> ErrorGuaranteed ;
7980
@@ -241,17 +242,14 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
241242 /// whose result could not be truly determined and thus we can't say
242243 /// if the program type checks or not -- and they are unusual
243244 /// occurrences in any case.
244- fn report_overflow_error < T > (
245+ fn report_overflow_error (
245246 & self ,
246- predicate : & T ,
247+ cause : OverflowCause < ' tcx > ,
247248 span : Span ,
248249 suggest_increasing_limit : bool ,
249250 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) ;
255253 mutate ( & mut err) ;
256254 err. emit ( ) ;
257255
@@ -264,33 +262,52 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
264262 ) ;
265263 }
266264
267- fn build_overflow_error < T > (
265+ fn build_overflow_error (
268266 & self ,
269- predicate : & T ,
267+ cause : OverflowCause < ' tcx > ,
270268 span : Span ,
271269 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+ }
286286 }
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+ } ;
294311
295312 if suggest_increasing_limit {
296313 self . suggest_new_overflow_limit ( & mut err) ;
@@ -316,7 +333,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
316333 let predicate = obligation. predicate . clone ( ) . to_predicate ( self . tcx ) ;
317334 let predicate = self . resolve_vars_if_possible ( predicate) ;
318335 self . report_overflow_error (
319- & predicate,
336+ OverflowCause :: TraitSolver ( predicate) ,
320337 obligation. cause . span ,
321338 suggest_increasing_limit,
322339 |err| {
@@ -367,7 +384,11 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
367384
368385 fn report_overflow_no_abort ( & self , obligation : PredicateObligation < ' tcx > ) -> ErrorGuaranteed {
369386 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+ ) ;
371392 self . note_obligation_cause ( & mut err, & obligation) ;
372393 self . point_at_returns_when_relevant ( & mut err, & obligation) ;
373394 err. emit ( )
0 commit comments