@@ -43,14 +43,14 @@ use rustc_ast::visit::Visitor;
4343use rustc_ast:: { self as ast, * } ;
4444use rustc_attr_parsing:: { AttributeParser , OmitDoc , Recovery , ShouldEmit } ;
4545use rustc_data_structures:: fingerprint:: Fingerprint ;
46- use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap , FxIndexSet } ;
46+ use rustc_data_structures:: fx:: FxIndexSet ;
4747use rustc_data_structures:: sorted_map:: SortedMap ;
4848use rustc_data_structures:: stable_hash:: { StableHash , StableHasher } ;
4949use rustc_data_structures:: steal:: Steal ;
5050use rustc_data_structures:: tagged_ptr:: TaggedRef ;
5151use rustc_errors:: { DiagArgFromDisplay , DiagCtxtHandle } ;
5252use rustc_hir:: def:: { DefKind , LifetimeRes , Namespace , PartialRes , PerNS , Res } ;
53- use rustc_hir:: def_id:: { CRATE_DEF_ID , DefId , LOCAL_CRATE , LocalDefId } ;
53+ use rustc_hir:: def_id:: { CRATE_DEF_ID , LOCAL_CRATE , LocalDefId } ;
5454use rustc_hir:: definitions:: PerParentDisambiguatorState ;
5555use rustc_hir:: lints:: DelayedLint ;
5656use rustc_hir:: {
@@ -64,15 +64,12 @@ use rustc_middle::span_bug;
6464use rustc_middle:: ty:: { DelegationInfo , PerOwnerResolverData , ResolverAstLowering , TyCtxt } ;
6565use rustc_session:: errors:: add_feature_diagnostics;
6666use rustc_span:: symbol:: { Ident , Symbol , kw, sym} ;
67- use rustc_span:: { DUMMY_SP , DesugaringKind , Span } ;
67+ use rustc_span:: { DUMMY_SP , DesugaringKind , ErrorGuaranteed , Span } ;
6868use smallvec:: SmallVec ;
6969use thin_vec:: ThinVec ;
7070use tracing:: { debug, instrument, trace} ;
7171
72- use crate :: errors:: {
73- AssocTyParentheses , AssocTyParenthesesSub , CycleInDelegationSignatureResolution ,
74- MisplacedImplTrait , UnresolvedDelegationCallee ,
75- } ;
72+ use crate :: errors:: { AssocTyParentheses , AssocTyParenthesesSub , MisplacedImplTrait } ;
7673use crate :: item:: Owners ;
7774
7875macro_rules! arena_vec {
@@ -84,7 +81,7 @@ macro_rules! arena_vec {
8481mod asm;
8582mod block;
8683mod contract;
87- mod delegation;
84+ pub mod delegation;
8885mod errors;
8986mod expr;
9087mod format;
@@ -307,8 +304,8 @@ impl<'tcx> ResolverAstLowering<'tcx> {
307304 self . extra_lifetime_params_map . get ( & id) . map_or ( & [ ] , |v| & v[ ..] )
308305 }
309306
310- fn delegation_info ( & self , id : LocalDefId ) -> Option < & DelegationInfo > {
311- self . delegation_infos . get ( & id)
307+ fn delegation_info ( & self , id : LocalDefId ) -> Option < Result < DelegationInfo , ErrorGuaranteed > > {
308+ self . delegation_infos . get ( & id) . copied ( )
312309 }
313310
314311 fn owner_def_id ( & self , id : NodeId ) -> LocalDefId {
@@ -441,6 +438,16 @@ enum AstOwner<'a> {
441438 ForeignItem ( & ' a ast:: ForeignItem ) ,
442439}
443440
441+ impl AstOwner < ' _ > {
442+ fn delegation ( & self ) -> Option < & ast:: Delegation > {
443+ match self {
444+ AstOwner :: Item ( Item { kind : ItemKind :: Delegation ( d) , .. } )
445+ | AstOwner :: AssocItem ( Item { kind : AssocItemKind :: Delegation ( d) , .. } , _) => Some ( d) ,
446+ _ => None ,
447+ }
448+ }
449+ }
450+
444451#[ derive( Copy , Clone , Debug ) ]
445452enum TryBlockScope {
446453 /// There isn't a `try` block, so a `?` will use `return`.
@@ -544,7 +551,7 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> mid_hir::Crate<'_> {
544551 let mut delayed_ids: FxIndexSet < LocalDefId > = Default :: default ( ) ;
545552
546553 for def_id in ast_index. indices ( ) {
547- if opt_delegation ( & ast_index[ def_id] ) . is_some ( ) {
554+ if ast_index[ def_id] . delegation ( ) . is_some ( ) {
548555 delayed_ids. insert ( def_id) ;
549556 } else {
550557 lowerer. lower_node ( def_id) ;
@@ -559,16 +566,6 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> mid_hir::Crate<'_> {
559566 mid_hir:: Crate :: new ( owners, delayed_ids, delayed_resolver, opt_hir_hash)
560567}
561568
562- fn opt_delegation < ' a > ( ast_owner : & ' a AstOwner < ' a > ) -> Option < & ' a Delegation > {
563- match ast_owner {
564- AstOwner :: Item ( Item { kind : ItemKind :: Delegation ( d) , .. } )
565- | AstOwner :: AssocItem ( Item { kind : AssocItemKind :: Delegation ( d) , .. } , _) => {
566- Some ( d. as_ref ( ) )
567- }
568- _ => None ,
569- }
570- }
571-
572569/// Lowers an AST owner corresponding to `def_id`, now only delegations are lowered this way.
573570pub fn lower_delayed_owner ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) {
574571 let krate = tcx. hir_crate ( ( ) ) ;
@@ -594,65 +591,6 @@ pub fn lower_delayed_owner(tcx: TyCtxt<'_>, def_id: LocalDefId) {
594591 }
595592}
596593
597- pub fn delegations_resolutions ( tcx : TyCtxt < ' _ > , _: ( ) ) -> FxIndexMap < LocalDefId , Option < DefId > > {
598- let krate = tcx. hir_crate ( ( ) ) ;
599-
600- let ( resolver, ast_crate) = & * krate. delayed_resolver . borrow ( ) ;
601-
602- // FIXME!!!(fn_delegation): make ast index lifetime same as resolver,
603- // as it is too bad to reindex whole crate on each delegation lowering.
604- let ast_index = index_crate ( resolver, ast_crate) ;
605-
606- let mut result = FxIndexMap :: < LocalDefId , Option < DefId > > :: default ( ) ;
607-
608- for & def_id in & krate. delayed_ids {
609- let delegation = opt_delegation ( & ast_index[ def_id] ) . expect ( "processing delegations" ) ;
610- let span = delegation. span ( ) ;
611-
612- let res_id = resolver
613- . delegation_info ( def_id)
614- . and_then ( |info| resolve_delegation ( tcx, info. resolution_id , span) ) ;
615-
616- result. insert ( def_id, res_id) ;
617- }
618-
619- result
620- }
621-
622- fn resolve_delegation ( tcx : TyCtxt < ' _ > , mut def_id : DefId , span : Span ) -> Option < DefId > {
623- let mut visited: FxHashSet < DefId > = Default :: default ( ) ;
624- let mut path: SmallVec < [ DefId ; 1 ] > = Default :: default ( ) ;
625-
626- let ( resolver, _) = & * tcx. hir_crate ( ( ) ) . delayed_resolver . borrow ( ) ;
627-
628- loop {
629- visited. insert ( def_id) ;
630-
631- path. push ( def_id) ;
632-
633- // If def_id is in local crate and it corresponds to another delegation
634- // it means that we refer to another delegation as a callee, so in order to obtain
635- // a signature DefId we obtain NodeId of the callee delegation and try to get signature from it.
636- if let Some ( local_id) = def_id. as_local ( )
637- && let Some ( delegation_info) = resolver. delegation_info ( local_id)
638- {
639- def_id = delegation_info. resolution_id ;
640- if visited. contains ( & def_id) {
641- // We encountered a cycle in the resolution, or delegation callee refers to non-existent
642- // entity, in this case emit an error.
643- match visited. len ( ) {
644- 1 => tcx. dcx ( ) . emit_err ( UnresolvedDelegationCallee { span } ) ,
645- _ => tcx. dcx ( ) . emit_err ( CycleInDelegationSignatureResolution { span } ) ,
646- } ;
647-
648- return None ;
649- }
650- } else {
651- return Some ( path[ 0 ] ) ;
652- }
653- }
654- }
655-
656594#[ derive( Copy , Clone , PartialEq , Debug ) ]
657595enum ParamMode {
658596 /// Any path in a type context.
0 commit comments