@@ -233,7 +233,7 @@ use rustc_middle::{bug, span_bug};
233233use rustc_session:: Limit ;
234234use rustc_session:: config:: EntryFnType ;
235235use rustc_span:: source_map:: { Spanned , dummy_spanned, respan} ;
236- use rustc_span:: { DUMMY_SP , Span } ;
236+ use rustc_span:: { DUMMY_SP , ErrorGuaranteed , Span } ;
237237use tracing:: { debug, instrument, trace} ;
238238
239239use crate :: errors:: { self , EncounteredErrorWhileInstantiating , NoOptimizedMir , RecursionLimit } ;
@@ -466,9 +466,11 @@ fn collect_items_rec<'tcx>(
466466 ) ) ;
467467
468468 rustc_data_structures:: stack:: ensure_sufficient_stack ( || {
469- let ( used, mentioned) = tcx. items_of_instance ( ( instance, mode) ) ;
470- used_items. extend ( used. into_iter ( ) . copied ( ) ) ;
471- mentioned_items. extend ( mentioned. into_iter ( ) . copied ( ) ) ;
469+ // FIXME: we could handle the `ErrorGuaranteed` generated by `tcx.eval_instance`.
470+ if let Ok ( ( used, mentioned) ) = tcx. items_of_instance ( ( instance, mode) ) {
471+ used_items. extend ( used. into_iter ( ) . copied ( ) ) ;
472+ mentioned_items. extend ( mentioned. into_iter ( ) . copied ( ) ) ;
473+ } ;
472474 } ) ;
473475 }
474476 MonoItem :: GlobalAsm ( item_id) => {
@@ -626,6 +628,8 @@ struct MirUsedCollector<'a, 'tcx> {
626628 /// Note that this contains *not-monomorphized* items!
627629 used_mentioned_items : & ' a mut UnordSet < MentionedItem < ' tcx > > ,
628630 instance : Instance < ' tcx > ,
631+ /// If an error is encountered during const evaluation.
632+ tained_by_errors : Option < ErrorGuaranteed > ,
629633}
630634
631635impl < ' a , ' tcx > MirUsedCollector < ' a , ' tcx > {
@@ -658,9 +662,10 @@ impl<'a, 'tcx> MirUsedCollector<'a, 'tcx> {
658662 "collection encountered polymorphic constant: {:?}" ,
659663 const_
660664 ) ,
661- Err ( err @ ErrorHandled :: Reported ( .. ) ) => {
665+ Err ( err @ ErrorHandled :: Reported ( reported , _ ) ) => {
662666 err. emit_note ( self . tcx ) ;
663- return None ;
667+ self . tained_by_errors = Some ( reported. into ( ) ) ;
668+ None
664669 }
665670 }
666671 }
@@ -1211,7 +1216,7 @@ fn collect_items_of_instance<'tcx>(
12111216 tcx : TyCtxt < ' tcx > ,
12121217 instance : Instance < ' tcx > ,
12131218 mode : CollectionMode ,
1214- ) -> ( MonoItems < ' tcx > , MonoItems < ' tcx > ) {
1219+ ) -> Result < ( MonoItems < ' tcx > , MonoItems < ' tcx > ) , ErrorGuaranteed > {
12151220 // This item is getting monomorphized, do mono-time checks.
12161221 tcx. ensure ( ) . check_mono_item ( instance) ;
12171222
@@ -1235,6 +1240,7 @@ fn collect_items_of_instance<'tcx>(
12351240 used_items : & mut used_items,
12361241 used_mentioned_items : & mut used_mentioned_items,
12371242 instance,
1243+ tained_by_errors : None ,
12381244 } ;
12391245
12401246 if mode == CollectionMode :: UsedItems {
@@ -1260,19 +1266,23 @@ fn collect_items_of_instance<'tcx>(
12601266 }
12611267 }
12621268
1263- ( used_items, mentioned_items)
1269+ if let Some ( err) = collector. tained_by_errors {
1270+ return Err ( err) ;
1271+ }
1272+
1273+ Ok ( ( used_items, mentioned_items) )
12641274}
12651275
12661276fn items_of_instance < ' tcx > (
12671277 tcx : TyCtxt < ' tcx > ,
12681278 ( instance, mode) : ( Instance < ' tcx > , CollectionMode ) ,
1269- ) -> ( & ' tcx [ Spanned < MonoItem < ' tcx > > ] , & ' tcx [ Spanned < MonoItem < ' tcx > > ] ) {
1270- let ( used_items, mentioned_items) = collect_items_of_instance ( tcx, instance, mode) ;
1279+ ) -> Result < ( & ' tcx [ Spanned < MonoItem < ' tcx > > ] , & ' tcx [ Spanned < MonoItem < ' tcx > > ] ) , ErrorGuaranteed > {
1280+ let ( used_items, mentioned_items) = collect_items_of_instance ( tcx, instance, mode) ? ;
12711281
12721282 let used_items = tcx. arena . alloc_from_iter ( used_items) ;
12731283 let mentioned_items = tcx. arena . alloc_from_iter ( mentioned_items) ;
12741284
1275- ( used_items, mentioned_items)
1285+ Ok ( ( used_items, mentioned_items) )
12761286}
12771287
12781288/// `item` must be already monomorphized.
0 commit comments