@@ -473,22 +473,12 @@ fn collect_items_rec<'tcx>(
473473 recursion_limit,
474474 ) ) ;
475475
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.
479476 // Plenty of code paths later assume that everything can be normalized.
480477 // 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) {
492482 let def_id = instance. def_id ( ) ;
493483 let def_span = tcx. def_span ( def_id) ;
494484 let def_path_str = tcx. def_path_str ( def_id) ;
@@ -630,6 +620,21 @@ fn collect_items_rec<'tcx>(
630620 }
631621}
632622
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+
633638fn check_recursion_limit < ' tcx > (
634639 tcx : TyCtxt < ' tcx > ,
635640 instance : Instance < ' tcx > ,
@@ -1784,4 +1789,5 @@ pub(crate) fn collect_crate_mono_items<'tcx>(
17841789pub ( crate ) fn provide ( providers : & mut Providers ) {
17851790 providers. hooks . should_codegen_locally = should_codegen_locally;
17861791 providers. items_of_instance = items_of_instance;
1792+ providers. has_normalization_error_in_mono = has_normalization_error_in_mono;
17871793}
0 commit comments