@@ -6,7 +6,6 @@ use crate::{mir, ty};
6
6
use std:: fmt:: Write ;
7
7
8
8
use rustc_data_structures:: fx:: { FxHashMap , FxIndexMap } ;
9
- use rustc_hir:: def:: DefKind ;
10
9
use rustc_hir:: def_id:: { DefId , LocalDefId } ;
11
10
use rustc_hir:: { self as hir, LangItem } ;
12
11
use rustc_span:: symbol:: Ident ;
@@ -234,14 +233,39 @@ impl<'tcx> CapturedPlace<'tcx> {
234
233
}
235
234
}
236
235
237
- fn closure_captures < ' tcx > (
238
- tcx : TyCtxt < ' tcx > ,
239
- def : LocalDefId ,
240
- ) -> & ' tcx [ & ' tcx ty:: CapturedPlace < ' tcx > ] {
241
- let ( DefKind :: Closure | DefKind :: Generator ) = tcx. def_kind ( def) else { return & [ ] } ;
236
+ #[ derive( Copy , Clone , Debug , HashStable ) ]
237
+ pub struct ClosureTypeInfo < ' tcx > {
238
+ user_provided_sig : ty:: CanonicalPolyFnSig < ' tcx > ,
239
+ captures : & ' tcx [ & ' tcx ty:: CapturedPlace < ' tcx > ] ,
240
+ kind_origin : Option < & ' tcx ( Span , HirPlace < ' tcx > ) > ,
241
+ }
242
+
243
+ fn closure_typeinfo < ' tcx > ( tcx : TyCtxt < ' tcx > , def : LocalDefId ) -> ClosureTypeInfo < ' tcx > {
244
+ debug_assert ! ( tcx. is_closure( def. to_def_id( ) ) ) ;
242
245
let typeck_results = tcx. typeck ( def) ;
246
+ let user_provided_sig = typeck_results. user_provided_sigs [ & def] ;
243
247
let captures = typeck_results. closure_min_captures_flattened ( def) ;
244
- tcx. arena . alloc_from_iter ( captures)
248
+ let captures = tcx. arena . alloc_from_iter ( captures) ;
249
+ let hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def) ;
250
+ let kind_origin = typeck_results. closure_kind_origins ( ) . get ( hir_id) ;
251
+ ClosureTypeInfo { user_provided_sig, captures, kind_origin }
252
+ }
253
+
254
+ impl < ' tcx > TyCtxt < ' tcx > {
255
+ pub fn closure_kind_origin ( self , def_id : LocalDefId ) -> Option < & ' tcx ( Span , HirPlace < ' tcx > ) > {
256
+ self . closure_typeinfo ( def_id) . kind_origin
257
+ }
258
+
259
+ pub fn closure_user_provided_sig ( self , def_id : LocalDefId ) -> ty:: CanonicalPolyFnSig < ' tcx > {
260
+ self . closure_typeinfo ( def_id) . user_provided_sig
261
+ }
262
+
263
+ pub fn closure_captures ( self , def_id : LocalDefId ) -> & ' tcx [ & ' tcx ty:: CapturedPlace < ' tcx > ] {
264
+ if !self . is_closure ( def_id. to_def_id ( ) ) {
265
+ return & [ ] ;
266
+ } ;
267
+ self . closure_typeinfo ( def_id) . captures
268
+ }
245
269
}
246
270
247
271
/// Return true if the `proj_possible_ancestor` represents an ancestor path
@@ -434,5 +458,5 @@ impl BorrowKind {
434
458
}
435
459
436
460
pub fn provide ( providers : & mut ty:: query:: Providers ) {
437
- * providers = ty:: query:: Providers { closure_captures , ..* providers }
461
+ * providers = ty:: query:: Providers { closure_typeinfo , ..* providers }
438
462
}
0 commit comments