@@ -46,12 +46,12 @@ use rustc_abi::ExternAbi;
4646use rustc_ast as ast;
4747use rustc_ast:: node_id:: NodeMap ;
4848use rustc_ast:: * ;
49- use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap } ;
49+ use rustc_data_structures:: fx:: FxHashSet ;
5050use rustc_hir:: attrs:: { AttributeKind , InlineAttr } ;
51- use rustc_hir:: def_id:: { DefId , LocalDefId } ;
5251use rustc_hir:: { self as hir, FnDeclFlags } ;
5352use rustc_middle:: span_bug;
54- use rustc_middle:: ty:: { Asyncness , PerOwnerResolverData , TyCtxt } ;
53+ use rustc_middle:: ty:: { Asyncness , PerOwnerResolverData } ;
54+ use rustc_span:: def_id:: { DefId , LocalDefId } ;
5555use rustc_span:: symbol:: kw;
5656use rustc_span:: { ErrorGuaranteed , Ident , Span , Symbol } ;
5757
@@ -62,7 +62,6 @@ use crate::diagnostics::{
6262} ;
6363use crate :: {
6464 AllowReturnTypeNotation , ImplTraitContext , ImplTraitPosition , LoweringContext , ParamMode ,
65- index_crate,
6665} ;
6766
6867mod generics;
@@ -121,64 +120,6 @@ static ATTRS_ADDITIONS: &[AttrAdditionInfo] = &[
121120 } ,
122121] ;
123122
124- pub ( crate ) fn delegations_resolutions (
125- tcx : TyCtxt < ' _ > ,
126- _: ( ) ,
127- ) -> FxIndexMap < LocalDefId , Result < DefId , ErrorGuaranteed > > {
128- let krate = tcx. hir_crate ( ( ) ) ;
129-
130- let ( resolver, ast_crate) = & * krate. delayed_resolver . borrow ( ) ;
131-
132- // FIXME!!!(fn_delegation): make ast index lifetime same as resolver,
133- // as it is too bad to reindex whole crate on each delegation lowering.
134- let ast_index = index_crate ( resolver, ast_crate) ;
135-
136- let mut result = FxIndexMap :: < LocalDefId , Result < DefId , ErrorGuaranteed > > :: default ( ) ;
137-
138- for & def_id in & krate. delayed_ids {
139- let delegation = ast_index[ def_id] . delegation ( ) . expect ( "processing delegations" ) ;
140- let span = delegation. last_segment_span ( ) ;
141-
142- if let Some ( info) = tcx. resolutions ( ( ) ) . delegation_infos . get ( & def_id) {
143- let res = info. resolution_id . map ( |id| check_for_cycles ( tcx, id, span) . map ( |_| id) ) ;
144- result. insert ( def_id, res. flatten ( ) ) ;
145- } else {
146- tcx. dcx ( ) . span_delayed_bug (
147- span,
148- format ! ( "delegation resolution record was not found for {def_id:?}" ) ,
149- ) ;
150- }
151- }
152-
153- result
154- }
155-
156- fn check_for_cycles ( tcx : TyCtxt < ' _ > , mut def_id : DefId , span : Span ) -> Result < ( ) , ErrorGuaranteed > {
157- let mut visited: FxHashSet < DefId > = Default :: default ( ) ;
158-
159- loop {
160- visited. insert ( def_id) ;
161-
162- // If def_id is in local crate and it corresponds to another delegation
163- // it means that we refer to another delegation as a callee, so in order to obtain
164- // a signature DefId we obtain NodeId of the callee delegation and try to get signature from it.
165- if let Some ( local_id) = def_id. as_local ( )
166- && let Some ( info) = tcx. resolutions ( ( ) ) . delegation_infos . get ( & local_id)
167- && let Ok ( id) = info. resolution_id
168- {
169- def_id = id;
170- if visited. contains ( & def_id) {
171- return Err ( match visited. len ( ) {
172- 1 => tcx. dcx ( ) . emit_err ( UnresolvedDelegationCallee { span } ) ,
173- _ => tcx. dcx ( ) . emit_err ( CycleInDelegationSignatureResolution { span } ) ,
174- } ) ;
175- }
176- } else {
177- return Ok ( ( ) ) ;
178- }
179- }
180- }
181-
182123impl < ' hir > LoweringContext < ' _ , ' hir > {
183124 fn is_method ( & self , def_id : DefId , span : Span ) -> bool {
184125 match self . tcx . def_kind ( def_id) {
@@ -188,18 +129,53 @@ impl<'hir> LoweringContext<'_, 'hir> {
188129 }
189130 }
190131
132+ fn check_for_cycles ( & self , mut def_id : DefId , span : Span ) -> Result < ( ) , ErrorGuaranteed > {
133+ let mut visited: FxHashSet < DefId > = Default :: default ( ) ;
134+
135+ loop {
136+ visited. insert ( def_id) ;
137+
138+ // If def_id is in local crate and it corresponds to another delegation
139+ // it means that we refer to another delegation as a callee, so in order to obtain
140+ // a signature DefId we obtain NodeId of the callee delegation and try to get signature from it.
141+ if let Some ( local_id) = def_id. as_local ( )
142+ && let Some ( info) = self . tcx . resolutions ( ( ) ) . delegation_infos . get ( & local_id)
143+ && let Ok ( id) = info. resolution_id
144+ {
145+ def_id = id;
146+ if visited. contains ( & def_id) {
147+ return Err ( match visited. len ( ) {
148+ 1 => self . dcx ( ) . emit_err ( UnresolvedDelegationCallee { span } ) ,
149+ _ => self . dcx ( ) . emit_err ( CycleInDelegationSignatureResolution { span } ) ,
150+ } ) ;
151+ }
152+ } else {
153+ return Ok ( ( ) ) ;
154+ }
155+ }
156+ }
157+
191158 pub ( crate ) fn lower_delegation (
192159 & mut self ,
193160 delegation : & Delegation ,
194161 item_id : NodeId ,
195162 ) -> DelegationResults < ' hir > {
196163 let span = self . lower_span ( delegation. last_segment_span ( ) ) ;
197164
198- let sig_id = self . tcx . delegations_resolutions ( ( ) ) . get ( & self . owner . def_id ) . copied ( ) ;
165+ let Some ( info) = self . tcx . resolutions ( ( ) ) . delegation_infos . get ( & self . owner . def_id ) else {
166+ self . dcx ( ) . span_delayed_bug (
167+ span,
168+ format ! ( "delegation resolution record was not found for {:?}" , self . owner. def_id) ,
169+ ) ;
170+
171+ return self . generate_delegation_error ( span, delegation) ;
172+ } ;
173+
174+ let sig_id = info. resolution_id . and_then ( |id| self . check_for_cycles ( id, span) . map ( |_| id) ) ;
199175
200176 // Delegation can be missing from the `delegations_resolutions` table
201177 // in illegal places such as function bodies in extern blocks (see #151356).
202- let Some ( Ok ( sig_id) ) = sig_id else {
178+ let Ok ( sig_id) = sig_id else {
203179 self . dcx ( ) . span_delayed_bug (
204180 span,
205181 format ! ( "LoweringContext: the delegation {:?} is unresolved" , item_id) ,
0 commit comments