@@ -473,22 +473,12 @@ fn collect_items_rec<'tcx>(
473
473
recursion_limit,
474
474
) ) ;
475
475
476
- // Check whether the MIR body is malformed. Usually it's due to normalization overflow.
477
- // FIXME: I assume that there are few type errors at post-analysis stage, but not
478
- // entirely sure.
479
476
// Plenty of code paths later assume that everything can be normalized.
480
477
// Check normalization here to provide better diagnostics.
481
- let body = tcx. instance_mir ( instance. def ) ;
482
- let normalization_failed = body. local_decls . iter ( ) . any ( |local| {
483
- instance
484
- . try_instantiate_mir_and_normalize_erasing_regions (
485
- tcx,
486
- ty:: TypingEnv :: fully_monomorphized ( ) ,
487
- ty:: EarlyBinder :: bind ( local. ty ) ,
488
- )
489
- . is_err ( )
490
- } ) ;
491
- if normalization_failed {
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) {
492
482
let def_id = instance. def_id ( ) ;
493
483
let def_span = tcx. def_span ( def_id) ;
494
484
let def_path_str = tcx. def_path_str ( def_id) ;
@@ -630,6 +620,21 @@ fn collect_items_rec<'tcx>(
630
620
}
631
621
}
632
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
+ body. local_decls . iter ( ) . any ( |local| {
628
+ instance
629
+ . try_instantiate_mir_and_normalize_erasing_regions (
630
+ tcx,
631
+ ty:: TypingEnv :: fully_monomorphized ( ) ,
632
+ ty:: EarlyBinder :: bind ( local. ty ) ,
633
+ )
634
+ . is_err ( )
635
+ } )
636
+ }
637
+
633
638
fn check_recursion_limit < ' tcx > (
634
639
tcx : TyCtxt < ' tcx > ,
635
640
instance : Instance < ' tcx > ,
@@ -1784,4 +1789,5 @@ pub(crate) fn collect_crate_mono_items<'tcx>(
1784
1789
pub ( crate ) fn provide ( providers : & mut Providers ) {
1785
1790
providers. hooks . should_codegen_locally = should_codegen_locally;
1786
1791
providers. items_of_instance = items_of_instance;
1792
+ providers. has_normalization_error_in_mono = has_normalization_error_in_mono;
1787
1793
}
0 commit comments