@@ -473,6 +473,23 @@ fn collect_items_rec<'tcx>(
473
473
recursion_limit,
474
474
) ) ;
475
475
476
+ // Plenty of code paths later assume that everything can be normalized.
477
+ // Check normalization here to provide better diagnostics.
478
+ // Normalization errors here are usually due to trait solving overflow.
479
+ // FIXME: I assume that there are few type errors at post-analysis stage, but not
480
+ // entirely sure.
481
+ if tcx. has_normalization_error_in_mono ( instance) {
482
+ let def_id = instance. def_id ( ) ;
483
+ let def_span = tcx. def_span ( def_id) ;
484
+ let def_path_str = tcx. def_path_str ( def_id) ;
485
+ tcx. dcx ( ) . emit_fatal ( RecursionLimit {
486
+ span : starting_item. span ,
487
+ instance,
488
+ def_span,
489
+ def_path_str,
490
+ } ) ;
491
+ }
492
+
476
493
rustc_data_structures:: stack:: ensure_sufficient_stack ( || {
477
494
let ( used, mentioned) = tcx. items_of_instance ( ( instance, mode) ) ;
478
495
used_items. extend ( used. into_iter ( ) . copied ( ) ) ;
@@ -603,6 +620,19 @@ fn collect_items_rec<'tcx>(
603
620
}
604
621
}
605
622
623
+ // Check whether we can normalize the MIR body. Make it a query since decoding MIR from disk cache
624
+ // may be expensive.
625
+ fn has_normalization_error_in_mono < ' tcx > ( tcx : TyCtxt < ' tcx > , instance : Instance < ' tcx > ) -> bool {
626
+ let body = tcx. instance_mir ( instance. def ) ;
627
+ instance
628
+ . try_instantiate_mir_and_normalize_erasing_regions (
629
+ tcx,
630
+ ty:: TypingEnv :: fully_monomorphized ( ) ,
631
+ ty:: EarlyBinder :: bind ( body) ,
632
+ )
633
+ . is_err ( )
634
+ }
635
+
606
636
fn check_recursion_limit < ' tcx > (
607
637
tcx : TyCtxt < ' tcx > ,
608
638
instance : Instance < ' tcx > ,
@@ -1770,4 +1800,5 @@ pub(crate) fn collect_crate_mono_items<'tcx>(
1770
1800
pub ( crate ) fn provide ( providers : & mut Providers ) {
1771
1801
providers. hooks . should_codegen_locally = should_codegen_locally;
1772
1802
providers. items_of_instance = items_of_instance;
1803
+ providers. has_normalization_error_in_mono = has_normalization_error_in_mono;
1773
1804
}
0 commit comments