@@ -217,6 +217,10 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
217
217
ty:: ClosureKind :: FnOnce => { }
218
218
}
219
219
220
+ // We won't be building MIR if the closure wasn't local
221
+ let closure_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( closure_def_id. expect_local ( ) ) ;
222
+ let closure_span = tcx. hir ( ) . span ( closure_hir_id) ;
223
+
220
224
let ( capture_index, capture) = if let Some ( capture_details) =
221
225
find_capture_matching_projections (
222
226
typeck_results,
@@ -226,7 +230,7 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
226
230
) {
227
231
capture_details
228
232
} else {
229
- if !tcx . features ( ) . capture_disjoint_fields {
233
+ if !enable_precise_capture ( tcx , closure_span ) {
230
234
bug ! (
231
235
"No associated capture found for {:?}[{:#?}] even though \
232
236
capture_disjoint_fields isn't enabled",
@@ -242,8 +246,7 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
242
246
return Err ( from_builder) ;
243
247
} ;
244
248
245
- let closure_ty = typeck_results
246
- . node_type ( tcx. hir ( ) . local_def_id_to_hir_id ( closure_def_id. expect_local ( ) ) ) ;
249
+ let closure_ty = typeck_results. node_type ( closure_hir_id) ;
247
250
248
251
let substs = match closure_ty. kind ( ) {
249
252
ty:: Closure ( _, substs) => ty:: UpvarSubsts :: Closure ( substs) ,
@@ -780,3 +783,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
780
783
}
781
784
}
782
785
}
786
+
787
+ /// Precise capture is enabled if the feature gate `capture_disjoint_fields` is enabled or if
788
+ /// user is using Rust Edition 2021 or higher.
789
+ fn enable_precise_capture ( tcx : TyCtxt < ' _ > , closure_span : Span ) -> bool {
790
+ tcx. features ( ) . capture_disjoint_fields || closure_span. rust_2021 ( )
791
+ }
0 commit comments